admin管理员组文章数量:1387339
Hi guys so i am trying to do some js work and learn and i wanted to create a simple navbar so when the user clicks on it , it will take them to that section of the screen, which is all on one page, however the way i have done it seems very long and bad and i was wondering if there is an easier way to do it? or maybe a library that does it for you etc. If anyone could show me a tutorial or something that could help
Fiddle
html:
<nav class="navbar navbar-default" data-spy="affix" data-offset-top="10">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="#a">a</a></li>
<li><a href="#b">b</a></li>
<li><a href="#c">c</a></li>
<li><a href="#d">d</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>
JS:
$(document).ready(function () {
$(document).on("scroll", onScroll);
//smoothscroll
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
$(document).off("scroll");
$('a').each(function () {
$(this).removeClass('active');
})
$(this).addClass('active');
var target = this.hash,
menu = target;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top+2
}, 500, 'swing', function () {
window.location.hash = target;
$(document).on("scroll", onScroll);
});
});
});
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#navbar a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
$('#navbarr ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
css:
affix {
top: 0;
width: 100%
}
.affix + .section {
margin-top: 68px;
}
.navbar {
margin-bottom: 0px;
left: 0;
top: 0;
width: 100%;
list-style: none;
height: 70px;
z-index: 1
}
.navbar-nav {
float: none;
margin: 0;
text-align: center
}
.navbar-nav>li {
float: none;
display: inline-block
}
.navbar-nav>li>a {
line-height: 38px
}
.navbar-nav>li>a.active {
background-color: #E7E7E7
}
.navbar-default .navbar-nav>li>a:hover, .navbar-default .navbar-nav>li>a:focus {
color: #333333;
background-color: #E7E7E7
}
.navbar-toggle {
background-color: #000000;
background-image: none;
border-radius: 4px;
float: right;
margin-bottom: 8px;
margin-right: 15px;
margin-top: 18px;
padding: 9px 10px;
position: relative
}
.navbar-inverse .navbar-toggle .icon-bar {
background-color: #2DCC70
}
.navbar-default .navbar-toggle:hover, .navbar-default .navbar-toggle:focus {
background-color: #000000
}
.navbar-inverse .navbar-collapse, .navbar-inverse .navbar-form {
border-color: transparent
}
#a{
height: 700px;
background-color: Red;
}
#b{
height: 700px;
background-color: blue;
}
#c{
height: 700px;
background-color: yellow;
}
#d{
height: 700px;
background-color: black;
}
Hi guys so i am trying to do some js work and learn and i wanted to create a simple navbar so when the user clicks on it , it will take them to that section of the screen, which is all on one page, however the way i have done it seems very long and bad and i was wondering if there is an easier way to do it? or maybe a library that does it for you etc. If anyone could show me a tutorial or something that could help
Fiddle
html:
<nav class="navbar navbar-default" data-spy="affix" data-offset-top="10">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="#a">a</a></li>
<li><a href="#b">b</a></li>
<li><a href="#c">c</a></li>
<li><a href="#d">d</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>
JS:
$(document).ready(function () {
$(document).on("scroll", onScroll);
//smoothscroll
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
$(document).off("scroll");
$('a').each(function () {
$(this).removeClass('active');
})
$(this).addClass('active');
var target = this.hash,
menu = target;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top+2
}, 500, 'swing', function () {
window.location.hash = target;
$(document).on("scroll", onScroll);
});
});
});
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#navbar a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
$('#navbarr ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
css:
affix {
top: 0;
width: 100%
}
.affix + .section {
margin-top: 68px;
}
.navbar {
margin-bottom: 0px;
left: 0;
top: 0;
width: 100%;
list-style: none;
height: 70px;
z-index: 1
}
.navbar-nav {
float: none;
margin: 0;
text-align: center
}
.navbar-nav>li {
float: none;
display: inline-block
}
.navbar-nav>li>a {
line-height: 38px
}
.navbar-nav>li>a.active {
background-color: #E7E7E7
}
.navbar-default .navbar-nav>li>a:hover, .navbar-default .navbar-nav>li>a:focus {
color: #333333;
background-color: #E7E7E7
}
.navbar-toggle {
background-color: #000000;
background-image: none;
border-radius: 4px;
float: right;
margin-bottom: 8px;
margin-right: 15px;
margin-top: 18px;
padding: 9px 10px;
position: relative
}
.navbar-inverse .navbar-toggle .icon-bar {
background-color: #2DCC70
}
.navbar-default .navbar-toggle:hover, .navbar-default .navbar-toggle:focus {
background-color: #000000
}
.navbar-inverse .navbar-collapse, .navbar-inverse .navbar-form {
border-color: transparent
}
#a{
height: 700px;
background-color: Red;
}
#b{
height: 700px;
background-color: blue;
}
#c{
height: 700px;
background-color: yellow;
}
#d{
height: 700px;
background-color: black;
}
Share
Improve this question
asked Oct 2, 2017 at 16:35
RonTheOldRonTheOld
7775 gold badges18 silver badges31 bronze badges
2 Answers
Reset to default 3You should be able to use this code snippet to smoothly scroll to the desired element on link click:
$("#navbar a").click(function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 2000);
});
jQuery scroll to element
Working example: https://jsfiddle/345jpqwo
You can use this
$('ul.navbar-nav>li').find('a').click(function(){
var $href = $(this).attr('href');
var $anchor = $('#'+$href).offset();
$('body').animate({ scrollTop: $anchor.top },1000);
return false;
});
本文标签: javascriptNavbar to scroll to position after clickStack Overflow
版权声明:本文标题:javascript - Navbar to scroll to position after click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744555330a2612438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论