[英]One page menu on scroll with underline sliding links as active
I have a one page scroll menu with an underline sliding in from left to right when hovering a link. 我有一个单页滚动菜单,当悬停链接时,下划线从左到右滑动。 I need help on how to make a link active (meaning the line full) when in specific section such as "About".
在诸如“关于”之类的特定部分中,我需要有关如何使链接处于活动状态(意味着线路已满)的帮助。
Heres a picture that might help to show what I'm talking about (notice the link "Run" being highlighted): 这是一张可能有助于显示我在说什么的图片(请注意,突出显示了“运行”链接):
I've seen many examples of a link being active online but its very confusing cause I haven't found one with my specific problem. 我已经看到了很多在线链接处于活动状态的示例,但是其非常令人困惑的原因是我没有发现与我的特定问题有关的链接。
The closest example I found online was this http://jsfiddle.net/mdesdev/zkrh7/ but i still can't figure out how to fix the underline active. 我在网上找到的最接近的示例是此http://jsfiddle.net/mdesdev/zkrh7/,但我仍然不知道如何修复下划线。
I have created a js fiddle to show what I have so far: https://jsfiddle.net/rsrsrs/36rtdboh/ 我创建了一个js小提琴来显示到目前为止的内容: https : //jsfiddle.net/rsrsrs/36rtdboh/
HTML: HTML:
<div id="nav_Wrapper_dk">
<nav id="dk_Nav" role="navigation" class="cf">
<div><a href="#home" class="link_Button sliding-u-l-r scroll">Home</a> </div>
<div><a href="#about" class="link_Button sliding-u-l-r scroll">About us</a></div>
<div><a href="#link3" class="link_Button sliding-u-l-r">Gallery</a></div>
<div><a href="#link4" class="link_Button sliding-u-l-r">Find Us</a></div>
<div><a href="#link5" class="link_Button sliding-u-l-r">Contact</a></div>
<div><a href="#link6" class="link_Button sliding-u-l-r">Catering</a></div>
<div><a href="#link7" class="link_Button sliding-u-l-r">Blog</a></div>
</nav>
</div>
<div id="home"></div>
<div id="about"></div>
CSS: CSS:
#nav_Wrapper_dk {
position: fixed;
width: 100%;
height: 50px;
background: white;
border-bottom: 1px solid #f1f1f1;
}
#dk_Nav {
width: 742.6167px;
height: 50px;
margin-left: auto;
margin-right: auto;
top: 0;
right: 0;
left: 0;
z-index: 2001;
background: white;
}
#dk_Nav div {
margin-top: 11px;
}
.link_Button {
display: block;
float: left;
height: 40px;
font-family: 'Open Sans', sans-serif;
font-size: .7em;
color: black;
line-height: 3.3em;
text-transform: uppercase;
letter-spacing: .2em;
margin-right: 44.8px;
}
#dk_Nav div a {
text-decoration: none
}
/* LEFT TO RIGHT */
.sliding-u-l-r {
display: inline-block;
}
.sliding-u-l-r:after {
content: '';
display: block;
height: 3px;
width: 0;
background: transparent;
transition: width .3s ease, background-color .3s ease;
}
.sliding-u-l-r:hover:after {
width: 100%;
background: black;
}
#home {
width: 100%;
height: 1000px;
background: #ccc;
}
#about {
width: 100%;
height: 1000px;
background: white;
}
Javascript: Javascript:
// Scroll Menu
jQuery(document).ready(function($) {
$(".scroll").click(function(event) {
event.preventDefault();
$('html,body').animate( { scrollTop:$(this.hash).offset().top } , 1000);
});
});
Any sort of help would be highly appreciated, thanks! 任何帮助将不胜感激,谢谢!
make a .active
class that displays the bar as it looks at the end of the animation. 制作一个
.active
类,在动画结束时显示.active
。
Toggle it on click between the elements like this: 单击此元素之间的切换即可:
$(document).on('click','.scroll',function(e){
$('.scroll').removeClass('active');
$(e.target).addClass('active');
});
JSFIDDLE Example JSFIDDLE示例
https://jsfiddle.net/Panomosh/yu530bpf/ https://jsfiddle.net/Panomosh/yu530bpf/
Modify your css class like this 像这样修改您的CSS类
.sliding-u-l-r:hover:after, .sliding-u-l-r.active:after {
width: 100%;
background: black;
}
and your javascript to something like this 和你的JavaScript到这样的事情
jQuery(document).ready(function($) {
$(".scroll").click(function(event) {
event.preventDefault();
$( ".scroll" ).removeClass("active");
$( this ).addClass("active");
$('html,body').animate( { scrollTop:$(this.hash).offset().top } , 1000);
});
});
Here is the working example of what you want : 这是您想要的工作示例:
Basically on click of menu 2 interactions are happening- 基本上,点击菜单2就会发生互动-
- The elements scroll smoothly to the given id definite point.( The scroll is smoother now. )
元素平滑滚动到给定的id确定点。( 现在滚动更平滑。 )
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;
}
}
- The current menu item gets the active class .
当前菜单项获取活动类。
JS- JS-
$(this).addClass('active').parent().siblings().children().removeClass('active');
Css- CSS-
.sliding-u-l-r.active:after {
width: 100%;
background: black;
}
Below is the compiled snippet of the above explanations :) 以下是上述说明的摘要:)
// Scroll Menu $(function() { $("nav a").click(function() { //**Add class active to current clicked menu item and remove class active from other menu item**// $(this).addClass('active').parent().siblings().children().removeClass('active'); //** Smooth scroll Logic **? 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; } } }); });
#nav_Wrapper_dk { position: fixed; width: 100%; height: 50px; background: white; border-bottom: 1px solid #f1f1f1; } body { margin: 0px; } #dk_Nav { width: 742.6167px; height: 50px; margin-left: auto; margin-right: auto; top: 0; right: 0; left: 0; z-index: 2001; background: white; } #dk_Nav div { margin-top: 11px; } .link_Button { display: block; float: left; height: 40px; font-family: 'Open Sans', sans-serif; font-size: .7em; color: black; line-height: 3.3em; text-transform: uppercase; letter-spacing: .2em; margin-right: 44.8px; } #dk_Nav div a { text-decoration: none } /* LEFT TO RIGHT */ .sliding-ulr { display: inline-block; } .sliding-ulr:after { content: ''; display: block; height: 3px; width: 0; background: transparent; transition: width .3s ease, background-color .3s ease; } .sliding-ulr:hover:after { width: 100%; background: black; } .sliding-ulr.active:after { width: 100%; background: black; } #home { width: 100%; height: 1000px; background: #ccc; } #about { width: 100%; height: 1000px; background: white; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="nav_Wrapper_dk"> <nav id="dk_Nav" role="navigation" class="cf"> <div><a href="#home" class="link_Button sliding-ulr scroll">Home</a></div> <div><a href="#about" class="link_Button sliding-ulr scroll">About us</a></div> <div><a href="#link3" class="link_Button sliding-ulr">Gallery</a></div> <div><a href="#link4" class="link_Button sliding-ulr">Find Us</a></div> <div><a href="#link5" class="link_Button sliding-ulr">Contact</a></div> <div><a href="#link6" class="link_Button sliding-ulr">Catering</a></div> <div><a href="#link7" class="link_Button sliding-ulr">Blog</a></div> </nav> </div> <div id="home"></div> <div id="about"></div>
You jsfiddle example in this snippet with your requirement. 您可以根据需要在此片段中使用jsfiddle示例。 Hope this will help.
希望这会有所帮助。
// Scroll Menu jQuery(document).ready(function($) { $("div a").click(function(event) { event.preventDefault(); 1 $( "nav" ).find( ".active" ).removeClass('active'); $(this).addClass('active'); $('html,body').animate( { scrollTop:$(this.hash).offset().top } , 1000,'linear'); }); });
#nav_Wrapper_dk { position: fixed; width: 100%; height: 50px; background: white; border-bottom: 1px solid #f1f1f1; } #dk_Nav { width: 742.6167px; height: 50px; margin-left: auto; margin-right: auto; top: 0; right: 0; left: 0; z-index: 2001; background: white; } #dk_Nav div { margin-top: 11px; } .link_Button { display: block; float: left; height: 40px; font-family: 'Open Sans', sans-serif; font-size: .7em; color: black; line-height: 3.3em; text-transform: uppercase; letter-spacing: .2em; margin-right: 44.8px; } #dk_Nav div a { text-decoration: none } /* LEFT TO RIGHT */ .sliding-ulr { display: inline-block; } .sliding-ulr:after { content: ''; display: block; height: 3px; width: 0; background: transparent; transition: width .3s ease, background-color .3s ease; } .sliding-ulr:hover:after ,.sliding-ulr.active:after{ width: 100%; background: black; } #home { width: 100%; height: 1000px; background: #ccc; } #about { width: 100%; height: 1000px; background: white; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="nav_Wrapper_dk"> <nav id="dk_Nav" role="navigation" class="cf"> <div><a href="#home" class="link_Button sliding-ulr scroll active">Home</a></div> <div><a href="#about" class="link_Button sliding-ulr scroll">About us</a></div> <div><a href="#link3" class="link_Button sliding-ulr">Gallery</a></div> <div><a href="#link4" class="link_Button sliding-ulr">Find Us</a></div> <div><a href="#link5" class="link_Button sliding-ulr">Contact</a></div> <div><a href="#link6" class="link_Button sliding-ulr">Catering</a></div> <div><a href="#link7" class="link_Button sliding-ulr">Blog</a></div> </nav> </div> <div id="home"></div> <div id="about"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.