javascript - Jquery Scrolling and Previous and Next Div Buttons -
so i'm trying have previous , next buttons on top of div, , when click on them takes me next , previous div it's supposed to.
here's code:
$('button.navbutton').on('click', function(e) { if ($(this).hasclass('next') && $('.selected').next('div.day').length > 0 ) { var $next = $('.selected').next('.day'); var top = $next.offset().top; $('.selected').removeclass('selected'); $('body').animate({ scrolltop: top }, function () { $next.addclass('selected'); }); } else if ($(this).hasclass('prev') && $('.selected').prev('div.day').length > 0 ) { var $prev = $('.selected').prev('.day'); var top = $prev.offset().top; $('.selected').removeclass('selected'); $('body').animate({ scrolltop: top }, function () { $prev.addclass('selected'); }); } });
it works, except if reach div scrolling , not buttons, doesn't. know because div not on view still has "selected" class, tried function:
$('div.day').each(function(){ if($(this).offset().top < winheight/2 && !$(this).hasclass('selected') ){ $('.selected').removeclass('selected'); $(this).addclass('selected'); } }
this not not work makes previous code not work either.
i'm new javascript can't figure out why makes other code stop working, , how can make div being viewed "selected" one? (kinda 9gag j k shortcuts).
thanks lot , sorry using time.
Comments
Post a Comment