【问题标题】:How to smooth the moving of background image?如何平滑背景图像的移动?
【发布时间】:2013-11-10 08:49:31
【问题描述】:

我已经成功创建了一个移动/平移背景图像。它在 jsfiddle 中看起来和工作正常:

http://jsfiddle.net/juhant/jxthp/6/

但是当我在浏览器中测试它时,移动并不顺畅,照片有时会冻结。

这是 HTML:

<div id="pageBg">
</div>

这是 CSS:

#pageBg {
    background: url('http://enos.itcollege.ee/~rselis/bg_front.jpg') no-repeat 0 0 scroll;    
    height: auto;
    left: 0;
    min-height: 900px;
    overflow: hidden;
    position: fixed;   
    top: 0;
    width: 100%;
    height: 100%;
}

还有 jQuery 位:

$(document).ready(function(){
   $('#pageBg').mousemove(function(e){
      var mousePosX = (e.pageX/$(window).width())*100;
      $('#pageBg').css('background-position-x', mousePosX +'%');

       var mousePosY = (e.pageY/$(window).height())*100;
$('#pageBg').css('background-position-y', mousePosY +'%');
   }); 
});

这是什么原因造成的,我该如何解决?先感谢您。

【问题讨论】:

  • 它在 chrome 中运行良好。
  • 好的,这很奇怪,因为它在我的 Chrome 浏览器中冻结了。那我应该在其他设备上测试它。有没有办法让它在 Mozilla 和 IE 中也能正常工作?
  • 在 chrome 上效果很好
  • 谢谢 Bharath 和 Szymon。你能说出为什么图像在 Mozilla 中根本没有移动吗?是否需要添加某种脚本才能使其在所有浏览器中运行?
  • 我认为它不起作用,因为 Mozilla 和 IE 无法识别 background-position-x 和 y。我正在尝试这样做

标签: jquery html css panning


【解决方案1】:

它现在可以在 firefox 和 ie 上运行,但在 firefox 上它太敏感但背景位置现在很好

            $('#pageBg').mousemove(function(e){
                var mousePosX = (e.pageX/$(document).width())*100;
                var mousePosY = (e.pageY/$(document).height())*100;

                $('#pageBg').css({
                    'background-position': mousePosX +'%' + mousePosY +'%'
                });

            }); 

已编辑:您必须更改 window => document/width/height 。 Firefox 不像其他浏览器那样计算鼠标右键。 所以有办法修复它或离开(窗口)并检测浏览器(firefox)然后

var mousePosY = (e.pageY/$(document).height()); //without 100

或将(窗口)更改为(文档),它适用于 Chrome、Opera、Firefox、IE、Maxthon 和 Safari。 对我来说应该是平滑的。

【讨论】:

  • 非常感谢。有没有办法处理这种敏感问题?
  • 没问题我很高兴能帮上忙,现在我正在寻找 FF 的问题
  • 我想我们说的是同一件事。我在我原来的帖子里提到过。移动不流畅,有时图像会冻结。现在我发现敏感性问题是所有浏览器的问题。如果这对您有帮助,我在这里上传了测试页面:[link]wv.webweaver.ee
  • 我看到您在移入文本(在您的网站中)背景不移动时遇到问题,请将您的 $('#pageBg').mousemove 更改为 $(document).mousemove
猜你喜欢
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多