当我放置我的div id标签#fading时,一切似乎都会立即消失。我希望我的部件一个接一个地褪色;个别。我对jquery不太熟练,所以一些帮助会很棒。
第1部分 第2部分 第3部分 第4部分 第5部分
</div>
</div>
</nav>
</div>
<div style="background:white;">
<div class="row" id="fading" style="padding-left:15%; padding-right:15%;margin-top:100px;">
<div class="col-xs-8">
<p><b><em>Part 1:</b></em> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-xs-4">
<img src="./photo1.jfif" class="img-responsive ">
</div>
</div>
<div class="row" style="padding-left:15%; padding-right:15%;margin-top:150px;">
<div class="col-xs-8">
<p><b><em>Part 2:</b></em> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-xs-4">
<img src="./photo2.jpg" class="img-responsive ">
</div>
</div>
<div class="row" style="padding-left:15%; padding-right:15%;margin-top:200px;">
<div class="col-xs-8">
<p><b><em>Part 3:</b></em> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-xs-4">
<img src="./photo3.jfif" class="img-responsive ">
</div>
</div>
<div class="row" style="padding-left:15%; padding-right:15%;margin-top:250px;">
<div class="col-xs-8">
<p><b><em>Part 4:</b></em> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-xs-4">
<img src="./photo4.jfif" class="img-responsive ">
</div>
</div>
<div class="row" style="padding-left:15%; padding-right:15%;margin-top:300px;">
<div class="col-xs-8">
<p><b><em>Part 5:</b></em> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-xs-4">
<img src="./photo5.jfif" class="img-responsive ">
</div>
</div>
</body>
以下是需要修复或更改的脚本。
<script>
var target = $('#fading');
var targetHeight = target.outerHeight();
$(document).scroll(function(e){
var scrollPercent = (targetHeight - window.scrollY) / targetHeight;
if(scrollPercent >= 0){
target.css('opacity', scrollPercent);
}
});
</script>
答案 0 :(得分:1)
我认为这是你可能会追求的?
http://codepen.io/anon/pen/pJomJY?editors=001
该脚本现在考虑了每个.row
元素的滚动百分比:
$(window).scroll(function() {
var posY = window.scrollY;
$('.row').each(function(key, item) {
var target = $(item);
var topMargin = parseInt(target.css('margin-top'));
var targetHeight = target.outerHeight(true);
var targetStart = target.position().top;
var targetEnd = targetStart+targetHeight;
var scrollPercent = (targetEnd-posY)/targetHeight;
scrollPercent = Math.max(0, Math.min(scrollPercent, 1));
if (posY > targetStart-topMargin) {
target.css('opacity', scrollPercent);
}
});
});
答案 1 :(得分:0)
尝试以下查询:
$(document).scroll(function(e){
$('.row').each(function(i, o) {
var target = $(o);
var targetHeight = target.outerHeight() + target.position().top;
var scrollPercent = (targetHeight - window.scrollY) / targetHeight;
if(scrollPercent >= 0){
target.css('opacity', scrollPercent);
}
})
});
我希望它可以帮到你!