我如何水平滚动到页面中的下一篇文章(或#等)?
我正在尝试使用在 http://marcgrabanski 找到的 jQuery 脚本。 com/articles/scrollto-next-article-button-jquery 允许我设置“下一篇”和“上一篇”按钮,使用户在页面中从一篇文章滚动到下一篇(或上一篇)。这个想法是,按钮将保持在固定位置,多次单击按钮将使用户滚动到下一篇文章(或其他元素)。
我有一个水平滚动的页面,所以我想调整代码,以便它不是找到容器中每个 h2 的顶部,而是找到每个 h2 的左侧并水平而不是垂直滚动用户。这是我正在使用的代码:
jQuery(function($){
$('<div id="next_arrow">Next</div>')
.prependTo("body") //append the Next arrow div to the bottom of the document
.click(function(){
scrollTop = $(window).scrollTop();
$('#container h2').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
}
});
});
});
提前非常感谢任何帮助。谢谢。
jP
@paulGraffix、@ShankarSangoli 和 @esses 感谢您的回复。
作为我上一个问题的后续问题,我将如何限制滚动仅水平滚动?
如果您单击 http://174.121.134.126/~methfree/ 窗口顶部的箭头如果浏览器窗口足够小,可以垂直滚动,则将垂直(以及水平)滚动。有什么方法可以在脚本中添加scroll-x(或类似的东西)以将滚动限制为仅水平滚动?
谢谢,
jP
I'm trying to work with a jQuery script I found at http://marcgrabanski.com/articles/scrollto-next-article-button-jquery that allows me to set up "next" and "previous" buttons that scroll the user through a page from one article to the next (or previous). The idea is that the button would stay in a fixed position and clicking the button multiple times would keep the user scrolling on to the next article (or other element).
I have a page that scrolls horizontally, so I want to adapt the code so that instead of finding the top of each h2 in the container, it finds the left side of each h2 and scrolls the user horizontally and not vertically. Here is the code I'm using:
jQuery(function($){
$('<div id="next_arrow">Next</div>')
.prependTo("body") //append the Next arrow div to the bottom of the document
.click(function(){
scrollTop = $(window).scrollTop();
$('#container h2').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
}
});
});
});
Any help is greatly appreciated in advance. Thank you.
jP
@paulGraffix, @ShankarSangoli, and @esses thank you for your responses.
As a follow-up to my last question, how would I go about limiting the scroll to scroll horizontally only?
If you click on the arrows at the top of http://174.121.134.126/~methfree/ the window will scroll vertically (as well as horizontally) if the browser window is small enough to scroll vertically. Is there any way to add a scroll-x (or something like that) to the script to limit the scroll to horizontal only?
Thanks,
jP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,您应该能够将所有垂直参考切换为水平参考,并且它应该可以工作。尝试这样的事情:
Basically you should just be able to switch out all the vertical references to horizontal ones and it should work. Try something like this:
您可以使用 offsetLeft (或 offsetRight)而不是使用 (window).scrollTop 来修改代码。
此链接可能会有所帮助: http:// /unknownerror.net/2011-04/top-clienttop-scrolltop-offsettop-the-difference- Between-memo-4017
you may be able to modify your code using offsetLeft (or offsetRight) instead of using (window).scrollTop.
this link may be helpful: http://unknownerror.net/2011-04/top-clienttop-scrolltop-offsettop-the-difference-between-memo-4017
试试这个
Try this