jQuery动画|防止函数触发冗余
如何跟踪以下动画以查看是否已应用该动画? 通常我只会在变量打开时将其设置为1或true,但此动画基于类应用于多个元素jQuery动画|防止函数触发冗余,jquery,jquery-animate,Jquery,Jquery Animate,如何跟踪以下动画以查看是否已应用该动画? 通常我只会在变量打开时将其设置为1或true,但此动画基于类应用于多个元素 function animateDiv(what) { $(what).animate({ backgroundPosition:"0px -250px" },8000).css('background-position','0px -250px'); $(what).animate({ backgroundPosition:"0px
function animateDiv(what) {
$(what).animate({ backgroundPosition:"0px -250px"
},8000).css('background-position','0px -250px');
$(what).animate({
backgroundPosition:"0px 0px"
},8000).css('background-position','0px 0px');
}
$(document).on( 'hover', '.animation-div', function(){ animateDiv(this); } );
一种可能性是:
$(document).on('hover', '.animation-div', function() {
if (!$(this).hasData('done')) {
$(this).data('done');
animateDiv(this);
}
});
一种可能性是:
$(document).on('hover', '.animation-div', function() {
if (!$(this).hasData('done')) {
$(this).data('done');
animateDiv(this);
}
});
不确定这是否回答了问题,但其中一些可能会有所帮助
function addAnotherClass($self) {
$self.toggleClass('another_class_for_reference');
}
$('.animation-div').hover(
function() {
var $self = $(this);
$self.animate({"left": "+=50px"}, "slow");
$self.addClass('active');
addAnotherClass($self);
},
function() {
var $self = $(this);
$self.animate({"left": "-=50px"}, "slow");
$self.removeClass('active');
addAnotherClass($self);
}
);
不确定这是否回答了问题,但其中一些可能会有所帮助
function addAnotherClass($self) {
$self.toggleClass('another_class_for_reference');
}
$('.animation-div').hover(
function() {
var $self = $(this);
$self.animate({"left": "+=50px"}, "slow");
$self.addClass('active');
addAnotherClass($self);
},
function() {
var $self = $(this);
$self.animate({"left": "-=50px"}, "slow");
$self.removeClass('active');
addAnotherClass($self);
}
);
根据元素制作标志根据元素制作标志