如何水平滚动到页面中的下一篇文章(或#等)
How would I scroll horizontally to the next article in a page (or #, etc.)?
我正在尝试使用我在http://marcgrabanski.com/articles/scrollto-next-article-button-jquery上找到的jQuery脚本,该脚本允许我设置"下一个"answers"上一个"按钮,这些按钮可以将用户从一篇文章滚动到下一篇(或上一篇)。这个想法是,按钮将保持在一个固定的位置,并点击按钮多次将保持用户滚动到下一篇文章(或其他元素)。
我有一个页面水平滚动,所以我想调整代码,而不是在容器中找到每个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/顶部的箭头,如果浏览器窗口足够小可以垂直滚动,则窗口将垂直滚动(以及水平滚动)。有没有办法添加滚动x(或类似的东西)的脚本,以限制滚动水平只?
谢谢,
jP
基本上你应该能够把所有的垂直引用切换到水平引用,它应该工作。试试这样做:
jQuery(function($){
$('<div id="next_arrow">Next</div>')
.prependTo("body") //append the Next arrow div to the bottom of the document
.click(function(){
scrollLeft = $(window).scrollLeft();
$('#container h2').each(function(i, h2){ // loop through article headings
h2Left = $(h2).offset().left; // get article heading left
if (scrollLeft<h2Left) { // compare if document is left of heading
$.scrollTo(h2, 800); // scroll to in .8 of a second
return false; // exit function
}
});
});
});
您可以使用offsetLeft(或offsetRight)来修改代码,而不是使用(window). scrolltop .
此链接可能有所帮助:http://unknownerror.net/2011-04/top-clienttop-scrolltop-offsettop-the-difference-between-memo-4017
试试这个
jQuery(function($){
$('<div id="next_arrow">Next</div>') //item to be added
.prependTo("body") //append the Next arrow div to the bottom of the document
.click(function(){
scrollLeft = $(window).scrollLeft();
$('#container h2').each(function(i, h2){ // loop through article headings
h2left = $(h2).offset().left; // get article heading left
if (scrollLeft < h2Left) { // compare if document is below heading
$.scrollTo(h2, 800); // scroll to in .8 of a second
return false; // exit function
}
});
});
});
- 使用ajax在多个页面上发布一篇文章
- 计算一篇文章中的节数(不止一篇文章)
- 计算一篇文章中有多少节
- 如何提供指向'下一篇文章'
- 翻译一篇文章'上.单击'PHP中的事件,来自'window.open'到基本URL链接
- 我怎么能只用Javascript(AJAX)编写其中一篇文章呢
- 单击我的joomla网站上的模块标题以指向一篇文章
- 定位页面上的第一篇文章和最后一篇文章
- jquery按钮只在一篇文章中启用
- 如何点击按钮显示下一篇文章
- 将变量数据从.js的一篇文章中抓取到php中
- 每回一篇文章,页面都会向后滚动
- 如何获取我的博客上最后一篇文章的日期,并将其显示在另一个网站上?
- 在领英上发表一篇文章
- 使用Tumblr API获取下一篇或上一篇文章
- 在展开下一篇文章时折叠文章(Readmore.js - Extension)
- 如何滚动一个大的表面?(例如一篇文章)
- Angular Typeahead:点击建议并立即发表一篇文章
- 如何水平滚动到页面中的下一篇文章(或#等)
- 如何在网页上为网页上的每一篇文章单独添加facebook分享按钮