开发者

jQuery toggle backgroundPosition on hover?

开发者 https://www.devze.com 2023-04-02 15:30 出处:网络
This changes background position on hover, I want to return position to 0 0 on mouse out. How do I do it?

This changes background position on hover, I want to return position to 0 0 on mouse out. How do I do it?

$("#love").hover(function 开发者_StackOverflow中文版() {
    document.getElementById('love').style.backgroundPosition= "0 -19px";
});


In this situation, you should use CSS rather than JavaScript because it will be faster:

<style type="text/css">
#love {
    background-position: 0 0;
}
#love:hover {
    background-position: 0 -19px;
}
</style>


$("#love").hover(function () {
document.getElementById('love').style.backgroundPosition= "0 -19px";
},function () {
 document.getElementById('love').style.backgroundPosition= "0 0";
});


Use power of jQuery

$("#love").hover(function () {
    $(this).css('background-position', '0 -19px');
}, function() { 
    $(this).css('background-position', '0 0');
});

or it can be done just in css using :hover

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号