我爲wordpress主題的側欄小部件創建了切換下拉效果。使用jQuery爲多個背景位置設置動畫效果
h2
小工具內的標籤有兩個不同背景位置的背景圖像。
的CSS這個背景是這樣的:
background-image: url(images/h2bg.png), url(images/button.png);
background-position: center left, bottom right;
background-repeat: no-repeat,no-repeat;
我想要做的是動畫第一背景(h2bg.png)從元素框,當用戶點擊h2元素上,然後將第二個背景圖像的位置更改爲top left
。 (這是一個很小的精靈圖像)
我的jQuery腳本是這樣的:(你不需要去通過它)
$(document).ready(function() {
$('#sidebar div.widget').each(function(i) {
height = $(this).css('height');
$(this).attr('data-height',height);
$(this).attr('data-animate','ON');
});
$('.widget h2').click(function() {
if ($(this).parent('div.widget').attr('data-animate') == 'OFF') {
$(this).parent('div.widget').attr('data-animate','ON');
height = $(this).parent('div.widget').attr('data-height');
$(this).parent('div.widget').animate({
height: height
},500);
$(this).animate({
'background-position-x': '0%, 100%'
},500,function() {
$(this).css('background-position-y','50%, 100%');
});
} else {
$(this).parent('div.widget').attr('data-animate','OFF');
$(this).parent('div.widget').animate({
height: '29px'
},500);
$(this).animate({
'background-position-x': '-100%, 100%'
},500,function() {
$(this).css('background-position-y','50%, 0%');
});
}
});
});
長話短說。我注意Java返回background-position-x
和background-position-y
作爲percenatges之間用分號分隔。
所以我在動畫中使用的百分比,它對一個背景圖像工作正常。但是當涉及到多個背景時,它不起作用!我用分號嘗試它...沒有分號和像素值,但沒有運氣!
我該如何處理jQuery的多個背景位置屬性?
你可以舉一個如何使用這些屬性的例子嗎?它會幫助那些不熟悉jQuery的人。 – KLee1