admin管理员组文章数量:1432187
i have a div in the middle of body tag
<div id="place">
</div>
what i want to do is, when i scroll down and e across the div with id "place", i want to show an alert.the logic i set is when the window scroll postion is greater than the div from the window top , i execute alert . i know that my logic is stupid ! but i want to learn how to do this .
what i have tried so far
$(window).scroll(function(){
var toElement = $("#place").position();
if(scroll.positon() > toElement){
alert("hello");
}
});
i am new to jquery, so could you help me
i have a div in the middle of body tag
<div id="place">
</div>
what i want to do is, when i scroll down and e across the div with id "place", i want to show an alert.the logic i set is when the window scroll postion is greater than the div from the window top , i execute alert . i know that my logic is stupid ! but i want to learn how to do this .
what i have tried so far
$(window).scroll(function(){
var toElement = $("#place").position();
if(scroll.positon() > toElement){
alert("hello");
}
});
i am new to jquery, so could you help me
Share Improve this question edited Mar 14, 2014 at 4:59 Sahil Mittal 20.8k12 gold badges68 silver badges91 bronze badges asked Aug 16, 2013 at 10:44 Wang'l PakhrinWang'l Pakhrin 8683 gold badges16 silver badges31 bronze badges 1- possible duplicate: stackoverflow./questions/8554580/… – DevlshOne Commented Aug 16, 2013 at 10:47
2 Answers
Reset to default 3Try this:
$(window).scroll(function() {
var offset = $("#place").offset().top;
if ($(window).scrollTop() >= offset) {
alert("hello");
}
});
You can trigger an event from your script after you have made the div visible using the .trigger function
e.g
//declare event to run when div is visible
function isVisible(){
alert("hi");
}
//hookup event
$('#place').bind('isVisible', isVisible);
//show div and trigger custom event in callback when div is visible
$('#place').show('slow', function(){
$(this).trigger('isVisible');
});
本文标签: javascriptneed to show alert when i scroll down a certain div in htmlStack Overflow
版权声明:本文标题:javascript - need to show alert when i scroll down a certain div in html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745587366a2664990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论