这应该是一个非常基本的问题,但我花了早上的大部分时间来解决这个问题,此时我几乎要认输了。我什至没有一点点 js foo ——但我发现了一段注释得很好的代码,我希望用它来为 anchor 链接设置动画:
$(document).ready(function() {
$('a[href*=#]').bind('click', function(e) {
e.preventDefault(); //prevent the "normal" behaviour which would be a "hard" jump
var target = $(this).attr("href"); //Get the target
var scrollToPosition = $(target).offset().top;
// perform animated scrolling by getting top-position of target-element and set it as scroll target
$('html, body').stop().animate({ scrollTop: scrollToPosition}, 600, function() {
location.hash = target; //attach the hash (#jumptarget) to the pageurl
});
return false;
});
});
我试图让它落在 offset().top 上方 30px 处——我试过了
$('html, body').stop().animate({scrollTop:scrollToPosition -30}, 600,
这几乎有效——它到达正确的位置,然后反弹回来。
我也尝试过
scrollTop: $(target).offset().top - 20 },
我也尝试过
scrollTop: $(hash).offset().top + $('#access').outerHeight()
这似乎没有改变任何事情。
看来答案可能在这里:JQuery page scroll issue with fixed header但我似乎不太明白。
我知道这与其他问题类似 - 但我已经浏览了我能找到的内容,而且我是文盲,无法复制/粘贴任何可以解决问题的内容。
如果能找到解决方案,我将非常感激。
非常感谢,
马丁
附注
我发现的另一 block 代码确实有效,但它剥离了主题标签,这使得它几乎毫无用处。
$(function(){
$('a[href*=#]').click(function() {
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) {
var targetOffset = $target.offset().top;
$('html,body').animate({scrollTop: targetOffset - 30}, 1000);
return false;
}
}
});
});
最佳答案
编辑:
您只需要检测固定标题的高度,并从您正确执行的 scrollToPosition
中减去该高度。问题是 window.location.hash = ""+ target;
将页面跳转到具有该 id 的元素的顶部。因此,如果您像以前一样在那里设置动画,然后更改为该散列,它将像您所描述的那样“反弹”。这是我们解决这个问题的第一种方法:
// Get the height of the header
var headerHeight = $("div#header").height();
// Attach the click event
$('a[href*=#]').bind("click", function(e) {
e.preventDefault();
var target = $(this).attr("href"); //Get the target
var scrollToPosition = $(target).offset().top - headerHeight;
$('html').animate({ 'scrollTop': scrollToPosition }, 600, function(){
window.location.hash = "" + target;
// This hash change will jump the page to the top of the div with the same id
// so we need to force the page to back to the end of the animation
$('html').animate({ 'scrollTop': scrollToPosition }, 0);
});
$('body').append("called");
});
这是第一个方法的新 jsfiddle:http://jsfiddle.net/yjcRv/1/
进一步编辑: 控制哈希更改事件的更好方法是使用像 jQuery Address 这样的插件。 。这样你就可以更多地利用你的 hashchange 事件。下面是一个用法示例:
// Get the height of the header
var headerHeight = $("div#header").height();
$.address.change(function(evt){
var target = "#" + evt["pathNames"][0]; //Get the target from the event data
// If there's been some content requested go to it…else go to the top
if(evt["pathNames"][0]){
var scrollToPosition = $(target).offset().top - headerHeight;
$('html').animate({ 'scrollTop': scrollToPosition }, 600);
}else{
$('html').animate({ 'scrollTop': '0' }, 600);
}
return false;
});
// Attach the click event
$('a').bind("click", function(e) {
// Change the location
$.address.value($(this).attr("href"));
return false;
});
此处的实例:http://www.vdotgood.com/stack/user3444.html
注意:您现在不需要将哈希添加到链接 href 属性中。以下是您可以使用 jQuery 选择器定位的链接:
<!-- This is correct -->
<a href="/target" class="myclass">Target</a>
<!-- These are incorrect -->
<a href="/#/target" class="myclass">Target</a>
<a href="#/target" class="myclass">Target</a>
要定位此链接,您可以使用如下选择器:
$("a.myclass").click(function(){
$.address.value($(this).attr("href"));
return false;
});
<小时/>
jQuery Address 实际上会查找具有以下属性的链接:
<a href="/target" rel="address:/target">Target</a>
此处的 rel
属性包含 address:
,后跟您在本例中定义的相对 URL /target
。如果您使用它,jQuery Address 将检测链接并自动触发哈希更改事件。
关于jquery - 使用 animate.scrolltop 和 (target).offset().top 占固定标题;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068587/