2012-10-06 274 views
0

我在使用jquery在鼠標懸停主div時更改子div的背景。jquery動畫背景

$('#bottom-a .module.responsive').hover(function() { 
    $('#bottom-a .responsive .mod-icon').css({ 
     "background-position": "left bottom" 
    }); 
}, function() { 
    $('#bottom-a .responsive .mod-icon').css({ 
     "background-position": "left top" 
    }); 
}); 

如何向此腳本添加動畫?感謝名單!

+1

有你看着jQuery的動畫功能? http://api.jquery.com/animate/ – Dan

+0

是的,但不能把它放在一起,今天有點醉了,所以很難想:D – LifeIsShort

+0

用'animate'替換'css' – Johan

回答

2

你可以簡單地用CSS做沒有修改代碼:

#bottom-a .responsive .mod-icon { 
    background-image:url('imgurl.jpg'); 
    transition: background 2s; 
    -moz-transition: background 2s; /* Firefox 4 */ 
    -webkit-transition: background 2s; /* Safari and Chrome */ 
    -o-transition: background 2s; /* Opera */ 
} 
+0

它工作完美!謝謝! – LifeIsShort

+0

... CSS3增加了很多功能,現在經常可以獲得令人驚歎的效果,只有使用CSS和沒有Javascript – obe6

1
$('#bottom-a .module.responsive').hover(function() { 
    $('#bottom-a .responsive .mod-icon').animate({ 
     "background-position": "left bottom" 
    },1000); 
}, function() { 
    $('#bottom-a .responsive .mod-icon').animate({ 
     "background-position": "left top" 
    },1000); 
}); 
+0

奇怪,但不起作用 – LifeIsShort

+0

這個解決方案不需要jQuery UI嗎? – 2012-10-06 19:28:45