using ++ in jquery
Just a quick one. I need a simple animation that basically says.
$("#rightArrow").hover(function() {
$('.projectImages'开发者_运维问答).css('left', '++27px');
});
I've tested it and it seems that the ++ is the wrong syntax here. Just wondering what it was that was supposed to go in. I've tried += and I've also tried using the animation feature but the closest I've got so far is this
$("#rightArrow").hover(function() {
$('.projectImages').animate({left: '+=27px'}, 'slow');
});
Which gives some interesting results but not quite what I was looking for.
I'm not sure there's a quick and easy way to do it, due to how Javascript handles strings and integers. Try:
$("#rightArrow").hover(function() {
newLeft = parseInt($(this).css('left'), 10) + 27;
$('.projectImages').animate({left: newLeft + 'px'}, 'slow');
});
I managed to create a fiddle with a continuously sliding object by starting/clearing an interval function:
timer=null;
$("#rightArrow").hover(function(e) {
timer=setInterval( function(){
var left = $("#x").position().left;
$("#x").animate({
left: ""+(left+100)+"px"
});
});
//alert($("#x").position().left);
},
function(e){ // exit function
clearInterval( timer );
}
);
in Javascript if you are using eval() you are probably doing something wrong. Use parseInt() instead.
精彩评论