【发布时间】:2014-02-10 20:00:26
【问题描述】:
本质上,我试图通过根据滚动距离为 div 的背景位置设置动画来实现视差效果。这在 Firefox 中完美运行,但基于 webkit 的浏览器似乎无法正常工作。也就是说,所有其他浏览器都没有返回 body 的 scrollTop() 值。我试过'document','window','body,html'等,但没有其他工作。
这是小提琴:http://jsfiddle.net/7WVc4/
这是 JS:
$(document).ready(function(){
$('body').scroll(function(){
var scrolled = $('body').scrollTop();
scrolled*=-0.4;
$('#container').css('background-position','0px ' + scrolled + 'px');
});
});
还有 HTML:
<div id='container'></div>
还有 CSS:
#container {
background-image:url('http://subtlepatterns.com/patterns/food.png');
background-attachment: fixed;
width:80%;
margin:auto;
height:1000px;
}
【问题讨论】:
-
@Ashley Medway 感谢您的回答 - 它现在可以在 Chrome 中运行,但不能在 Firefox 中运行,也许一些有条件的浏览器可以解决它?
-
@Drew Baker 在 JSFiddle 中运行良好,但恐怕在 Chrome、Safari 或 Firefox 中无法正常运行。
-
请直接将 cmets 添加到我的答案中。我用一个链接更新了它,表明它确实可以在野外工作。
-
你解决了吗?我的回答正确吗?
-
Fin,你还在做这个吗?你能接受答案吗?
标签: jquery webkit background-image parallax scrolltop