我如何水平滚动到页面中的下一篇文章(或#等)?

发布于 2024-11-28 21:11:02 字数 1328 浏览 0 评论 0原文

我正在尝试使用在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-12-05 21:11:02

基本上,您应该能够将所有垂直参考切换为水平参考,并且它应该可以工作。尝试这样的事情:

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 
        } 
      }); 
    }); 
});

Basically you should just be able to switch out all the vertical references to horizontal ones and it should work. Try something like this:

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 
        } 
      }); 
    }); 
});
夜未央樱花落 2024-12-05 21:11:02

您可以使用 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

黎夕旧梦 2024-12-05 21:11:02

试试这个

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 
        } 
      }); 
    }); 
});

Try this

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 
        } 
      }); 
    }); 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文