Jquery 使用animate.scrolltop和(target.offset().top)说明固定标题;
这应该是一个相当基本的问题,但我早上大部分时间都在讨论这个问题,现在我几乎要认输了。我甚至没有一点js-foo——但我发现了一段注释很好的代码,我希望用它来制作锚链接的动画,它是:Jquery 使用animate.scrolltop和(target.offset().top)说明固定标题;,jquery,jquery-animate,target,offset,scrolltop,Jquery,Jquery Animate,Target,Offset,Scrolltop,这应该是一个相当基本的问题,但我早上大部分时间都在讨论这个问题,现在我几乎要认输了。我甚至没有一点js-foo——但我发现了一段注释很好的代码,我希望用它来制作锚链接的动画,它是: $(document).ready(function() { $('a[href*=#]').bind('click', function(e) { e.preventDefault(); //prevent the "normal" behaviour which would be a "hard" jump v
$(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;
});
});
我试着让它降落在偏移量上方30px处()
$('html,body').stop().animate({scrollTop:scrollToPosition-30},600,
这几乎起作用了——它去对了地方,然后又反弹回来
我也试过了
scrollTop:$(目标).offset().top-20},
我也试过了
scrollTop:$(散列).offset().top+$('#access').outerHeight()
这似乎没有改变什么
答案似乎就在这里:但我似乎不太明白
我知道这与其他问题类似——但我已经完成了我能找到的东西,而且我还不够文盲,所以我无法复制/粘贴任何修复问题的东西
我非常感激能有个解决办法
非常感谢,
马丁
PS
我发现的另一段代码确实有效,但它去掉了hashtag,这使得它基本上没有用处
$(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;
}
}
});
});
编辑:
您只需检测固定标题的高度,并从正确执行的滚动位置中减去该高度。问题是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");
});
对于第一种方法,这里有一个新的
进一步编辑:
控制哈希更改事件的更好方法是使用类似的插件。有了它,您可以更多地利用哈希更改事件。下面是一个使用示例:
// 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;
});
现场示例如下:
注意:现在不需要将哈希添加到links 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>
jQuery地址实际上查找具有以下属性的链接:
<a href="/target" rel="address:/target">Target</a>
这里的rel
属性包含地址:
,后面是您在本例中定义的相对url/target
。如果您使用该属性,jQuery地址将检测链接并自动触发哈希更改事件。我知道这是一个老问题(有点)但我在网站上的固定下拉导航中遇到了类似的问题。请注意,这是一个平滑滚动的代码片段,尽管您可以通过更改动画速度轻松实现自动滚动
jQuery:
$('body').on('click','a[href^="#"]',function(event){
event.preventDefault();
var target_offset = $(this.hash).offset() ? $(this.hash).offset().top : 0;
//change this number to create the additional off set
var customoffset = 75
$('html, body').animate({scrollTop:target_offset - customoffset}, 500);
});
我已经使用这段代码很长时间了,没有任何问题。我唯一不喜欢的是,它会抓取任何#标记。因此,在像Flexslider插件这样的导航使用#的插件中,我手动将它们从插件中删除。我已经从中调整了原始脚本。它工作得很好,但不能设置延迟
var headerHeight = $("header").height();
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top - headerHeight
}, 1200, 'swing', function () {
window.location.hash = target ;
});
});
});
是的,我来晚了一点,但我突然想到了这个问题。。。
干杯!在这个问题被问到8年后,在上次评论发表6年后,我会问一个后续问题,很奇怪有人会看到这一点
我正在使用一个引导模板,该模板使用此javascript滚动到锚点,然后折叠(先前扩展的)移动菜单:
$pageScrollLink.on('click', function(e){
var anchor = $(this),
target = anchor.attr('href');
pageScroll(target);
e.preventDefault();
});
function pageScroll(target){
var ww = Math.max($window.width(), window.innerWidth),
offset = ww > 992 ? navHeightShrink : navHeight;
$htmlBody.stop().animate({
scrollTop: $(target).offset().top - (offset - 1)
}, 1000, 'easeInOutExpo');
// Automatically retract the navigation after clicking on one of the menu items.
$navbarCollapse.collapse('hide');
};
只要'href'是一个普通的老“#锚定”就行。但是如果我尝试在不同的文件中使用锚定链接,比如说“otherfile.php#myanchor”,甚至是一个完全限定的URL,比如说“它失败了”,抛出一个错误,抱怨“top”未定义或其他什么
有人知道为什么只有当href是#锚定而不是另一种格式时它才起作用吗
谢谢。非常感谢Steve。我发誓我会学到足够多的知识来理解我正在复制和粘贴的内容,希望是今年,但这让我省去了无尽的头痛。干杯-mQuick问题:代码工作得很好,但添加到url的哈希值显示为“#未定义”“-有什么想法吗?不用担心,伙计。只要坚持下去,定期学习一些好的在线教程,你就会学到你想要的知识!迪福看看tutsplus网站(,)和网站设计/开发,从Carsonified学习,所有这些都是很好的免费资源。哈——似乎不起作用。但是,我只是在chrome上试用了它。”(与ff相反)而且几乎什么也没发生——看起来要做的事情太多了。要使它在Chrome/Safari和其他webkit浏览器中工作,您需要将$('html')。animate更改为$('html,body')。animate
$pageScrollLink.on('click', function(e){
var anchor = $(this),
target = anchor.attr('href');
pageScroll(target);
e.preventDefault();
});
function pageScroll(target){
var ww = Math.max($window.width(), window.innerWidth),
offset = ww > 992 ? navHeightShrink : navHeight;
$htmlBody.stop().animate({
scrollTop: $(target).offset().top - (offset - 1)
}, 1000, 'easeInOutExpo');
// Automatically retract the navigation after clicking on one of the menu items.
$navbarCollapse.collapse('hide');
};