【问题标题】:How can I scroll the page to an element after being re-directed from another page? [closed]从另一个页面重定向后,如何将页面滚动到某个元素? [关闭]
【发布时间】:2013-05-26 07:05:21
【问题描述】:
<script>
    $(document).ready(function () {
        $("#button").click(function () {
            window.location.href = "page2.aspx"; 

            $('html, body').animate({ 
                scrollToElement ("#div").offset().top 
            }, 2000); 
        }); 
    });
</script>

这个想法是,您单击 page1 上的按钮,然后重定向到 page2,然后使用 jQuery 滚动到特定元素。

【问题讨论】:

  • 请发布任何现有代码You have tried so far 并指出导致您出现问题的特定代码。
  • 嗯,到目前为止我已经尝试了很多,但没有一个是真正有效的。我得到的最多的是它只是重定向到page2。
  • 感谢您的宝贵时间和帮助!

标签: jquery redirect


【解决方案1】:

你总是可以为那个元素设置一个锚点,就像这样

<a name="scroll"></a>
<h2>I wanna scroll here</h2>

并链接到它:http://mydomain.com/index.php#scroll

使用 jQuery 来完成这项任务的唯一好处是为滚动本身设置动画,在这种情况下,您可以执行以下操作:

<h2 id="scrollhere">I wanna scroll here</h2>

链接:http://mydomain.com/index.php#scrollhere

然后在jQuery中重定向页面:

$(document).ready(function() {
    if (window.location.hash != null && window.location.hash != '') 
        $('body').animate({
            scrollTop: $(window.location.hash).offset().top
        }, 1500);
});

【讨论】:

  • 我不是很喜欢 jQuery,所以如果你能给我更多的帮助,并指定这个“链接在这里”部分,我将非常感激
  • 像往常一样链接。 # 之外的任何内容都称为hash。如果有同名的a 标签,它会自动立即滚动到它。 jQuery 解决方案是相同的想法,只是它会对其进行动画处理(参见jQuery.animatejQuery.scrollTop)。 1500 是以毫秒为单位进行滚动所需的时间,因此为 1.5 秒(使用您喜欢的任何内容)。开头的 if 是为了确保哈希不为空,以免在滚动时出错,因为它会尝试滚动到任何内容。
  • 此处链接:(链接)表示正常链接:&lt;a href="http://mydomain.com/index.php#scrollhere"&gt;My link&lt;/a&gt;
  • 谢谢你,效果很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
相关资源
最近更新 更多