使用带有jQuery scrollTo的箭头键

Ted*_*Ted 3 html javascript jquery scrollto arrow-keys

我已经成功实现了scrollTo jQuery插件,当单击一个链接时,该插件会滚动到下一个div,类为"new".但是,我还希望能够使用箭头键向上和向下滚动到同一个类的下一个/上一个div.

我已经浏览了整个互联网,但一直无法找到如何做到这一点.我是JS的新手,所以非常简单的说明将不胜感激!

这是相关代码:

<script type="text/javascript">
jQuery(function($){

 $('<div id="next_arrow"></div>') 
  .prependTo("body") //append the Next arrow div to the bottom of the document
  .click(function(){ 
   scrollTop = $(window).scrollTop();
   $('.new').each(function(i, h2){ // loop through article headings
    h2top = $(h2).offset().top; // get article heading top
    if (scrollTop < h2top) { // compare if document is below heading
     $.scrollTo(h2, 800); // scroll to in .8 of a second
     return false; // exit function
    }
   });
  });

});
</script>
Run Code Online (Sandbox Code Playgroud)

为了使箭头键有效,我需要添加什么?

谢谢,特德

dav*_*ell 7

您可以使用keydown事件侦听器来侦听按键.您可以在&lt;input&gt;字段等上使用它.因为keydown事件会冒泡 DOM,所以您可以在document对象上使用它来捕获页面上的任何按键:

$(function () {
  $(document).keydown(function (evt) {
    alert("Key pressed: " + evt.keyCode);
  });
});
Run Code Online (Sandbox Code Playgroud)

每个按键都有一个代码.如果您在网页中使用上面的代码,您将看到向下箭头的键代码为40.您可以使用处理程序中的ifswitch语句来解决此问题:

jQuery(function () {

  $(document).keydown(function (evt) {
    if (evt.keyCode == 40) { // down arrow
      alert("You pressed down.");
    }
  });

});
Run Code Online (Sandbox Code Playgroud)

现在,您需要绑定实际跳转到下一个标题的代码.我建议将代码抽象为函数,以便将其用于按键和单击.这是函数,以及使用它的原始代码的变体:

// Here is the function:

function scrollToNew () {
  scrollTop = $(window).scrollTop();
  $('.new').each(function(i, h2){ // loop through article headings
    h2top = $(h2).offset().top; // get article heading top
    if (scrollTop < h2top) { // compare if document is below heading
      $.scrollTo(h2, 800); // scroll to in .8 of a second
      return false; // exit function
    }
  });
}

// Here is your original code, modified to use the function:

jQuery(function () {

  $("#next").click(scrollToNew);

});
Run Code Online (Sandbox Code Playgroud)

最后,您可以添加按键代码并从那里调用函数:

function scrollToNew () {
  scrollTop = $(window).scrollTop();
  $('.new').each(function(i, h2){ // loop through article headings
    h2top = $(h2).offset().top; // get article heading top
    if (scrollTop < h2top) { // compare if document is below heading
      $.scrollTo(h2, 800); // scroll to in .8 of a second
      return false; // exit function
    }
  });
}

jQuery(function () {

  $("#next").click(scrollToNew);

  $(document).keydown(function (evt) {
    if (evt.keyCode == 40) { // down arrow
      evt.preventDefault(); // prevents the usual scrolling behaviour
      scrollToNew(); // scroll to the next new heading instead
    }
  });

});
Run Code Online (Sandbox Code Playgroud)

更新:要向上滚动,请执行两项操作.将keydown处理程序更改为:

  $(document).keydown(function (evt) {
    if (evt.keyCode == 40) { // down arrow
      evt.preventDefault(); // prevents the usual scrolling behaviour
      scrollToNew(); // scroll to the next new heading instead
    } else if (evt.keyCode == 38) { // up arrow
      evt.preventDefault();
      scrollToLast();
    }
  }
Run Code Online (Sandbox Code Playgroud)

并编写一个scrollToLast()基于scrollToNew()它的函数来查找页面上没有的最后一个新标题:

function scrollToLast () {
  scrollTop = $(window).scrollTop();

  var scrollToThis = null;

  // Find the last element with class 'new' that isn't on-screen:
  $('.new').each(function(i, h2) {
    h2top = $(h2).offset().top;
    if (scrollTop > h2top) {
      // This one's not on-screen - make a note and keep going:
      scrollToThis = h2;
    } else {
      // This one's on-screen - the last one is the one we want:
      return false;
    }
  });

  // If we found an element in the loop above, scroll to it:
  if(scrollToThis != null) {
    $.scrollTo(scrollToThis, 800);
  }
}
Run Code Online (Sandbox Code Playgroud)