我尝试在我的网站上实现视差滚动。这几乎是完美的。问题是,我希望在底部几乎有一个视差部分。当我向下滚动时,我将图像设置为更高的背景图像,如下所示:
照片应该已经停在灰色部分的开头,你会看到吼叫。
我的代码:
// Can also be used with $(document).ready() / $(window).load()
$(document).ready(function() {
// Cache the Window object
$window = $(window);
$('section[data-type="background"]').each(function(){
var $bgobj = $(this); // assigning the object
$(window).scroll(function() {
// Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = -($window.scrollTop() / $bgobj.data('speed'));
// Put together our final background position
var coords = '50% '+ yPos + 'px';
// Move the background
$bgobj.css({ backgroundPosition: coords });
}); // window scroll Ends
});
});
HTML标记下方:
<section class="quote" id="quote-home" data-speed="4" data-type="background">
<div class="container_12">
<header class="grid_12 centered">
<h1>
The beginning of knowlegde
<br>
is the discovery of something
<br>
we do not understand.”
</h1>
<span class="cross-x"></span>
<h2>- Frank Herbert</h2>
</header><!-- End header.grid_12 -->
<div class="clear"></div><!-- End div.clear -->
</div><!-- End div.container_12 -->
</section><!-- End section#quote -->
<section id="twitter" class="alt">
和css:
#quote-home {
height: 500px;
margin: 0 auto;
width: 100%;
max-width: 100%;
position: relative;
background: url(../images/coffeescripthands.jpg) 50% 0 no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}