gpt4 book ai didi

javascript - 平滑滚动时更改 id 的颜色

转载 作者:行者123 更新时间:2023-11-28 08:14:03 26 4
gpt4 key购买 nike

我如何添加到这个平滑滚动功能来更改滚动到的 id 的颜色?

$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});

最佳答案

在字体颜色的情况下添加这个

 target.css('color', 'red');

之后

target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

或者在动画的回调函数中:

$('html,body').animate({
scrollTop: target.offset().top
}, 1000, function(){
target.css('color', 'red');
});

如果要清除其他链接,可以使用:

$('a[name]').css('color', 'black');

更新

在您的例子中,您将颜色添加到 p 标签。所以你必须改变你的选择器:

$("p[id^='faq_']").css('color', '#707070');

Demo

引用

.css()

关于javascript - 平滑滚动时更改 id 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com