javascript - Prevent shaking on hard scroll while animating scrollTop -
i scrolling section section, if scroll harshly mouse (not 1 easy scroll) or scroll harshly on laptop touchpad, shakes , down before scrolling animation starts or jumps 2 sections!
is possible make scroll smoothly , 1 section (not jump 2 sections) regardless of how hard scrolls , disable scrolling while animation taking place?
my code (jsfiddle):
var isanimated = false; var nbhdlength = $('.nbhd').length; var lastsectionid = nbhdlength - 1; var allheight = (nbhdlength - 1) * window.innerheight; var = true; function setheights() { $('.nbhd').css('height', window.innerheight); } setheights(); $('html').mousewheel(function(e) { var = e.deltay > 0; if (up) { console.log('up'); = true; } else { console.log('down'); = false; // console.log(up); } if (!up && $('#id' + lastsectionid).hasclass('scrolledto') || (!up && !$('.scrolledto').length)) { $('.scrolledto').removeclass('scrolledto'); return; } if (isanimated) return; isanimated = true; var currentsectionid = $('.nbhd.scrolledto').data('id'); ? currentsectionid-- : currentsectionid++; if (currentsectionid < 0) currentsectionid = 0; if (!$('.scrolledto').length) currentsectionid = lastsectionid; $('.scrolledto').removeclass('scrolledto'); var section = $('#id' + currentsectionid); section.addclass('scrolledto'); var pos = section.offset().top; $('body, html').animate({ scrolltop: pos }, 1000, function() { settimeout(function() { isanimated = false; }, 100) console.log($(window).scrolltop()); }); });
.div { width: 100%; } .red { background: red; } .yellow { background: yellow; } .green { background: green; } .blue { background: blue; } .abovefooter { background: gray; height: 200px; } .footer { background: black; height: 350px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js"></script> <div id="id0" data-id="0" class="nbhd red scrolledto"></div> <div id="id1" data-id="1" class="nbhd yellow"></div> <div id="id2" data-id="2" class="nbhd green"></div> <div id="id3" data-id="3" class="nbhd blue"></div> <div class="abovefooter"></div> <div class="footer"></div>
you need prevent default mousewheel event happening, using javascript perform desired effect instead.
$('html').mousewheel(function(e) { e.preventdefault(); //... }
Comments
Post a Comment