我一直试图在我的网站上添加一个平滑的滚动功能一段时间,但似乎无法让它工作。
以下是与我的导航相关的HTML代码:
<div id="nav-wrapper">
<div id="nav" class="navbar navbar-inverse affix-top" data-spy="affix" data-offset-top="675">
<div class="navbar-inner" data-spy="affix-top">
<div class="container">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<!-- Everything you want hidden at 940px or less, place within here -->
<div class="nav-collapse collapse">
<ul class="nav">
<li><a href="#home">Home</a></li>
<li><a href="#service-top">Services</a></li>
<li><a href="#contact-arrow">Contact</a></li>
</ul><!--/.nav-->
</div><!--/.nav-collapse collapse pull-right-->
</div><!--/.container-->
</div><!--/.navbar-inner-->
</div><!--/#nav /.navbar navbar-inverse-->
</div><!--/#nav-wrapper-->
这是我添加的JS代码:
<script src="js/jquery.scrollTo-1.4.3.1-min.js"></script>
<script>
$(document).ready(function(e) {
$('#nav').scrollSpy()
$('#nav ul li a').bind('click', function(e) {
e.preventDefault();
target = this.hash;
console.log(target);
$.scrollTo(target, 1000);
});
});
</script>
对于它的价值,here是我收到的关于我到目前为止所做的事情的信息,here是我当前形式的网站。如果你可以帮助我,我会给你一块馅饼或饼干或其他东西。谢谢!
答案 0 :(得分:198)
你真的需要那个插件吗?您可以为scrollTop
属性设置动画:
$("#nav ul li a[href^='#']").on('click', function(e) {
// prevent default anchor click behavior
e.preventDefault();
// store hash
var hash = this.hash;
// animate
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 300, function(){
// when done, add hash to url
// (default click behaviour)
window.location.hash = hash;
});
});
答案 1 :(得分:14)
如果你有一个固定的导航栏,你需要这样的东西。
从上述最佳答案和评论中汲取......
$(".bs-js-navbar-scrollspy li a[href^='#']").on('click', function(event) {
var target = this.hash;
event.preventDefault();
var navOffset = $('#navbar').height();
return $('html, body').animate({
scrollTop: $(this.hash).offset().top - navOffset
}, 300, function() {
return window.history.pushState(null, null, target);
});
});
首先,为了防止&#34; undefined&#34;错误,在调用target
之前将哈希存储到变量preventDefault()
,然后再引用该存储的值,如pupadupa所述。
下一步。您无法使用window.location.hash = target
,因为它会同时设置网址和位置,而不是单独设置。您最终将位于元素开头的位置,其id与href相匹配...但由固定的顶部导航栏覆盖。
为了解决这个问题,您可以将scrollTop值设置为目标的垂直滚动位置值减去固定导航栏的高度。直接定位该值可以保持平滑滚动,而不是在之后添加调整,并且会产生不专业的紧张情绪。
您会注意到网址没有变化。要设置此项,请使用return window.history.pushState(null, null, target);
来手动将网址添加到历史记录堆栈中。
完成!
其他说明:
1)使用.on
方法是最新的(截至2015年1月)jquery方法,优于.bind
或.live
,甚至.click
,原因我和#39;留给你找出来。
2)navOffset值可以在函数内部或外部,但您可能希望它在外面,因为您可能很好地引用其他函数/ DOM操作的垂直空间。但我把它留在里面,使它整齐地融入一个功能。
答案 2 :(得分:8)
$("#YOUR-BUTTON").on('click', function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $("#YOUR-TARGET").offset().top
}, 300);
});
答案 3 :(得分:4)
如果您下载了jquery easing插件(check it out),那么您只需将其添加到main.js文件中:
$('a.smooth-scroll').on('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top + 20
}, 1500, 'easeInOutExpo');
event.preventDefault();
});
并且也不要忘记将smooth-scroll类添加到您的标签中:
<li><a href="#about" class="smooth-scroll">About Us</a></li>
答案 4 :(得分:3)
我把它结合起来,这就是结果 -
$(document).ready(function() {
$("#toTop").hide();
// fade in & out
$(window).scroll(function () {
if ($(this).scrollTop() > 400) {
$('#toTop').fadeIn();
} else {
$('#toTop').fadeOut();
}
});
$('a[href*=#]').each(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname
&& this.hash.replace(/#/,'') ) {
var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
if ($target) {
var targetOffset = $target.offset().top;
$(this).click(function() {
$('html, body').animate({scrollTop: targetOffset}, 400);
return false;
});
}
}
});
});
我测试了它并且工作正常。希望这会有所帮助:)
答案 5 :(得分:2)
// styles.css
html {
scroll-behavior: smooth
}
来源:https://www.w3schools.com/howto/howto_css_smooth_scroll.asp#section2
答案 6 :(得分:0)
onetrickpony
发布的内容是可以的,但如果您想要更通用的解决方案,可以使用下面的代码。
您可以选择id
- 标记的属性name
,而不是只选择锚点的<a>
,而是选择标准格式。这样可以避免编写额外的id
标记。只需将smoothscroll类添加到navbar元素即可。
改变了什么
1)$('#nav ul li a[href^="#"]')
至$('#nav.smoothscroll ul li a[href^="#"]')
2)$(this.hash)
至$('a[name="' + this.hash.replace('#', '') + '"]')
最终代码
/* Enable smooth scrolling on all links with anchors */
$('#nav.smoothscroll ul li a[href^="#"]').on('click', function(e) {
// prevent default anchor click behavior
e.preventDefault();
// store hash
var hash = this.hash;
// animate
$('html, body').animate({
scrollTop: $('a[name="' + this.hash.replace('#', '') + '"]').offset().top
}, 300, function(){
// when done, add hash to url
// (default click behaviour)
window.location.hash = hash;
});
});
答案 7 :(得分:0)
使用此代码,该ID将不会显示在链接上
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});