javascript - Turning jQuery event listener on after off -


so want turn off jquery event under condition (if user scrolls down) if opposite (scroll up) want event turned on fire.

this code new jquery, pretty sure i'm missing in handler - don't know should be.

here code:

 function myfunction() {   var handler = function(e){     //code here } var position = $(window).scrolltop();  $(window).scroll(function() {     var scroll = $(window).scrolltop();     if(scroll > position) {          // scrolling downwards          $(window).off("scroll", handler);            }          if(scroll < position) {          //scrolling upwards          $(window).on("scroll", handler);           hypedocument.showpreviousscene(hypedocument.kscenetransitionpushtoptobottom, 1.1)          }     position = scroll; });    } 

$(function() {  var handler = function(e){     // hypedocument.showpreviousscene(hypedocument.kscenetransitionpushtoptobottom, 1.1)     console.log('handler'); }  var position = $(window).scrolltop();  $(window).scroll(function() {     var scroll = $(window).scrolltop();     if (scroll > position) {          // scrolling downwards          // nothing          }          if(scroll < position) {          //scrolling upwards          handler();          }     position = scroll; });  }); 

open console , you'll see "handler" in output each time scroll up.

  1. you need add scroll handler after document.ready event:

     $(function() {       /* code here */  }); 
  2. i think doing wrong trying subscribe , unsubscribe handler. should determine direction of scroll do. if right direction – call handler:

    if (scroll < position) {      //scrolling upwards      handler(); } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -