webForumDet fria alternativet

förenkla denna kod

4 svar · 467 visningar · startad av room

roomMedlem sedan mars 2001270 inlägg
#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');
		}
	});
spangoMedlem sedan juni 20008 205 inlägg
#2

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

roomMedlem 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!

nitro2k01Medlem sedan aug. 20039 342 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.

roomMedlem sedan mars 2001270 inlägg
#5

tack som fan!!!

130 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
127 ms — hämta tråd, inlägg och bilagor (db)