页面加载更改为锚定 [英] on page load change to anchor

查看:73
本文介绍了页面加载更改为锚定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jquery.Localscroll在锚点之间平滑移动窗口.页面加载时如何更改URL以包含锚链接,以便在页面加载时平滑滚动到某个部分.

I'm using Jquery.Localscroll to move the window smoothly between anchors. How would you change the URL when the page loads to include an anchor link so it scrolls smoothly to a section when the page loads.

或者在加载文档时是否有更好的方式滚动到某个部分.

Or is there a better way to scroll to a section when the document loads.

谢谢.

推荐答案

很明显,如果您指定以传统方式滚动到的锚点:

Obviously if you specify the anchor to scroll to in the tradional way:

<a href="yourURL#someanchor">Link</a>

然后浏览器将自动滚动.

Then the browser will scroll automatically.

如果您打算让浏览器加载页面,然后启动代码以平滑(动画)滚动到特定锚点,则可以执行以下操作:

If your intention is for the browser to load the page and then have your code kick in to do a smooth (animated) scroll to a particular anchor then something like this would work:

// on calling page
<a href="yourURL?scrollto=someanchor">Link</a>

// on "yourURL" page:
$(document).ready(function() {
   // check for "scrollto" parameter and if it exists
   // use Localscroll to move to specified anchor
   var match = /[?&]scrollto(?:=([^&]*))?/.exec(window.location.search);
   if( match != null ){
      var anchor = match[1];
      // your code to scroll to anchor here
   }
});

这篇关于页面加载更改为锚定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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