最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - need to show alert when i scroll down a certain div in html - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

Try 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');
});
发布评论

评论列表(0)

  1. 暂无评论