向 jQuery scrollTop 添加箭头键导航
我正在使用 jQuery scrollTop 来浏览页面上的点。效果很好。但我想知道是否可以为其添加上/下箭头键导航。例如,向下箭头滚动到下一点、下一点等。向上箭头返回一个点,等等。非常感谢对此的任何帮助!
HTML:
<a class="bookmark" href="#option1">Option 1</a>
<a class="bookmark" href="#option2">Option 2</a>
<a class="bookmark" href="#option3">Option 3</a>
<div id="option1">Stuff</div>
<div id="option2">Stuff</div>
<div id="option3">Stuff</div>
jQuery
$( '.bookmark' ).click(function() {
var elementClicked = $(this).attr( "href" );
var destination = $(elementClicked).offset().top;
$("html:not(:animated),body:not(:animated)")
.delay( 300 ).animate({ scrollTop: destination-20}, 700 );
return false;
});
I'm using jQuery scrollTop to navigate through points on a page. Works great. But I was wondering if it would be possible to add up/down arrow key navigation to it. So for instance, down arrow scrolls to the next point, next point, etc. Up arrow goes back one point, etc, etc. Any help on this one is greatly appreciated!
HTML:
<a class="bookmark" href="#option1">Option 1</a>
<a class="bookmark" href="#option2">Option 2</a>
<a class="bookmark" href="#option3">Option 3</a>
<div id="option1">Stuff</div>
<div id="option2">Stuff</div>
<div id="option3">Stuff</div>
jQuery
$( '.bookmark' ).click(function() {
var elementClicked = $(this).attr( "href" );
var destination = $(elementClicked).offset().top;
$("html:not(:animated),body:not(:animated)")
.delay( 300 ).animate({ scrollTop: destination-20}, 700 );
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西应该就是您正在寻找的。
可以轻松添加一些内容来确定您在移动之前位于页面的哪个部分,以防用户使用鼠标滚动!
Something like this should be what you're looking for.
Could easily add something that determines what section of the page you're on before moving incase the user scrolls with the mouse!
不完全是您问题的答案,但是 jscrollpane 内置了此功能,如果您想尝试使用它。
在初始化中,您只需输入 { showArrows: true; } 就完成了。
Not exactly an answer to your question, but jscrollpane has this feature built in, if you wanted to try using that.
In the initialization you just enter { showArrows: true; } and done.