基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码

需求:

需要从页面A跳转到页面B的指定位置;

具体思路:

第一、需要在B页面拿到A页面要跳转的具体位置;

第二、使用jquery的animate方法传入scrollTop进行丝滑的滚动;

具体实现:

在这里自己使用的是判断当前序号02距离文档顶部的距离,然后模拟滚动到此,其实现的原理是一样的,具体代码实现如下所示:

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script>
  <style type="text/css">
    html,
    body {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      height: 100%;
    }

    body {
      display: flex;
      flex-direction: column;
      text-align: center;
    }

    .gd-flex {
      display: flex;
    }

    .gd-flex-v {
      display: flex;
      flex-direction: column;
    }

    .title {
      width: 100%;
      line-height: 32px;
      margin: 16px 0px;
      font-size: 24px;
      font-weight: 700;
    }

    .content {
      flex: 1;
      width: 200px;
      margin: 0 auto;
      border: 1px solid black;
      overflow-y: auto;
    }

    .color-red {
      color: red;
    }
  </style>
</head>

<body>
  <div class="title">这是一个标题</div>
  <div class="content">
    <div class="gd-flex">
      <p class="color-red">01</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">02</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">03</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">04</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">05</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">06</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">07</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">08</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
    <div class="gd-flex">
      <p class="color-red">09</p>
      <p class="p">This is some text. This is some text. This is some text. This is some text. This is some text. This
        is some text.
      </p>
    </div>
  </div>

  <script>
    $(document).ready(function () {
      $('.color-red').each(function (index, item) {
        var text = $(item).text();
        if (text == 02) { 
          // 获取匹配元素在当前视口的相对偏移
          var scrollTop = $(item).offset().top - 150;
          console.log('scrollTop', scrollTop);
          $('.content').animate({
            scrollTop: scrollTop
          }, 500)
        }
      })
    });
  </script>
</body>

</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星繁~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值