获取当前滚动位置并将其作为带有链接的变量传递? [英] Get current scroll position and pass it as a variable with a link?

查看:11
本文介绍了获取当前滚动位置并将其作为带有链接的变量传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老实说,我在这方面超出了我的深度.我创建了一个分页系统,您可以在其中单击链接重新加载页面,并且一定数量的列表项显示在无序列表中.当您再次单击它时,它会重新加载页面并显示更多项目.想想 Twitter 如何在您的提要中显示推文.

I'll be honest, I'm way out of my depth here. I've created a pagination system where you click on a link it reloads the page and a certain number of list items are shown in an unordered list. When you click it again it'll reload the page and more items showing. Think how Twitter shows the tweets in your feed.

问题是当它重新加载页面时它停留在顶部.我想要的是它跳到以前页面上的相同位置.

The problem is when it reloads the page it stays at the top. What I would like is for it to jump to the same position on the page it previously had.

我已经为链接设置了一个选择器,我只是不知道如何获取当前滚动位置,然后将其传递到新页面以供跳转.

I already have a selector for the link set up fine I just don't know how to get the current scroll position and then pass it to the new page for it to jump to.

这是我目前的代码:

jQuery(document).ready(function(){
    $("a[rel=more]").live("click", function(){
        //script...
    });
});

我该去哪里?

推荐答案

  1. 使用 AJAX 重新加载项目,而不是重新加载整个页面.

  1. Reload the items using AJAX instead of reloading the whole page.

使用window.pageYOffsetwindow.scrollTo(0, y)获取和设置滚动位置.

Get and set the scroll position using window.pageYOffset and window.scrollTo(0, y).

我会将位置存储在 URL 的哈希中:

I'd store the position in the hash of the URL:

// after loading the document, scroll to the right position
// maybe location.hash has to be converted to a number first
$(document).ready(function() {
    window.scrollTo(0, location.hash);
});

// I'm not quite sure if reload() does preserve the hash tag.
location.hash = window.pageYOffset;
location.reload();

这篇关于获取当前滚动位置并将其作为带有链接的变量传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆