search

Home  >  Q&A  >  body text

Div captures mouse scroll

I currently have a page of the following form

<div id="content">
    <div id="content-page-1">
    <!--content-->
    </div>
    <div id="content-page-2">
    <!--content-->
    </div>
</div>

Is there any way to scroll

  1. Paste/Align div (100% height and 100% width of display area)
  2. Automatically scroll to the next div when scrolling is detected

Use jquery?

P粉729436537P粉729436537467 days ago714

reply all(2)I'll reply

  • P粉457445858

    P粉4574458582023-11-02 17:19:57

    I tried different plugins but they all had issues firing multiple events in mvc so I came up with this solution using underscore.js

    <script  type="text/javascript">
        $(document).ready(function () {
                var isc = _.throttle(function (event) {
                    if ($(window).scrollTop() + $(window).height() > $(document).height() - 200) {
                        if (event.handled !== true) {
                            $.post('@path', function (html) {
                                $('#user-feed').append(html);
                            });
                        }
                    }
                }, 300);
    
                $(window).scroll(isc);
        });
    
    </script>

    reply
    0
  • P粉276064178

    P粉2760641782023-11-02 16:14:55

    If you listen to scroll events on the node, you can easily use a plugin like scrollTo to smoothly scroll to the "next div" or the previous div (however you define it).

    var prevScrollTop = 0;
    var $scrollDiv    = $('div#content');
    var $currentDiv   = $scrollDiv.children('div:first-child');
    $scrollDiv.scroll(function(eventObj)
    {
        var curScrollTop = $scrollDiv.scrollTop();
        if (prevScrollTop < curScrollTop)
        {
        // Scrolling down:
            $currentDiv = $currentDiv.next().scrollTo();
        }
        else if (prevScrollTop > curScrollTop)
        {
        // Scrolling up:
            $currentDiv = $currentDiv.prev().scrollTo(); 
        }
        prevScrollTop = curScrollTop;
    });

    reply
    0
  • Cancelreply