所以我需要一个网站的一部分来让背景图像产生视差。问题是代码从网站的开头计算视差(它是一个长向下滚动类型的网站),当我到达提到的部分时,背景已经太远了。代码中是否需要某种“延迟”,以便在滚动 X 个像素后开始视差?这里的代码:
查询:
$(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 id="background" data-type="background" data-speed="5">
<div class="container">
<div class="shadow-block"><img src="images/shadow-b.png" alt="" class="scale-with-grid"/></div>
<div class="quote two-thirds column" >
</div>
</div>
</section>
CSS:
#background {
background: url(../images/big-bg.jpg)no-repeat center center;
background-size:cover;
width: 100%;
height:500px;
padding: 40px 0;
overflow: hidden;
position: relative;
}