2013-02-01 12 views
0

http://zentili.koding.com我已經得到這個JavaScript,加載索引頁的主#content div內的鏈接菜單項的內容,並應用散列加載頁面的名稱減去'.php',否則加載散列+'.php',如果它輸入在URL中。作品非常好。另一方面,ENG/ITA條目在散列之前的url內部添加?locale = lang_LANG,以便本地化也可以正常工作。如果你看起來不錯,你可能會注意到當你在ENG和ITA之間切換時,索引內容會在去散列之前出現一會兒。我知道這是因爲頁面首先被加載,然後被帶到哈希,但我想知道是否有某種方法隱藏主頁,並在加載時直接進入哈希位置。加載頁面在其散列,然後直接顯示結果,而不顯示索引第一

這裏我的菜單代碼:

<script type="text/javascript"> 
$(document).ready(function() { 
var hash = window.location.hash.substr(1); 
var href = $('#menubar a.item').each(function(){ 
var href = $(this).attr('href'); 
if(hash==href.substr(0,href.length-4)){ 
    var toLoad = hash+'.php'; 
    $('#content').load(toLoad); 
    $("#menubar a.item").removeClass("current"); 
    $(this).addClass("current"); 
} 
}); 

$('#menubar a.item').click(function(){ 
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4); 
var toLoad = $(this).attr('href'); 
    $('#content').fadeOut('fast',loadContent); 
    function loadContent() { 
    $('#content').load(toLoad,'',showNewContent) } 
    function showNewContent() { 
    $('#content').fadeIn('fast'); } 
    $("#menubar a.item").removeClass("current"); 
    $(this).addClass("current"); 

    return false; 
}); 

}); 

function goENG(){ 
    var hash = window.location.hash; 
    var eng = '?locale=en_EN'; 
     window.location.replace(eng+hash) ; 
}; 

function goITA(){ 
    var hash = window.location.hash; 
    var ita = '?locale=it_IT'; 
     window.location.replace(ita+hash) ; 
}; 

</script> 

功能goENG()和goITA()通過在ENG和ITA一的onclick調用。我希望找到一些解決方案。

回答

0

頁面無法直接進入鏈接。它將按照自然順序加載,然後它將轉到散列表。對於你想達到的目標,我相信有一個簡單的解決方案。

  1. 在文檔加載之前隱藏主內容div。使用css規則「知名度:隱藏」爲此
  2. 如果有散列,請加載它,然後使內容可見。
  3. 如果網址中沒有散列,請在dom加載時使內容可見。

    $(document).ready(function() { 
         var hash = window.location.hash.substr(1); 
         if ($('#menubar a.item').length > 0) { 
          var href = $('#menubar a.item').each(function(){ 
           var href = $(this).attr('href'); 
           if(hash==href.substr(0,href.length-4)){ 
            var toLoad = hash+'.php'; 
            $('#content').load(toLoad, function(){ 
             $('#content').attr('visibility', 'visible'); 
            }); 
    
            $("#menubar a.item").removeClass("current"); 
            $(this).addClass("current"); 
           } else { 
            $('#content').attr('visibility', 'visible');      
           } 
          }); 
         } else { 
          $('#content').attr('visibility', 'visible'); 
         } 
        }); 
    

--UPDATE--

如果設置爲#內容 「visibility:hidden的」

$('#content').attr('visibility', 'visible'); 

應始終火,否則你的#內容股利將不可見。這裏的技巧是在我們完成哈希檢查後將其設置爲可見。您可以繼續加載div中的內容並使其可見。在完全加載散列之後,使#content div可見不需要完成。

+0

對此有幫助嗎? –

+0

我如何在後臺「加載」散列?如果(window.location.hash){('#content')。attr('visibility','visible'); }; } })添加'$(document).ready(function() ;'with'visibility:hidden;'on #content根本不顯示內容! – zentili

+0

我已經更新了上面的答案。你需要做的可見性:隱藏在CSS中做可見性在js中可見後,檢查散列並加載DOM –

相關問題