首页 > jQuery中animate()方法重复执行导致浏览器卡死

jQuery中animate()方法重复执行导致浏览器卡死

问题:含animate()的方法重复点击10次左右的时候会出现卡顿然后卡死,浏览器卡死的情况。暂时发现浏览器使用内存增加,不知道是不是这个问题
代码:
function addToCart_fly(obj,goodsId){
/购物车图标往上浮起/
var v_cart_fly = $(".ect-icon-flow").clone();
v_cart_fly.css({
'z-index': 9000,
'position': 'absolute',
'top': $(".ect-icon-flow").offset().top +'px',
'left': $(".ect-icon-flow").offset().left +'px',
'width': $(".ect-icon-flow").width() +'px',
'height':$(".ect-icon-flow").height() +'px',
'display':'block'
});
$(".ect-icon-flow").parent().append(v_cart_fly);
v_cart_fly.animate({
top:-30,
left:$(".ect-box-flex:eq(4)").offset().left-49,
width:$(".ect-icon-flow").width(),
height:$(".ect-icon-flow").height(),
},'normal');

/*商品飞入购物车*/
var div =$(obj);      
var cloneDiv = $(obj).clone().css('opacity','0.7');
cloneDiv.css({
        'z-index': 9000,
        'position': 'absolute',
        'top':  div.offset().top +'px',
        'left':  div.offset().left +'px',
        'width': div.width() +'px',
        'height':div.height() +'px'
});
/*购物车飞回*/
div.parent().append(cloneDiv);
cloneDiv.animate({
        top:$(".ect-box-flex:eq(4)").offset().top,
        left:$(".ect-box-flex:eq(4)").offset().left-40,
        width:0,
        height:0,
    },'slow').fadeTo(0,0.9).hide(0);

v_cart_fly.animate({
   top:5,
   left:$(".ect-box-flex:eq(4)").offset().left-49,
   width:$(".ect-icon-flow").width(),
   height:$(".ect-icon-flow").height(),
},,'normal').hide(0);}

效果:购物车上浮,商品飞入上浮的购物车,购物车归位


在animate()之前加个stop()
x.stop().animate(...)


给animate加上stop();
stop() 方法停止当前正在运行的动画。
$(selector).stop(stopAll,goToEnd)

stopAll可选。规定是否停止被选元素的所有加入队列的动画。
goToEnd可可选。规定是否允许完成当前的动画。该参数只能在设置了 stopAll 参数时使用。

【热门文章】
【热门文章】