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');
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745587367a4634624.html
评论列表(0条)