kswedberg jquery 平滑滚动插件 - 突出显示链接并删除滚动上的突出显示?

发布于 2024-10-16 19:42:37 字数 1352 浏览 1 评论 0原文

我使用 Karl Swedberg 的 jquery 平滑滚动插件来实现同一页面链接: https://github.com/kswedberg/jquery-smooth-scroll

这些链接有一个固定位置,因此它们始终可见。我希望单击链接后突出显示链接,但在滚动页面时删除突出显示(因为您可能不再位于链接对应的部分)。

让链接在点击时改变颜色很简单。但是,由于插件本身会滚动,因此在滚动时删除颜色会更加困难。我尝试过这段代码:

$(document).ready(function() {
$('#scrollcontrolls a').smoothScroll(
    {
    afterScroll: function() { 
        $(window).scroll(function () { 
            $("#scrollcontrolls a").css("color", "black");
        });

    }
});
});

$(document).ready(function() {
$('#scrollcontrolls a').click(function() {
    $(this).css('color','red');
});
});

问题是,一旦初始化了窗口滚动函数,每当平滑滚动插件滚动页面时,它就会触发。这意味着在您单击一个链接后,它将始终覆盖单击时应用于该链接的颜色,因此单击其他链接不会更改其颜色。

谢谢

编辑-我制作了一个与下面的代码兼容的版本:

 $(window).bind('scroll', function () { 
$("#scrollcontrolls a").css("color", "black");
 });

 $(document).ready(function() {
$('#scrollcontrolls a').smoothScroll(
    {
    afterScroll: function() { 
        $(window).unbind();
        $(this).css('color','red');
        $(window).bind('scroll', function () { 
            $("#scrollcontrolls a").css("color", "black");
        });
    }
});
 });

但是有时单击链接不会改变其颜色,但再次单击它会改变它的颜色。我认为 Scroll 之后触发的不同函数并不总是按顺序运行。如果这是问题所在,我该如何让他们这样做?

谢谢

Im using Karl Swedberg's jquery smooth scrolling plugin for same page links:
https://github.com/kswedberg/jquery-smooth-scroll

The links have a fixed position so they are always visible. I want the links to be highlighted once you've clicked on them, but for the highlight to be removed when you scroll the page (as you may no longer be on the section the link corresponds to).

Its simple to have the links change color on click. However, removing the color when you scroll is harder as the plugin itself scrolls. Ive tried with this code:

$(document).ready(function() {
$('#scrollcontrolls a').smoothScroll(
    {
    afterScroll: function() { 
        $(window).scroll(function () { 
            $("#scrollcontrolls a").css("color", "black");
        });

    }
});
});

$(document).ready(function() {
$('#scrollcontrolls a').click(function() {
    $(this).css('color','red');
});
});

The problem is that once the window scroll function is initialized, it then fires whenever the smooth scroll plugin scrolls the page. This means after you've clicked on one link, it will then always override the color applied to the link on click, so clicking other links doesn't change their color.

Thanks

EDIT- Ive made a version that sort of works with the code below:

 $(window).bind('scroll', function () { 
$("#scrollcontrolls a").css("color", "black");
 });

 $(document).ready(function() {
$('#scrollcontrolls a').smoothScroll(
    {
    afterScroll: function() { 
        $(window).unbind();
        $(this).css('color','red');
        $(window).bind('scroll', function () { 
            $("#scrollcontrolls a").css("color", "black");
        });
    }
});
 });

However sometimes clicking on the link doesn't change its color, but clicking on it again will. I think the different functions fired afterScroll are not always run sequentially. If this is the problem, how can I make them do so?

Thanks

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

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

发布评论

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

评论(1

月下客 2024-10-23 19:42:37

这是一个老问题,但我认为提到 jQuery Waypoints 可能会很有用,它正是您所追求的(并且显然是基于 jQuery 的)。非常有用的插件,你可以在这里找到它。

It's an old question, but thought it might be useful to mention jQuery Waypoints, which does exactly what you're after (and is jQuery based, obviously). Very useful plugin, you can find it here.

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