Trigger event when user scroll to specific element - with jQuery

后端 未结 12 1460
攒了一身酷
攒了一身酷 2020-11-22 11:52

I have an h1 that is far down a page..

TRIGGER EVENT WHEN SCROLLED TO.

and I want to trigger an alert

12条回答
  •  深忆病人
    2020-11-22 12:36

    This should be what you need.

    Javascript:

    $(window).scroll(function() {
        var hT = $('#circle').offset().top,
            hH = $('#circle').outerHeight(),
            wH = $(window).height(),
            wS = $(this).scrollTop();
        console.log((hT - wH), wS);
        if (wS > (hT + hH - wH)) {
            $('.count').each(function() {
                $(this).prop('Counter', 0).animate({
                    Counter: $(this).text()
                }, {
                    duration: 900,
                    easing: 'swing',
                    step: function(now) {
                        $(this).text(Math.ceil(now));
                    }
                });
            }); {
                $('.count').removeClass('count').addClass('counted');
            };
        }
    });
    

    CSS:

    #circle
    {
        width: 100px;
        height: 100px;
        background: blue;
        -moz-border-radius: 50px;
        -webkit-border-radius: 50px;
        border-radius: 50px;
        float:left;
        margin:5px;
    }
    .count, .counted
    {
      line-height: 100px;
      color:white;
      margin-left:30px;
      font-size:25px;
    }
    #talkbubble {
       width: 120px;
       height: 80px;
       background: green;
       position: relative;
       -moz-border-radius:    10px;
       -webkit-border-radius: 10px;
       border-radius:         10px;
       float:left;
       margin:20px;
    }
    #talkbubble:before {
       content:"";
       position: absolute;
       right: 100%;
       top: 15px;
       width: 0;
       height: 0;
       border-top: 13px solid transparent;
       border-right: 20px solid green;
       border-bottom: 13px solid transparent;
    }
    

    HTML:

    145
    145
    1234

    Check this bootply: http://www.bootply.com/atin_agarwal2/cJBywxX5Qp

提交回复
热议问题