之前,我有这个剧本,我的导航:jQuery的scrollTo捕捉负载
$(function() {
$("a").click(function (e) {
var target = $(this).attr("href");
if (target.match("^#")) { // Check to see if we are an anchor link
e.preventDefault(); // Prevent default only if we are :)
navigate(target);
}
});
});
function navigate(target) {
var offset = $("header").outerHeight(true);
var $target = $(target);
if ($target.length > 0) {
var position = $target.offset().top - offset;
$("html, body").animate({
scrollTop: position
}, 500);
}
};
主要的事情要注意这个是偏移因为导航是固定。 我对这个网站看起来是这样的:
<nav id="navigation" class="navbar navbar-main" role="navigation">
<div class="collapse navbar-collapse">
<ul class="nav">
<li class="active">@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Strategies", "Index", "Strategies")</li>
<li><a href="#services">Services</a></li>
<li><a href="#packages">Packages</a></li>
<li><a href="#work">How we work</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#clients">Clients</a></li>
<li>@Html.ActionLink("Blog", "Index", "Blog")</li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</nav>
注意的HREFs这里。他们使用的散列正如你所期望的那样。但正如你所看到的,有2页将你带到另一个单独的页面(策略和博客)。 这些页面上的菜单如下:
<nav id="navigation" class="navbar navbar-main" role="navigation">
<div class="collapse navbar-collapse">
<ul class="nav">
<li class="@Html.ActivePage("Home", "Index")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@Html.ActivePage("Strategies", "Index")">@Html.ActionLink("Strategies", "Index", "Strategies")</li>
<li><a href="/#services">Services</a></li>
<li><a href="/#packages">Packages</a></li>
<li><a href="/#work">How we work</a></li>
<li><a href="/#portfolio">Portfolio</a></li>
<li><a href="/#clients">Clients</a></li>
<li class="@Html.ActivePage("Blog", "Index")">@Html.ActionLink("Blog", "Index", "Blog")</li>
<li><a href="/#contact">Contact</a></li>
</ul>
</div>
</nav>
唯一不同的是,所有的环节现在有/#,而不是仅仅#将首先将其转到主页,然后它的动作它的位置。
因为这是一个正常的链接,它不是由我的导航字符串处理,所以我创造了这个功能:
function getHashtag() {
var href = location.href; // get the url
var split = href.split("#"); // split the string; usually there'll be only one # in an url so there'll be only two parts after the splitting
if (split[1] != null) {
navigate(split[1]);
}
}
它得到的哈希,然后明确要求我导航功能。
我的整个脚本现在看起来是这样的:
$(function() {
getHashtag();
$("a").click(function (e) {
var target = $(this).attr("href");
if (target.match("^#")) { // Check to see if we are an anchor link
e.preventDefault(); // Prevent default only if we are :)
navigate(target);
}
});
});
function getHashtag() {
var href = location.href; // get the url
var split = href.split("#"); // split the string; usually there'll be only one # in an url so there'll be only two parts after the splitting
if (split[1] != null) {
navigate(split[1]);
}
}
function navigate(target) {
var offset = $("header").outerHeight(true);
var $target = $(target);
if ($target.length > 0) {
var position = $target.offset().top - offset;
$("html, body").animate({
scrollTop: position
}, 500);
}
};
正如你所看到的,我的页面加载后调用getHashtag,我需要做的就是把它叫做什么装载前和覆盖默认操作(可能是e.preventDefault)。 有人知道我该怎么做?
干杯
我不知道你在找什么难道要防止在哈希值的URL的情况下,默认的“jumpto”的行为? –
您可以在您的页面的
部分之前将您的“getHashtag”调用放入内联脚本中,以便在加载完整页面之前执行它。但是你的“导航”函数使用了一些jQuery方法,这些方法需要加载jQuery库。您可以尝试不使用jQuery,使用JavaScript的getElementById和其他函数。 –