santa santa - 2 months ago 13
jQuery Question

Animate only once

I use jQuery .animate() to move an item once, but it seems to be moving in increments of 100px multiple times. Is it because of mouseover over?

var ww = $(window).width();

$(document).on('mouseover', '#myTarget', function () {
var dp = $(this).offset().left;

if ((ww - dp) < 150) {
$('.myDiv.active').animate({left:'-=100px'},500);
}
});

Answer

Changing it to $(document).on('mouseenter', '#myTarget', function () { would be better. Every time your mouse enters or leaves the element or one of its children, a mouseover event is triggered. This will not happen with mouseenter. The example here shows this.

See this answer for a similar question and the source for the example provided.