javascript 使元素滚动更慢(视差)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9235568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make element scroll slower (Parrallax)
提问by Liam
I have an element on my page absolutely positioned.
我的页面上有一个绝对定位的元素。
Im trying to write a snippet of jQuery to make that element scroll at a slower rate than the rest of the elements on the page.
我试图编写一段 jQuery 来使该元素以比页面上其他元素更慢的速度滚动。
I've written this so far but cannot seem to get it too work at all. Has anybody experience with this and if so would you care explaining?
到目前为止,我已经写了这篇文章,但似乎根本无法让它发挥作用。有没有人有这方面的经验,如果有,你愿意解释吗?
$(document).ready(function() {
$window = $(window);
$('.twit-bird').css({
'top' : -($('window')/3)+"px"
});
});
I've also tried to add an anchor, a fixed div at the top of my window to work out the calcs from that with no luck...
我还尝试在我的窗口顶部添加一个锚点,一个固定的 div 来计算它,但没有运气......
also tried this
也试过这个
$(document).ready(function() {
// Cache the Window object
windowScroll = $(this).scrollTop();
$(window).scroll(function() {
$('.twit-bird').css({
'top' : -(windowScroll/3)+"px"
});
});
});
回答by Timothy Aaron
I can point you in the right direction. You need your $('.twit-bird').css()
to get called every time the window is scrolled. Also you forgot .scrollTop()
, and don't quote window
(or, even better just use this
) ...
我可以为您指明正确的方向。$('.twit-bird').css()
每次滚动窗口时,您都需要调用它。你也忘记了.scrollTop()
,不要引用window
(或者,甚至更好地使用this
)......
$(window).scroll(function () {
$('.twit-bird').css({
'top' : -($(this).scrollTop()/3)+"px"
});
});