制作简单的视差而不工作..
css top属性未设置或访问...
如何设置TOP以使视差工作.. pz
<div class="bg">
<div class="container">
<h1>Parallax Effect</h1>
<p class="lead">goes here</p>
</div>
</div>
CSS 中的
.bg {
background: url('http://sharafmedia.com/images/banner05.jpg') repeat;
background-position: center ;
background-size: cover;
width: 100%;
height: 300px;
top:0;
left:0;
z-index: -1;
padding:10px;
}
的jQuery
function parallax(){
var scrolled = $(window).scrollTop();
$('.bg').css('top', -(scrolled * 0.3) + 'px');
console.log($('.bg').css('top', -(scrolled * 0.3) + 'px'));
}
$(window).scroll(function(e){
parallax();
});
答案 0 :(得分:0)
尝试将 parseInt 添加到计算的滚动中。
function parallax(){
var scrolled = $(window).scrollTop();
$('.bg').css('top', parseInt(scrolled * 0.3 * -1) + 'px');
console.log($('.bg').css('top', parseInt(scrolled * 0.3 * -1) + 'px'));
}