Scrolling down to next element via keypress & scrollTo plugin - jQuery

那年仲夏 提交于 2019-12-04 16:39:33

There's no real question here but I'm going to guess the problem is that your page doesn't scroll past the first one. It's because you call this on every keypress:

            n = $('.screen-wrapper').next()

It's probably returning the same one every single time you call it. Try moving the instantiation outside of the keypress.

          var s = $('.screen');
          var curr=-1, node;

          $(document).keypress(function(e){
             switch( e.keyCode ) {
               case 40:
                  node = s[++curr];
                  if (node) {
                    $.scrollTo( node,800);
                  }
                  else {
                    curr = s.length-1;
                  }
                  break;
                case 38:
                   node = s[--curr];
                   if (node) {
                     $.scrollTo( node ,800);
                   }
                   else {
                      curr = 0;
                    }
                   break;
                }
          });

I just implemented something similar and I used id attributes for each element.

Why not do something like

window.location.href = '#section-' + curr;

instead of using scrollTo?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!