webForumDet fria alternativet

förenkla denna kod

JavaScript

4 svar · 467 visningar · startad av room

Medlem sedan mars 2001270 inlägg
Frågan#1

Hej.. Jag håller på att lägga in ett mootools script och skulle vilja förenkla denna kod om det går. Känns lite dumt att skriva denna repeterande kodsnutt hela tiden. Skickar med de två första händelserna (portfolioasset 1 och 2) i den slutgiltiga så har jag ca 20 st liknande händelser. Hoppas ni förstår hur jag menar med att förenkla den.

	$('portfolioAsset1').addEvents({
		'mouseenter': function(){
			// Always sets the duration of the tween to 1000 ms and a bouncing transition
			// And then tweens the height of the element
			this.set('tween', {
				duration: 1000,
				//transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
			}).tween('height', '300px');
		},
		'mouseleave': function(){
			// Resets the tween and changes the element back to its original size
			this.set('tween', {}).tween('height', '149px');
		}
	});
	$('portfolioAsset2').addEvents({
		'mouseenter': function(){
			// Always sets the duration of the tween to 1000 ms and a bouncing transition
			// And then tweens the height of the element
			this.set('tween', {
				duration: 1000,
				//transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
			}).tween('height', '300px');
		},
		'mouseleave': function(){
			// Resets the tween and changes the element back to its original size
			this.set('tween', {}).tween('height', '149px');
		}
	});
Medlem sedan juni 20008 205 inlägg
#2

Lägg $('portfolioAsset1') och $('portfolioAsset2') i en array som du loopar över.

Medlem sedan mars 2001270 inlägg
#3

Ok. Låter bra. Men jag är totalt kass på javascript så därför frågar jag här. Skulle någon vänlig själ kunna kodsätta spango's svar.

Tack på förhand!

Medlem sedan aug. 20039 340 inlägg
#4
for(var i=1; i<=20; i++){
	$('portfolioAsset' + i).addEvents({
		'mouseenter': function(){
			// Always sets the duration of the tween to 1000 ms and a bouncing transition
			// And then tweens the height of the element
			this.set('tween', {
				duration: 1000,
				//transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
			}).tween('height', '300px');
		},
		'mouseleave': function(){
			// Resets the tween and changes the element back to its original size
			this.set('tween', {}).tween('height', '149px');
		}
	});
}

Om de olika delarna har olika storlek går det ocksp att lösa relativt enkelt. Säg till om så är fallet.

Medlem sedan mars 2001270 inlägg
#5

tack som fan!!!

255 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
121 ms — deklarationer (db)
0 ms — hämta statistik (cache)
128 ms — hämta tråd, inlägg och bilagor (db)
123 ms — ändringar (db)