【发布时间】:2014-09-23 05:23:21
【问题描述】:
我在许多页面上都看到了以下代码 sn-p。我了解它用于根据不同的 id 锚标签进行平滑滚动。但是,对于正则表达式替换、this 和 hash 变量的工作方式/方式,我仍然有些困惑。
这个频繁的代码 sn-p 到底在做什么?
$(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
}, 1000);
return false;
}
}
});
});
【问题讨论】:
标签: javascript jquery regex smooth-scrolling