2012-07-06 71 views
22

我使用下面的代码来滚动使用jQuery到锚点:jQuery的滚动锚(像素减去一定金额)

$(document).ready(function() { 
    function filterPath(string) { 
    return string 
    .replace(/^\//,'') 
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'') 
    .replace(/\/$/,''); 
    } 
    var locationPath = filterPath(location.pathname); 
    var scrollElem = scrollableElement('html', 'body'); 

    $('a[href*=#]').each(function() { 
    var thisPath = filterPath(this.pathname) || locationPath; 
    if ( locationPath == thisPath 
    && (location.hostname == this.hostname || !this.hostname) 
    && this.hash.replace(/#/,'')) { 
     var $target = $(this.hash), target = this.hash; 
     if (target) { 
     var targetOffset = $target.offset().top; 
     $(this).click(function(event) { 
      event.preventDefault(); 
      $(scrollElem).animate({scrollTop: targetOffset}, 400, function() { 
      location.hash = target; 
      }); 
     }); 
     } 
    } 
    }); 

    // use the first element that is "scrollable" 
    function scrollableElement(els) { 
    for (var i = 0, argLength = arguments.length; i <argLength; i++) { 
     var el = arguments[i], 
      $scrollElement = $(el); 
     if ($scrollElement.scrollTop()> 0) { 
     return el; 
     } else { 
     $scrollElement.scrollTop(1); 
     var isScrollable = $scrollElement.scrollTop()> 0; 
     $scrollElement.scrollTop(0); 
     if (isScrollable) { 
      return el; 
     } 
     } 
    } 
    return []; 
    } 

}); 

反正让它滚动到锚但减去一组量像素? (在我的情况,我想它去-92px)

感谢您的帮助。

+0

类似http://stackoverflow.com/questions/832860/how-to-scroll-the-window-using-jquery-scrollto-function – robbrit 2012-07-06 15:25:25

+0

他们如何相似?我要通过jquery滚动到每个锚点,当它们靠近页面顶部时,链接的内容要滚动。代码是非常不同的。 – John 2012-07-06 15:31:54

+0

唯一的区别是您要滚动到哪个选择器和偏移量。查看接受的答案,并用所需的选择器替换'#id',并用所需的偏移量替换'100'。 – robbrit 2012-07-06 15:58:50

回答

23

我必须解决这个问题我自己。您需要根据要滚动到的像素数量来调整偏移量。就我而言,我需要它在页面上高出50个像素。所以,我从targetOffset中减去了50。

现在,这扔摇晃你的代码部分的location.hash - 这是告诉浏览器其位置设置为特定点。在所有情况下,这是一个包含您刚刚滚动到的ID的字符串。所以,它会像'#foo'。你需要保持这个,所以我们将离开它。

然而,为了防止浏览器“跳”时的location.hash被设置(默认的浏览器操作),你只需要阻止默认动作。因此,请在动画函数中通过完成函数传递事件'e'。然后只需调用e.preventDefault()。你必须确保浏览器实际上正在调用一个事件,否则会出错。所以,一个if-test可以解决这个问题。

完成。下面是修改后的代码块:

if (target) { 
    var targetOffset = $target.offset().top - 50; 
    $(this).click(function(event) { 
     if(event != 'undefined') { 
      event.preventDefault();} 
     $(scrollElem).animate({scrollTop: targetOffset}, 400, function(e) { 
      e.preventDefault(); 
      location.hash = target; 
     }); 
    }); 
    } 
+0

对不起,我知道这是一个老问题,但是:animate回调没有收到任何参数(请参阅http://api.jquery.com/animate/),因此e.preventDefault()失败。在过去的2年中有这种变化吗?有没有最新的方法来防止location.hash = target的默认行为? – Andreyu 2014-06-18 13:38:51

+0

Andreyu,看看Borgar的答案在http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling – cfillol 2015-11-04 17:11:40

15

这是我使用:

function scrollToDiv(element){ 
    element = element.replace("link", ""); 
    $('html,body').unbind().animate({scrollTop: $(element).offset().top-50},'slow'); 
}; 

...其中50是增加像素数/减。

+0

谢谢!正是我需要的! – krunos 2015-11-04 12:30:23

1

我无法使用乔纳森野人的解决方案,我不能没有错误传递事件回调到动画()。我今天有这个问题,并发现了一个简单的解决方案:

 var $target = $(this.hash), target = this.hash; 
     if (target) { 
     var targetOffset = $target.offset().top - 92; 
     $(this).click(function(event) { 
      event.preventDefault(); 
      $(scrollElem).animate({scrollTop: targetOffset}, 400, function() { 
      location.hash = targetOffset; 
      }); 
     }); 

减去你的像素从targetOffset可变偏移,然后分配到的location.hash该变量。滚动到目标散列时停止页面跳转。

5

笑)

<span class="anchor" id="section1"></span> 
 
<div class="section"></div> 
 

 
<span class="anchor" id="section2"></span> 
 
<div class="section"></div> 
 

 
<span class="anchor" id="section3"></span> 
 
<div class="section"></div> 
 

 
<style> 
 
.anchor{ 
 
    display: block; 
 
    height: 115px; /*same height as header*/ 
 
    margin-top: -115px; /*same height as header*/ 
 
    visibility: hidden; 
 
} 
 
</style>

+0

非常感谢,而且没有jquery! – timhc22 2015-05-01 18:16:14

+0

这很好,但你必须知道,它可能会覆盖链接,按钮或任何其他交互元素,你在锚点上方115px,你将无法点击它们... – phoenix 2015-09-22 21:21:53

+0

完美的CSS解决方案,谢谢 – 2015-11-18 14:40:42

9

在我的网站尊重我这个代码工作的任何链接锚“150像素”高度之上修复菜单。

<!-- SMOOTH SCROLL --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 
$(function() { 
    $('a[href*=#]:not([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) { 
     $('html,body').animate({ 
      scrollTop: target.offset().top-150 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 
</script> 
<!-- End of SMOOTH SCROLL --> 
0

这是一个jQuery实现,我使用的是基于НикитаАндрейчук的解决方案。像素调整变量可以用这种方式动态设置,尽管在这个例子中它是硬编码的。

$('a').each(function() { 
    var pixels = 145; 
    var name = $(this).attr('name'); 
    if (typeof name != 'undefined') { 
     $(this).css({ 
      'display' : 'block', 
      'height'  : pixels + 'px', 
      'margin-top' : '-' + pixels + 'px', 
      'visibility' : 'hidden' 
     }); 
    } 
}); 
0

这是我使用的。根据需要设置偏移量。

$('a[href^="#"]').click(function(e) { 
    e.preventDefault(); 
    $(window).stop(true).scrollTo(this.hash {duration:1000,interrupt:true,offset: -50}); 
});