我正在創建一個網站,其中使用了「錨定導航」,就像使用Facebook和Google郵件一樣。我有它的工作,但不完全。當我加載類似#contact
之類的頁面時,它將不會加載它,除非我單擊它的鏈接。另外,是否會有更好,更有效的方式來做我正在做的事情?我不是JS編程的最佳人選。Javascript錨定導航
的JavaScript:
$.navigate = function() {
// a new link
if($anchor != document.location.hash){
// add a preloader
$("#content")
.empty()
.html('<p class="align-center"><img src="assets/images/loading.gif" /></p>');
// change the anchor saved for next time
$anchor = document.location.hash;
// get the variables ready for the get
var i = $anchor.substring(1).split("/");
var $page = i[0];
var $y = (i[1]) ? i[1] : false;
// get the file
$.get("callback.php", { x: $page, y: $y }, function(data){
$("#content")
.empty()
.html(data);
},"html");
}
};
$(function() {
var $anchor = "";
// change from the URI. This dont work.
if(document.location.hash){ $.navigate(); }
$("a","#nav")
.css({backgroundColor:"#f7f7f7"})
.each(function() {
$(this).attr("href","#" + $(this).attr("name"));
});
setInterval('$.navigate()', 300);
});
HTML:
<div id="nav">
<ul class="horizontal-nav">
<li><a href="http://streetmafia.co.uk/" name="index">Home</a></li>
<li><a href="http://streetmafia.co.uk/about" name="about">About</a></li>
<li><a href="http://streetmafia.co.uk/portfolio" name="portfolio">Portfolio</a></li>
<li><a href="http://streetmafia.co.uk/contact" name="contact">Contact</a></li>
</ul>
<div id="l-nav"></div>
<div id="r-nav"></div>
</div>
不要忘記接受答案。 – fserb 2010-01-03 03:59:57