Div captures mouse scroll
P粉729436537
P粉729436537 2023-11-01 18:57:05
0
2
660

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粉729436537
P粉729436537

reply all(2)
P粉457445858

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>
P粉276064178

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;
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!