简单的放松引导滚动间谍
Simple easing on bootstrap scrollspy
我认为对于那些熟悉javascript/jquery的人来说,这是一个非常简单的问题。我对这一切都很陌生,无法做到。我发现计算导航栏偏移量的代码看起来像这样:
var offset = 50;
$('.navbar li a').click(function(event) {
event.preventDefault();
$($(this).attr('href'))[0].scrollIntoView();
scrollBy(0, -offset);
});
这是目前为止我所拥有的最简单的例子。你可以看到,如果你点击导航栏中的链接它会跳转到section。在这个脚本中添加easing
,使它向下滚动一点平滑?
原始代码,我发现首先我有光滑的滚动,但新的脚本丢失。这是旧代码:
$(function() {
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500, 'easeInOutExpo');
event.preventDefault();
});
});
Plavookac大家好
我已经在这个Fiddle中为您设置了一个工作示例。当你把这段代码放在你的页面上时,把它放在所有其他js脚本链接的下面。或者如果你把它放在一个脚本链接中,把链接放在最后。我认为您已经有了jquery链接。
看看这里的代码,你会看到平滑的滚动和偏移。
$(document).ready(function(){
// Add scrollspy to <body>
$('body').scrollspy({target: "#navbar"});
// Add smooth scrolling on all links inside the navbar
$("#navbar a").on('click', function(event) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top - 60
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
});
});
注意这行代码…event.preventDefault();
这是用来防止闪烁时,第一次点击开始滚动。
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top - 60
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
这有帮助吗?
相关文章:
- 推特引导滚动间谍总是选择最后一个元素
- 启用数据间谍内部的滚动“;粘贴“;
- 引导滚动间谍卡在一个元素上
- 如何以多个目标可以同时处于活动状态的方式实现“滚动间谍”
- 使用子菜单具体化滚动间谍
- 推特引导滚动间谍
- 引导滚动间谍仅在第二次单击时获得高度偏移
- 引导滚动间谍偏移高度在火狐中不起作用
- 引导滚动间谍不起作用,导航菜单按钮导致标题重新加载
- 引导:滚动间谍和平滑滚动
- 按钮的滚动间谍偏移量
- Twitter引导滚动间谍 - 在平滑滚动时禁用
- 删除页面顶部的滚动间谍效果
- 使用推特引导滚动间谍
- Bootstrap滚动间谍和粘性导航不能一起工作
- 引导3.1附加滚动间谍内容保持可见时,使用溢出
- 简单的放松引导滚动间谍
- 引导水平滚动间谍
- Bootstrap粘贴导航条滚动间谍问题
- 如何将平滑滚动添加到Bootstrap's的滚动间谍功能