【问题标题】:jQuery animate() backgroundPosition not working finejQuery animate() backgroundPosition 无法正常工作
【发布时间】:2012-01-18 18:27:51
【问题描述】:

我有这个简单的 HTML:

<span class="coverImg" style="background-image:url('images/show2.jpg');"></span></a>

还有一些 Javascript:

$(function() {
            $(".coverImg").hover(function() {
                $(this).animate({
                    backgroundPosition : "0 0"
                }, "fast");
            }, function() {
                $(this).animate({
                    backgroundPosition : "50% 50%"
                }, "fast");
            });
        });

所以当鼠标悬停时功能正常工作虽然动画不是那么完美好而且几乎看不到缓动.. 但是当 mouseout 功能不起作用时,背景图像就坐在那里,即使在像素上也不会移动......

有什么问题?我错过了什么?

或者:

$(function() {
            $(".coverImg").mouseover(function() {
                $(this)
                .animate({
                    "background-position-x" : "-=20px",
                    "background-position-y" : "-=20px"
                }, "fast");
            }).mouseout(function() {
                $(this).animate({
                    "background-position-x" : "0 ",
                    "background-position-y" : "0"
                }, "fast");
            })
        })

这仅适用于 Chrome...

那么又是什么问题!什么错误!我有什么想念的?!

【问题讨论】:

  • 请在下方发布您的解决方案,然后“接受”您自己的答案。这是在这里回答您自己的问题的最恰当方式。
  • 哦,我明白了,第一次原谅我,实际上我对自己的解决方案有误......

标签: jquery jquery-animate background-image background-position


【解决方案1】:

我不认为 jQuery 可以将背景位置设置为默认动画——我使用 http://archive.plugins.jquery.com/project/backgroundPosition-Effect

标准 CSS 不支持 background-position-xbackground-position-y,只有少数像 Chrome 一样支持。

而 jQuery 的 animate() 方法不支持同时对两个值进行动画处理,有时会出错,或者在某些浏览器中什么也不做。

毕竟,请查看http://snook.ca/archives/javascript/jquery-bg-image-animations。如果您想为背景位置设置动画,应该有一个名为 jQuery.bgpos.js 的 jQuery 插件。

代码是这样的:

(function($) {
$.extend($.fx.step, {
    backgroundPosition : function(fx) {
        if(fx.state === 0 && typeof fx.end == 'string') {
            var start = $.curCSS(fx.elem, 'backgroundPosition');
            start = toArray(start);
            fx.start = [start[0], start[2]];
            var end = toArray(fx.end);
            fx.end = [end[0], end[2]];
            fx.unit = [end[1], end[3]];
        }
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];
        function toArray(strg) {
            strg = strg.replace(/left|top/g, '0px');
            strg = strg.replace(/right|bottom/g, '100%');
            strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
            var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
            return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
        }

    }
});})(jQuery);

【讨论】:

  • 谢谢!我会检查这个。我找到了另一种解决问题的方法,请检查我的编辑!它对我来说很好用!
  • 我刚刚发现这只能在 Chrome 中工作,甚至不能在 firefox 中使用......这一定是一个错误......
  • demo:protofunc.com/scripts/jquery/backgroundPosition 在 Firefox 中工作...确保这一行:“background-position-x”:“0”与示例匹配。
  • 我上一条评论中的示例网站对您有用吗? (试图缩小错误)
  • 它有效!你是什​​么意思“确保这一行:“background-position-x”:'0'与示例匹配“?
【解决方案2】:

似乎您对 jQuery 使用了一种严厉的方法。这可以单独使用 css 完成:

span {
    background: url(yourimage.jpg) top left no-repeat;
    transition: all 3s ease-in-out;
}

span:hover {
    background-position: 50% 50%;
}

背景位置的变化在现代浏览器中是动画的,在 IE8 及更低版本中只是静态变化。

您可能还想添加 transition 属性的其他浏览器特定前缀版本:

-webkit-transition:  
-moz-transition:  
-o-transition:  
transition:

【讨论】:

  • 谢谢! -o-transition 代表 Opera,对吗?我的意思是它在歌剧中应该可以正常工作,但事实并非如此,它是有线的……而且 IE9 也不能很好地支持。 chrome 和 FF 真的很好用……我可能还有其他错误?
  • 我认为 -e-transition: 适用于 IE9
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
相关资源
最近更新 更多