jquery - How to force/guide scrolling to the nearest element only? -
an example of website stellar.js. problem is, using parallax library (scrollmagic) , don't want redo website stellar. how this? (jquery acceptable.)
edit: forgot mention sections full-page.
this lets user scroll , when stops bounces next position.
scrollto($(".start")); $(document).on('mousewheel ', function(e) { //e.preventdefault(); /** optional, might want try **/ /** elements bounce to: **/ var $elements = $("div[data-scrollbolock]"); /** next/previous element based on scrolling direction: **/ var $elementtoscrollto = $elements.eq(0); var scrolldirection = "down"; if (e.originalevent.wheeldelta >= 0) { scrolldirection = "up"; $elements = $($elements.get().reverse()); } $elements.each(function() { var scrolltop = $(window).scrolltop(), elementoffset = $(this).offset().top, distance = (elementoffset - scrolltop); if (scrolldirection === "down") { if (distance > 0) { $elementtoscrollto = $(this); return false; } } else { if (distance < 0) { $elementtoscrollto = $(this); return false; } } }); /** scrollto $elementtoscrollto if user stopped scrolling **/ cleartimeout($.data(this, 'scrolltimer')); $.data(this, 'scrolltimer', settimeout(function() { //user stopped scrolling scrollto($elementtoscrollto); }, 80)); }); function scrollto($el) { $('html,body').animate({ scrolltop: $($el).offset().top }, 500); }
div { height: 200px; width: 50%; border-bottom: 2px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-scrollbolock>1</div> <div class="start" data-scrollbolock>2</div> <div data-scrollbolock>3</div> <div data-scrollbolock>4</div> <div data-scrollbolock>5</div> <div data-scrollbolock>6</div>
Comments
Post a Comment