简体   繁体   English

jQuery Shake打破位置:固定

[英]jQuery Shake broke position:fixed

In a web site I want to shake a div which includes an image, it is fixed at the bottom of the page. 在一个网站中,我想摇一个包含图像的div,它固定在页面底部。 I try to add shake function to it, it is shaking but position changes to left side. 我尝试向其添加抖动功能,它正在抖动,但位置更改为左侧。 How can I fix it ? 我该如何解决? Let me write the code. 让我写代码。

     var css_shake={
     right: '225px',
     left: 'auto',
     position: 'fixed',
     bottom:'50px'
 }
  jQuery.fn.shake = function() {
  this.each(function(i) {
  jQuery(this).css(css_shake);
    for (var x = 1; x <= 3; x++) {
        jQuery(this).animate({ left: -25 }, 10).animate({ left: 0 }, 50).animate({ left: 25 }, 10).animate({ left: 0 }, 50);
    }
});
return this;
}

when write $(div).shake() it works but not I want to do. 当写$(div).shake()时,它可以工作,但不是我想要的。

 <div  id="affix_sepet">
    <img src="img/sepet_yeni.png" height="226" width="163">
 </div>

This div also have bootstrap affix : jQuery("#affix_sepet").affix() 该div也具有引导词缀:jQuery(“#affix_sepet”)。affix()

     var css_shake={
     right: '225px',
     left: 'auto',
     position: 'fixed',
     bottom:'50px'
 }
  jQuery.fn.shake = function() {
  this.each(function(i) {
  jQuery(this).css(css_shake);

        jQuery(this).animate({ left: -25 }, 10).animate({ left: 0 }, 50).animate({ left: 25 }, 10).animate({ left: 0 }, 50);
        jQuery(this).animate({ top: -25 }, 10).animate({ top: 0 }, 50).animate({ top: 25 }, 10).animate({ top: 0 }, 50);
        jQuery(this).animate({ left: -25 }, 10).animate({ left: 0 }, 50).animate({ left: 25 }, 10).animate({ left: 0 }, 50);
        jQuery(this).animate({ top: -25 }, 10).animate({ top: 0 }, 50).animate({ top: 25 }, 10).animate({ top: 0 }, 50);
        jQuery(this).animate({ left: -25 }, 10).animate({ left: 0 }, 50).animate({ left: 25 }, 10).animate({ left: 0 }, 50);
        jQuery(this).animate({ top: -25 }, 10).animate({ top: 0 }, 50).animate({ top: 25 }, 10).animate({ top: 0 }, 50);




});
return this;
}

You are using absolute left values for your animation rather than relative values. 您正在为动画使用绝对左值,而不是相对值。 Try using this syntax: 尝试使用以下语法:

jQuery(this).animate({ left : '+=25px' }, 10).animate({ left : '-=25px' }, 10) ;

currently you are bouncing the element around absolute position -25 to 25, and leaving it up against the left border. 目前,您是在-25到25的绝对位置附近弹跳元素,并使其离开左侧边框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM