2017-04-30 38 views
0

尝试向此添加调整大小函数,以允许此脚本底部的if else语句根据窗口宽度进行刷新和调整大小。除非单击英雄上的向下箭头按钮,否则所有内容都可以正常工作。偏移顶部值不会在调整大小时更新。不知道如何正确执行调整大小函数更新

目前文稿中的地方 -

$(function() { 
    var windowW = $(window).width(); 
    $('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) && (windowW > 770)) { 
     $('html, body').animate({ 
      scrollTop: (target.offset().top) + 2 
     }, 450); 
     return false; 
     } else { 
      $('html, body').animate({ 
      scrollTop: (target.offset().top) - 86 
      }, 450); 
      return false; 
     } 
    } 
    }); 
}); 

事情我已经尝试过 - 这接缝打破它。

$(window).resize(function(e) { 
    var windowW = $(window).width(); 
    $('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) && (windowW > 770)) { 
     $('html, body').animate({ 
      scrollTop: (target.offset().top) + 2 
     }, 450); 
     return false; 
     } else { 
      $('html, body').animate({ 
      scrollTop: (target.offset().top) - 86 
      }, 450); 
      return false; 
     } 
    } 
    }); 
}); 

DEV LINK http://www.alexcoven.com/dev/element

+0

你需要刷新整个脚本吗?看起来就像'windowW'变量应该做的那样? – Sam0

+0

是啊,只是winowW变量 – alcoven

回答

0

感谢@ Sam0这是为我工作

$(function() { 
    var windowW = $(window).width(); 
    $(window).resize(function(){ 
    windowW = $(window).width(); 
    }); 
    $('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) && (windowW > 770)) { 
     $('html, body').animate({ 
      scrollTop: (target.offset().top) + 2 
     }, 450); 
     return false; 
     } else { 
      $('html, body').animate({ 
      scrollTop: (target.offset().top) - 86 
      }, 450); 
      return false; 
     } 
    } 
    }); 
}); 

所有我需要补充的是,更新windowW变量上的调整大小功能的脚本!

1

你能尝试只更新在调整大小windowW变量。每次新的点击后,以下变化也会重新评估'a[href*="#"]:not([href="#"])'。如果不成功,通过评论反馈?

$(function() { 
    var windowW = $(window).width(); 
    $(window).resize(function(){ 
    windowW = $(window).width(); 
    }); 
    $('body').on('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) && (windowW > 770)) { 
     $('html, body').animate({ 
      scrollTop: (target.offset().top) + 2 
     }, 450); 
     return false; 
     } else { 
      $('html, body').animate({ 
      scrollTop: (target.offset().top) - 86 
      }, 450); 
      return false; 
     } 
    } 
    },'a[href*="#"]:not([href="#"])'); 
}); 
+0

非常感谢你的家伙!我使用了脚本的顶部。底部禁用了平滑滚动。 – alcoven

相关问题