所以我正在使用jQuery脚本;
$(window).scroll(function(){
$(".element").css("opacity", 1 - $(window).scrollTop() / 700);
});
隐藏绝对定位的flexbox元素,因为它滚动到页面顶部。我在页面上还有其他内容,我想要应用同一套规则;我希望它们在页面底部可见时达到100%不透明度,然后在用户滚动时让它们淡出,但这似乎只适用于最初在窗口中显示的第一个元素。我不确定为什么会这样 - 我已经使用了700
值,但对于页面下方的元素它似乎永远不准确。
我正在做的内容是文字和图片。我认为这可能不是我希望的方式 - 如果我可以让它工作,理想情况下,文本块从顶部到底部逐渐淡出而不是整个元素,但我理解用这种方法可能是不可能的。
答案 0 :(得分:2)
我就是这样做的:
.topGradient{
height: 100px;
width: 100%;
background:linear-gradient(rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0));
position: fixed;
top:0;
z-index: 100;
}
.bottomGradient{
height: 100px;
width: 100%;
background:linear-gradient(to top, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0)); position: fixed;
bottom:0;
z-index: 100;
}
我所做的是创建2个div并将它们放在窗口的顶部和底部。然后我使用z-index将它们设置在所有其他元素之上,然后给它们一个具有透明度的渐变,从而为您提供元素的淡入/淡出效果。
答案 1 :(得分:2)
我建议您对所有要使用淡入淡出行为的元素使用公共类。您也可以使用选择器的组合。实现的主要问题是您只能监听视口的滚动位置,忽略了页面上的不同元素与文档顶部的垂直距离不同的事实。 您必须计算元素相对于视口顶部的位置。
因此,要做到这一点,你必须:
在我的概念验证示例中,我有:
$(window).scroll(function() {
// Setting: Start fading halfway up the page
var startPos = 0.5;
// Cache window object
var $w = $(window);
// Basically, we go through each element and check its relative position within the viewport
$('.scrollFade').each(function() {
// Get current relative position in viewport, based on the top edge
var pos = $(this).offset().top - $w.scrollTop();
// Get viewport height
var vh = $w.height();
if (pos < vh * startPos) {
// If element has past the starting threshold, we fade it
$(this).css('opacity', pos / (vh * startPos) * 1);
} else {
$(this).css('opacity', 1);
}
});
});
.textblock {
position: absolute;
display: flex;
flex-direction: column;
justify-content: flex-end;
bottom: 0px;
}
.extratext {
margin-top: 500px;
}
div {
height: 100vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p class="textblock scrollFade">
hey hey hey!
</p>
</div>
<div>
<p class="extratext scrollFade">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
1.10.32.
</p>
</div>
答案 2 :(得分:1)
您需要获取页面上的元素位置,并将其用作淡入淡出开始时的偏移量。 Here is an updated fiddle
Train_classifier[Train_classifier$Note.Reco!="" & Train_classifier$Reason.Reco!="",]
答案 3 :(得分:1)
fadeAtTop(el) {
const startPos = 0.25;
const $window = $(window);
const viewportHeight = $window.height();
el.toArray().forEach(el => {
const $el = $(el);
let position = $el.offset().top - $window.scrollTop();
let opacity = position < viewportHeight * startPos ? position / (viewportHeight * startPos) * 1 : 1;
$el.css('opacity', opacity);
});
}