jquery – 使用animate.scrolltop和(target).offset()计算固定标题.

前端之家收集整理的这篇文章主要介绍了jquery – 使用animate.scrolltop和(target).offset()计算固定标题.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这应该是一个非常基本的问题,但是我已经把我的大部分时间抛在脑后了,在这一点上,我很贴心地扔在毛巾上.我甚至没有一点点的js foo – 但是我发现一个很好的评论代码块,我希望用来动画锚链接是:
  1. $(document).ready(function() {
  2. $('a[href*=#]').bind('click',function(e) {
  3. e.preventDefault(); //prevent the "normal" behavIoUr which would be a "hard" jump
  4.  
  5. var target = $(this).attr("href"); //Get the target
  6.  
  7. var scrollToPosition = $(target).offset().top;
  8.  
  9. // perform animated scrolling by getting top-position of target-element and set it as scroll target
  10. $('html,body').stop().animate({ scrollTop: scrollToPosition},600,function() {
  11. location.hash = target; //attach the hash (#jumptarget) to the pageurl
  12. });
  13.  
  14. return false;
  15.  
  16. });
  17. });

我试图让它在偏移量()上方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

我发现的另一个代码块确实有效,但是它剥离了主题标签,这使得它几乎没有用.

  1. $(function(){
  2. $('a[href*=#]').click(function() {
  3. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  4. && location.hostname == this.hostname) {
  5. var $target = $(this.hash);
  6. $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
  7. if ($target.length) {
  8. var targetOffset = $target.offset().top;
  9. $('html,body').animate({scrollTop: targetOffset - 30},1000);
  10. return false;
  11. }
  12. }
  13. });
  14. });

解决方法

编辑:
您只需要检测固定标题的高度,并从您正在正确执行的scrollToPosition中减去该高度.问题是window.location.hash =“”target;使用该ID将页面跳转到元素的顶部.所以,如果你像你这样做,然后改变到这个哈希,它会“反弹”像你所描述的动画.这是我们可以打击的第一个方法
  1. // Get the height of the header
  2. var headerHeight = $("div#header").height();
  3.  
  4. // Attach the click event
  5. $('a[href*=#]').bind("click",function(e) {
  6. e.preventDefault();
  7.  
  8. var target = $(this).attr("href"); //Get the target
  9. var scrollToPosition = $(target).offset().top - headerHeight;
  10.  
  11. $('html').animate({ 'scrollTop': scrollToPosition },function(){
  12. window.location.hash = "" + target;
  13. // This hash change will jump the page to the top of the div with the same id
  14. // so we need to force the page to back to the end of the animation
  15. $('html').animate({ 'scrollTop': scrollToPosition },0);
  16. });
  17.  
  18. $('body').append("called");
  19. });

这是一个新的jsfiddle这个第一个方法http://jsfiddle.net/yjcRv/1/

进一步编辑:
一个更好的控制哈希变化事件的方法是使用像jQuery Address这样的插件.这样可以更有效地利用你的hashchange事件.以下是一个示例用法

  1. // Get the height of the header
  2. var headerHeight = $("div#header").height();
  3.  
  4. $.address.change(function(evt){
  5. var target = "#" + evt["pathNames"][0]; //Get the target from the event data
  6.  
  7. // If there's been some content requested go to it…else go to the top
  8. if(evt["pathNames"][0]){
  9. var scrollToPosition = $(target).offset().top - headerHeight;
  10. $('html').animate({ 'scrollTop': scrollToPosition },600);
  11. }else{
  12. $('html').animate({ 'scrollTop': '0' },600);
  13. }
  14.  
  15. return false;
  16. });
  17.  
  18. // Attach the click event
  19. $('a').bind("click",function(e) {
  20. // Change the location
  21. $.address.value($(this).attr("href"));
  22.  
  23. return false;
  24. });

现场示例:http://www.vdotgood.com/stack/user3444.html

注意:您现在不需要将哈希添加到您的链接href属性.这是一个可以使用jQuery选择器定位的链接

  1. <!-- This is correct -->
  2. <a href="/target" class="myclass">Target</a>
  3.  
  4. <!-- These are incorrect -->
  5. <a href="/#/target" class="myclass">Target</a>
  6.  
  7. <a href="#/target" class="myclass">Target</a>

要定位此链接,您可以使用如下选择器:

  1. $("a.myclass").click(function(){
  2. $.address.value($(this).attr("href"));
  3. return false;
  4. });

事实上,jQuery Address寻找具有以下属性链接

  1. <a href="/target" rel="address:/target">Target</a>

这里的rel属性包含地址:在这个case / target中由你定义的一个相对URL.如果您使用此功能,jQuery Address将会自动检测链接并触发哈希更改事件.

猜你在找的jQuery相关文章