- $(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},
哪些几乎可以工作 – 它到正确的地方,但后退.
我也试过
scrollTop:$(target).offset().top – 20},
我也试过
scrollTop:$(hash).offset().top $(‘#access’).outerHeight()
哪个似乎没有改变任何东西.
似乎答案可能在这里:JQuery page scroll issue with fixed header但我似乎不太明白.
我知道这与其他问题类似 – 但是我已经经历了我能找到的东西,而且我的文盲还不足以证明我无法复制/粘贴任何解决问题的东西.
我会非常感谢一个解决方案.
非常感谢,
马丁
PS
我发现的另一个代码块确实有效,但是它剥离了主题标签,这使得它几乎没有用.
- $(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 },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属性包含地址:在这个case / target中由你定义的一个相对URL.如果您使用此功能,jQuery Address将会自动检测链接并触发哈希更改事件.