2013-06-30 36 views
1

在一個網站,我想撼動一個包含圖像的div,它固定在頁面的底部。我嘗試給它添加搖動功能,它正在晃動,但是位置變爲左側。我該如何解決它?讓我寫代碼。jQuery Shake破解位置:固定

 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; 
} 

當寫$(div).shake()它的作品,但不是我想要做的。

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

這個div也有引導詞綴:jQuery的( 「#affix_sepet」)加蓋()

+0

你可以發佈的HTML以及? – jbkkd

回答

0

您正在使用絕對左側值動畫,而不是相對值。 嘗試使用此語法:

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

當前正在彈跳圍繞元素絕對位置-25至25,並留給了對左邊框。

+0

我試過不同的圖書館,但現在我明白了。 –

0
 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; 
} 
+0

不,它不起作用,它是一樣的 –

+0

:| ...問題解決了!? –

+0

不,它還是一樣,我想我會用一個圖書館。 –