jquery - jquery动画中的背景图片位置变化

标签 jquery html

我想更改 jquery 动画中的背景图像位置。但是我的代码不起作用。谁能帮我把它短路。

以下是我的代码

CSS

#pnav a{
    background:url(http://www.us-bingo.com/images/Tickets/Solid-Color-Tyvek-Wrist-Tickets-lg.gif) 0 0 no-repeat;
    display:block;
    width:20px;
    height:42px;
}

查询

$('#pnav a')
.hover(function () {
    $(this).stop().animate({
        'background-position' : '(0px -42px)'
    }, 150);
}, function () {
    $(this).stop().animate({
        'background-position' : '(0 0)'
    }, 150);
});

html

<div id="pnav">
<a href="#">123</a>
</div>

这是Fiddle file

提前谢谢你。

最佳答案

我认为 jQuery 动画无法为 background-position 设置动画,但您可以使用 CSS 过渡来代替

    #pnav a{
    background:url(http://www.us-bingo.com/images/Tickets/Solid-Color-Tyvek-Wrist-Tickets-lg.gif) 0 0 no-repeat;
    display:block;
    width:20px;
    height:42px;

    -webkit-transition: background-position 150ms ease-in-out;
    -moz-transition: background-position 150ms ease-in-out;
    -ms-transition: background-position 150ms ease-in-out;
    -o-transition: background-position 150ms ease-in-out;
    transition: background-position 150ms ease-in-out;
}

#pnav a:hover {
    background-position:0px -42px;

}

这是一个 fiddle :http://jsfiddle.net/pavloschris/eg5zC/7/transition 浏览器兼容性:http://caniuse.com/#search=transition

关于jquery - jquery动画中的背景图片位置变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15062440/

相关文章:

javascript - 未捕获的 TypeError : $(. ..).modal 不是函数

html - 汉堡菜单字体大小问题

javascript - 使用 Javascript 实现水平子菜单

javascript - 如何取消选中的单选按钮

javascript - 自动增加客户数量。

html - 我的网站安全吗?它安装了 SSL 证书,但地址栏上没有锁定

C# 代码 : Is this how ASP. NET AJAX 脚本可以工作吗?

javascript - 使用 JQuery 单击元素

jQuery 全日历 : Fadeout deleted Event

javascript - 如何为 2 个子 div 添加包装器 div(所有子 div 具有相同的类)