我正在尝试创建一个防弹脚本,它可以通过jquery动态滚动到锚,但前提是锚的href位置指向我当前站点中的现有锚。
这是脚本:
var assConfig;
assConfig = {
duration: 500,
easing: 'swing',
complete: null
};
//Smooth scrolling with links
$('a[href*=\\#]:not([href$=\\#])').on('click', function(event) {
var hash;
// if the anchor is on another site or subsite simply proceed with the default action
// which will probably be leaving the site and go to ghe href location
if (!o(event.target).attr("href")).startsWith("#") && (##### CONDITION-PART ######)) {
return;
}
event.preventDefault();
hash = '#' + $(event.target).attr("href").split("#")[1];
if (typeof $(hash).offset() !== 'undefined') {
$('html,body').animate({
scrollTop: $(this.hash).offset().top
}, assConfig.duration, assConfig.easing, assConfig.complete);
}
});
// Smooth scrolling when the document is loaded and ready
$(document).ready(function() {
if (typeof $(location.hash).offset() !== 'undefined') {
$('html,body').animate({
scrollTop: $(location.hash).offset().top
}, assConfig.duration, assConfig.easing, assConfig.complete);
}
});
我的问题是用什么代替
##### CONDITION-PART ######
. 如果锚以hashtag开头,则它只能位于同一页上。有锚链接,如:
href="/?controller=info&action=site/foo#my-beautiful-anchor-target"
href="/#my-beautiful-anchor-target"
href="http://www.example.com/?foo#my-beautiful-anchor-target"
有时它们可能意味着同一个站点,尽管它们不同,比如:
href="http://www.example.com/?foo#my-beautiful-anchor-target"
href="/?foo#my-beautiful-anchor-target"
href="?foo#my-beatiful-anchor-target"
那么,最好的检测方法是什么
$(event.target).href
和
window.location.href
页面是否重新加载?