jQuery 滚动到链接元素

发布于 2024-11-04 17:38:04 字数 223 浏览 0 评论 0原文

我正在尝试实现一些 jQuery 来滚动到一个链接,该链接在页面加载时有一个名为“selected-node”的类,但我没有运气让它工作。这是我的代码目前的样子:

$(document).ready(function () {
    $('a.selected-node').scrollTo();
});

我不是 jQuery 专家,所以你知道我做错了什么吗?

I'm trying to implement some jQuery to scroll to a link which has a class called "selected-node" on page load, but I haven't had any luck getting it to work. Here is what my code currently looks like:

$(document).ready(function () {
    $('a.selected-node').scrollTo();
});

I'm not a jQuery expert, so any insight into what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千と千尋 2024-11-11 17:38:04

假设您使用此插件 scrollTo 插件页面

鉴于此,您需要更改说明以说明滚动量:

 $.scrollTo($('a.selected-node') );//scroll to target

如果您需要在页面上使用插件的帮助,只需在页面上添加指向该插件(放置它的位置)的链接,位于您放置 jQuery 链接的位置下方图书馆。

Making the assumption you use this plugin scrollTo plugin page

Given that, you need to change instruction to say what amount to scroll:

 $.scrollTo($('a.selected-node') );//scroll to target

If you need help with using plug-in's on a page, simply add a link to that plugin (where you put it) on your page BELOW where you put your link to the jQuery library.

痴情换悲伤 2024-11-11 17:38:04

我认为您正在使用用于插件的代码。您是否正确地将插件代码添加到页面中?

这是您找到代码的地方吗?
http://flesler.blogspot.com/2007/10/jqueryscrollto.html

I think you're using code which is intended for a plugin. Did you properly add the plugin code to the page?

Is this where you found the code?
http://flesler.blogspot.com/2007/10/jqueryscrollto.html

烟柳画桥 2024-11-11 17:38:04
/*
    Anchor Slider by Cedric Dugas   
    Http://www.position-absolute.com 
*/
var locationHref = window.location.href,
    elementClick = $(this).children('a').attr("href"),
    destination = $(elementClick).offset().top;

$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100, function() {
    window.location.hash = elementClick
});

应该有效。我在我的页面上使用它 http://jasonrb.com

/*
    Anchor Slider by Cedric Dugas   
    Http://www.position-absolute.com 
*/
var locationHref = window.location.href,
    elementClick = $(this).children('a').attr("href"),
    destination = $(elementClick).offset().top;

$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100, function() {
    window.location.hash = elementClick
});

Should work. I use it on my page http://jasonrb.com

简单 2024-11-11 17:38:04

如果您使用 jQuery scrollTo 插件,请尝试:

$(body).scrollTo( $('a.selected-node'), 1000 )

if you're using the jQuery scrollTo plugin, then try:

$(body).scrollTo( $('a.selected-node'), 1000 )
携君以终年 2024-11-11 17:38:04

有一个 jQuery 插件完全可以满足您的需求,

我写了一个 jQuery 插件,动画滚动到视图之外的元素。

您可以简单地使用它,就像您最初想要做的那样:

$(document).ready(function () {
    $('a.selected-node').scrollintoview();
});

这个插件与scrollTo插件有何不同?

我的插件和 scrollTo 插件之间的区别在于,对于我的插件,您不必提供可滚动元素(您必须使用 .scrollTo() 插件提供) )。您只需提供您想要查看的元素,然后对其调用 scrollintoview() 。插件将自动找到最近的可滚动祖先,并滚动尚未在视图中的元素。它将通过动画来完成此操作(您可以控制其速度),最后它还可以调用您的 complete 函数。

如果您的元素位于视图下方,它将滚动到底部,如果元素位于视图上方,它将滚动到顶部。尽可能的最近的地方。通过随后突出显示它,可以轻松地在页面/元素滚动时为用户提供焦点。

检查我的博客文章,您可以在其中将找到最新版本的 .scrollintoview() 插件,其中包含您可能感兴趣的所有详细信息。

您应该使用动画滚动吗?

根据页面进程的不同,您可能根本无法使用动画滚动。也许您应该跳转到特定元素。在这种情况下,您的代码将大大简化,因为您可以使用内置功能:

$(document).ready(function () {
    $('a.selected-node').get(0).scrollIntoView();
});

这只会跳转到您的链接。所有浏览器都支持此 DOM 功能,并且已经支持很长时间了

There's a jQuery plugin exactly for your needs

I've written a jQuery plugin that animates scrolling to an out of view element.

You can simply use it similar to what you wanted to do in the first place:

$(document).ready(function () {
    $('a.selected-node').scrollintoview();
});

How does this plugin differ from scrollTo plugin?

The difference between my plugin and scrollTo plugin is that in case of my plugin you don't have to provide scrollable element (which you have to provide with .scrollTo() plugin). You only provide the element you want in view and then call scrollintoview() on it. Plugin will automatically find the closest scrollable ancestor and scroll it of element is not already in view. It will do this with animation (you can control its speed) and in the end it can also call your complete function.

If your element is below the view it will be scrolled to the bottom, if it's above it will be scrolled to the top. The nearest place possible. By highlighting it afterwards it makes it easy to give your users a point of focus when the page/element scrolls.

Check my blog post, where you will find the latest version of the .scrollintoview() plugin with all details you may be interested.

Should you use animated scrolling at all?

Depending on the page process it may not be feasible for your to use animated scrolling at all. Maybe you should rather just jump to a particular element. In that case your code will simplify big time because you can use built-in functionality:

$(document).ready(function () {
    $('a.selected-node').get(0).scrollIntoView();
});

This will just jump to your link. And this DOM function is supported in all browsers and has been for a long time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文