【发布时间】:2023-04-11 01:30:02
【问题描述】:
当元素在视口中时,我一直试图在滚动上显示它,如果没有,则再次隐藏它。但无论我怎么尝试,我都无法成功。
这是我目前所拥有的,但该函数只运行一次,当页面加载而不是滚动时,所以它不会更新值。
$(window).scroll(function() {
var top_of_element = $("#cont_quote blockquote").offset().top;
var bottom_of_element = $("#cont_quote blockquote").offset().top + $("#cont_quote blockquote").outerHeight();
var bottom_of_screen = $(window).scrollTop() + window.innerHeight;
var top_of_screen = $(window).scrollTop();
if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
$('#cont_quote blockquote').animate({'opacity':'1'},1000);
}
else {
$('#cont_quote blockquote').animate({'opacity':'0'},1000);
}
});
<section id="cont_quote">
<article class="cont_q">
<blockquote>Lorem ipsum</blockquote>
</article>
</section>
【问题讨论】:
标签: javascript jquery html scroll viewport