检测身体滚动命中顶部|元素的底部 [英] Detect when Body Scroll hits top | bottom of element
本文介绍了检测身体滚动命中顶部|元素的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在窗口滚动命中选定元素的顶部时使用javascript和jquery进行检测。我认为我正在取得进展,但仍然没有结果:
小提琴: https://jsfiddle.net/jzhang172/opvb2csy/
$(window).scroll(function() {var targetScroll = $('。div')。position()。top; if($(window).scrollTop()> targetScroll){alert('hey');});});
body,html {height:2000px;}。div {height:300px;宽度:300像素;位置:绝对的;顶:500像素;背景:red;}
< script src =https ://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js>< / script>< div class =div>< / div>
解决方案
<$ c $(window).scrollTop()> $($)$(window).scroll(function(){
var targetScroll = $('。div')。position ; targetScroll){
alert('hey');
}); //< ===语法错误:关闭if块
});
I'm trying to detect using javascript and jquery when the window scroll hits the top of a selected element. I think I'm making progress but still no results:
fiddle: https://jsfiddle.net/jzhang172/opvb2csy/
$(window).scroll(function() {
var targetScroll = $('.div').position().top;
if($(window).scrollTop() > targetScroll){
alert('hey');
});
});
body,html{
height:2000px;
}
.div{
height:300px;
width:300px;
position:absolute;
top:500px;
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="div"></div>
解决方案
$(window).scroll(function() {
var targetScroll = $('.div').position().top;
if($(window).scrollTop() > targetScroll) {
alert('hey');
}); // <=== Syntax Error: Closing Parenthese around an if block
});
这篇关于检测身体滚动命中顶部|元素的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文