Jquery 滚动到锚点 -60px
Jquery Scroll to Anchor -60px
我有这个 JQuery 功能,我希望它滚动到位置 - 60px,因为我有一个与内容重叠的固定导航栏。如何将这个 60px 添加到此代码?
/**
* Scroll to section
* @param string des HTML identity of section block
* @return void
*/
function goToSectionID(des){
var os = (history.pushState)?51:0;
os = (jQuery(window).width()>800)?os:0;
var pos = (jQuery(des).length>0 )?jQuery(des).offset().top-os:0;
onanimate = true;
jQuery('html,body').animate({scrollTop:pos},1000,function(){
if(history.pushState){
history.pushState(null,null,des);
}else window.location.hash = des;
jQuery(window).scrollTop(pos);
onanimate=false
});
}
那个函数是太复杂了,还是太简单了,要看情况,但是我们还没有看到你的代码。
最简单的方法:
$(".arrowimg").click(function () {
$('html, body').animate({
scrollTop: $('#in').offset().top += 500
},500);
});
第一行显然是,eventListener 和处理程序。第二行,出于跨浏览器的原因,目标 html 和正文。使用动画方法。 ScrollTop,然后在 HTML
中带有 ID 锚点的目标
<div class="arrowimg" id="in"></div>
那么我不知道你是想向上还是向下移动 60 像素,因为我们没有你的代码,但这是 + 或 - 的问题。
这是link
我有这个 JQuery 功能,我希望它滚动到位置 - 60px,因为我有一个与内容重叠的固定导航栏。如何将这个 60px 添加到此代码?
/**
* Scroll to section
* @param string des HTML identity of section block
* @return void
*/
function goToSectionID(des){
var os = (history.pushState)?51:0;
os = (jQuery(window).width()>800)?os:0;
var pos = (jQuery(des).length>0 )?jQuery(des).offset().top-os:0;
onanimate = true;
jQuery('html,body').animate({scrollTop:pos},1000,function(){
if(history.pushState){
history.pushState(null,null,des);
}else window.location.hash = des;
jQuery(window).scrollTop(pos);
onanimate=false
});
}
那个函数是太复杂了,还是太简单了,要看情况,但是我们还没有看到你的代码。
最简单的方法:
$(".arrowimg").click(function () {
$('html, body').animate({
scrollTop: $('#in').offset().top += 500
},500);
});
第一行显然是,eventListener 和处理程序。第二行,出于跨浏览器的原因,目标 html 和正文。使用动画方法。 ScrollTop,然后在 HTML
中带有 ID 锚点的目标<div class="arrowimg" id="in"></div>
那么我不知道你是想向上还是向下移动 60 像素,因为我们没有你的代码,但这是 + 或 - 的问题。
这是link