如何使用 jQuery 将页面向上或向下滚动到锚点?
我正在寻找一种方法,当您单击页面上方或下方的本地锚点链接时包含幻灯片效果。
我想要这样的链接:
<a href="#nameofdivetc">link text, img etc.</a>
也许添加了一个类,这样您就知道您希望此链接成为滑动链接:
<a href="#nameofdivetc" class="sliding-link">link text, img etc.</a>
然后,如果单击此链接,页面会向上或向下滑动到所需的位置(可以可以是 div、标题、页面顶部等)。
这是我之前的情况:
$(document).ready(function(){
$(".scroll").click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 1500, 'easeInSine');
});
});
I'm looking for a way to include a slide effect for when you click a link to a local anchor either up or down the page.
I'd like something where you have a link like so:
<a href="#nameofdivetc">link text, img etc.</a>
perhaps with a class added so you know you want this link to be a sliding link:
<a href="#nameofdivetc" class="sliding-link">link text, img etc.</a>
Then if this link is clicked, the page slides up or down to the required place (could be a div, heading, top of page etc).
This is what I had previously:
$(document).ready(function(){
$(".scroll").click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 1500, 'easeInSine');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
描述
您可以使用
jQuery.offset()
和jQuery.animate()
来完成此操作。查看 jsFiddle 演示。
示例
更多信息
Description
You can do this using
jQuery.offset()
andjQuery.animate()
.Check out the jsFiddle Demonstration.
Sample
More Information
假设您的 href 属性链接到带有同名标签 id 的 div(像往常一样),您可以使用以下代码:
HTML
JAVASCRIPT - (Jquery)
Assuming that your href attribute is linking to a div with the tag id with the same name (as usual), you can use this code:
HTML
JAVASCRIPT - (Jquery)
这让我的生活变得更加轻松。您基本上将元素 id 标记及其滚动条放入其中,无需大量代码
http://balupton。 github.io/jquery-scrollto/
在 Javascript 中
在你的 html 中
我一直在页面下方
This made my life so much easier. You basically put in your elements id tag and its scrolls to it without a lot of code
http://balupton.github.io/jquery-scrollto/
In Javascript
In your html
Here I am all the way down the page
您还应该考虑到目标有填充,因此使用
position
而不是offset
。您还可以考虑您不希望与目标重叠的潜在导航栏。You should also consider that the target has a padding and thus use
position
instead ofoffset
. You can also account for a potential nav bar you don't want to be overlapping the target.我使用 jQuery 的方法是让所有嵌入的锚链接滑动而不是立即跳转
这与 Santi Nunez 但它更可靠。
支持
My approach with jQuery to just make all of the embedded anchor links slide instead of jump instantly
It's really similar to the answer by Santi Nunez but it's more reliable.
Support
这是对我有用的解决方案。这是一个通用函数,适用于所有引用命名
a
的a
标记。注意 1:确保使用双引号
"
如果您使用单引号,请将上面的代码部分更改为var target = $("a[name='" + name.substring(1) + "']");
注意 2:在某些情况下,特别是当您使用来自在引导程序中,名为的
a
将隐藏在导航栏下方。在这些情况下(或任何类似的情况),您可以减少滚动的像素数以达到最佳位置,例如:$('html,body').animate({scrollTop: $(target).offset().top - 15 }, 'slow');
将带您到达target< /code> 还剩 15 个像素 顶部。
Here is the solution that worked for me. This is a generic function which works for all of the
a
tags referring to a nameda
Note 1: Make sure that you use double quotes
"
in your html. If you use single quotes, change the above part of the code tovar target = $("a[name='" + name.substring(1) + "']");
Note 2: In some cases, especially when you use the sticky bar from the bootstrap, the named
a
will hide beneath the navigation bar. In those cases (or any similar case), you can reduce the number of the pixels from your scroll to achieve the optimal location. For example:$('html,body').animate({ scrollTop: $(target).offset().top - 15 }, 'slow');
will take you to thetarget
with 15 pixels left on the top.我坚持使用我的原始代码,并且还使用此代码在“返回顶部”链接中加入了淡入淡出,也从这里开始:
http://webdesignerwall.com/tutorials/animated-scroll-to-top
效果很好:)
I stuck with my original code and also included a fade in 'back-to-top' link making use of this code and a bit from here too:
http://webdesignerwall.com/tutorials/animated-scroll-to-top
Works well :)
好的,最简单的方法是: -
在点击功能(Jquery)中: -
HTML
Ok simplest method is : -
Within the click function (Jquery) : -
HTML
您可能需要添加 offsetTop 和 scrollTop 值,以防您不是为整个页面设置动画,而是为一些嵌套内容设置动画。
例如:
You may want to add offsetTop and scrollTop value in case You are animating not the whole page , but rather some nested content.
e.g :
SS Slow Scroll
此解决方案不需要锚标记,但您当然需要将菜单按钮(任意属性,示例中的“ss”)与 html 中的目标元素 id 相匹配。
ss="about"
带您前往id="about"
小提琴
https://jsfiddle.net/Hastig/stcstmph/4/
SS Slow Scroll
This solution does not require anchor tags but you do of course need to match the menu button (arbitrary attribute, 'ss' in example) with the destination element id in your html.
ss="about"
takes you toid="about"
Fiddle
https://jsfiddle.net/Hastig/stcstmph/4/
我在 https://css-tricks.com/snippets/jquery 上偶然发现了这个示例/smooth-scrolling/ 解释每一行代码。我发现这是最好的选择。
https://css-tricks.com/snippets/jquery/smooth-scrolling/< /a>
你可以使用原生:
或使用 jquery:
I stumbled upon this example on https://css-tricks.com/snippets/jquery/smooth-scrolling/ explaining every line of code. I found this to be the best option.
https://css-tricks.com/snippets/jquery/smooth-scrolling/
You can go native:
or with jquery:
在这里测试一下:
http://jsbin.com/ucati4
Test it here:
http://jsbin.com/ucati4
以下解决方案对我有用:
following solution worked for me: