【发布时间】:2017-04-30 18:14:22
【问题描述】:
尝试为此添加一个调整大小函数,以允许此脚本底部的 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;
}
}
});
});
【问题讨论】:
-
是否需要刷新整个脚本?看起来像
windowW变量应该做的? -
是的,只是 winowW 变量
标签: jquery css function scroll resize