打开一个链接,然后使用jQuery自动滚动到特定的div [英] Open a link and autoscroll to specific div using jQuery

查看:96
本文介绍了打开一个链接,然后使用jQuery自动滚动到特定的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,当您打开链接时,它将滚动到该页面上的特定div.

I have this code that when you open a link it will scroll to the specific div on that page.

collection.html

<a id='about' href="index.html">about</a>

index.html

<div id='#moreInfo>contents here</div>

<script>
   $(document).ready(function(){
          $('html, body').animate({
             scrollTop: $("#moreInfo").offset().top
           }, 1000);
   })
</script>

我的问题是,每当我加载 index.html 时,它总是滚动到 moreInfo div.我想要的是当我在 collection.html 上并单击关于链接时,它将重定向到 index.html ,然后滚动顺利移至 moreInfo 分区.

My problem is whenever I load the index.html, it always scrolls to the moreInfo div. What I want is when I'm on collection.html and I click on the about link, it will redirect to index.html then scroll smoothly to the moreInfo div.

我将不胜感激.

推荐答案

一种简单的方法是将链接指向位置哈希.

An easy way to do this is just to have your link point to a location hash.

<a id='about' href="index.html#moreInfo">about</a>

然后,在您的JavaScript中,您只需检查该人是否来自该链接即可.

Then, in your JavaScript you could just check if the person came from that link.

   $(document).ready(function(){
      if (window.location.hash == "#moreInfo") {
        $('html, body').animate({
           scrollTop: $("#moreInfo").offset().top
         }, 1000);
      }
   });

这篇关于打开一个链接,然后使用jQuery自动滚动到特定的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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