jquery - Images tremble on animate -


i wrote little script animate elements of list on mousewheel scroll

html

<div id="scroll">     <ul>         <li style="background-image: url('http://i.imgur.com/egmfpfq.jpg')"></li>         <li style="background-image: url('http://i.imgur.com/qjmcv5g.jpg')"></li>         <li style="background-image: url('http://i.imgur.com/egmfpfq.jpg')"></li>         <li style="background-image: url('http://i.imgur.com/qjmcv5g.jpg')"></li>         <li style="background-image: url('http://i.imgur.com/egmfpfq.jpg')"></li>     </ul> </div> 

jquery

$.fn.scroll = function () { var $this = $(this); var $list = $(this).find('ul'); var $lis = $list.find('li'); var count = $lis.length; var direction, currentslideposition;  $this.addclass('scroll'); $list.addclass('slides-list'); $lis.addclass('slide'); $lis.filter(':first').addclass('current'); $lis.filter(':not(:first)').addclass('past');  var scrollheight = $lis.eq(0).height();  function gotoslide(direction) {     currentslideposition = $lis.filter('.current').index();      if ((direction === 1 && currentslideposition === 0) || (direction === -1 && currentslideposition === count - 1)) {         return;     }      $lis.removeclass('current');     $lis.eq(currentslideposition - direction).removeclass('past prev').addclass('current');     $lis.filter('.current').prevall().addclass('prev');     $lis.filter('.current').nextall().addclass('past');     console.log($list.scrolltop())     $list.animate({         scrolltop: (direction === -1) ? $list.scrolltop()+scrollheight : $list.scrolltop()-scrollheight     }, {         duration: 600     }); }  $this.on('mouseenter', function () {     var $this = $(this);      $this.bind('mousewheel', function (e) {         if ($list.is(':animated')) {              return;         }          if (e.originalevent.wheeldelta > 0) {             direction = 1; //up         } else {                             direction = -1; //down         }          gotoslide(direction);     }); }); };  $(document).ready(function () {     $('#scroll').scroll(); }); 

http://jsfiddle.net/m5unj2wu/15/

but on every scroll background images tremble , twitch , looks ugly. there way fix it?

your scrolling competing browser's own scrolling, need tell not attempt scroll itself. can achieve event.preventdefault() in event handler:

$this.bind('mousewheel', function (e) {    e.preventdefault(); 

note though older ie versions don't natively support preventdefault(), jquery provides function you on browsers, cross-browser safe.

http://jsfiddle.net/m5unj2wu/17/


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -