gpt4 book ai didi

java - 滚动条上的 Jquery 下划线

转载 作者:行者123 更新时间:2023-11-29 05:38:16 25 4
gpt4 key购买 nike

我想尝试在一个非常酷的网站上找到的功能,但我不知道从哪里开始。

http://adamrudzki.com/

该功能是在页面向下滚动时移动的下划线元素。我在这里找到了类似的 SO Underlining menu item但如果有人可以帮助我在 id 之后提供功能,我将不胜感激。还不太熟悉 Jquery。

提前致谢!

最佳答案

在您的示例网站上每个 <a>标签有一个 <span>作为下划线的元素。但我在想也许我们可以切断标记并使用 border-bottom反而。基本上这里有两个事件 - scroll()click() .

这是基本的标记:

<nav> 
<a>Home</a>
<a>About</a>
<a>Portfolio</a>
<a>Contact</a>
</nav>
<div id="contents">
<section>Home</section>
<section>About</section>
<section>Portfolio</section>
<section>Contact</section>
</div>

CSS,只是想强调边框:

a {
border:0 solid #FFF;
border-bottom-width:0;
}

jQuery scroll()事件:

$(window).scroll(function () {
//get the window scrollTop on scroll
var st = $(window).scrollTop();
/* we use each() to iterate on every section and
check if the offset position is in relative condition to the
scrollTop value
*/
$('#contents section').each(function (index) {
var offsetTop = $(this).offset().top,
h = $(this).height();
//this condition satisfies that this section is currently on the viewport
if (st >= offsetTop && st < offsetTop + h) {
/*find the nav <a> that has the same index to this section
currently on the viewport and
show its border-bottom by setting its width.
*/
$('nav a').eq(index).css({
'border-bottom-width': '3px'
});
} else {
//hide the border-bottom
$('nav a').eq(index).css({
'border-bottom-width': '0'
});
}
});
}).trigger('scroll');

导航 <a> click()事件:

$('nav a').click(function () {
/* click has no index argument compared to each() function
so we have to get it with index() */
var index = $(this).index(),
$target = $('#contents section').eq(index); // find the target section
//animate scrolling to the target section
$('html, body').stop(true, true).animate({
scrollTop: $target.offset().top
}, 'slow');
});

请注意,我们使用的是 index准确定位 <section>/<a> ,如果 <section>,此解决方案将正常工作根据导航排列 <a>排列位置。

查看此示例 jsfiddle .

关于java - 滚动条上的 Jquery 下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677238/

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