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
Use jquery?
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>
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; });