2011-02-11 94 views
0

爲了讓Google友好,我想稍微更改一下這個腳本。使用jQuery循環顯示#anchor而不是URL中的索引

我有一個頁面佈局,我使用jQuery Cycle在內容之間進行動畫製作。現在,該腳本將在URL中顯示當前幻燈片的編號。防爆。 「index.html#1」或「index.html#4」。它看起來可怕的,我認爲谷歌會想太多... ;-)

我希望它顯示的div #anchor而不是它的指數,所以網址將是「的index.html#項目」,而不是

HTML菜單:

<div id="menu"> 
    <ul> 
     <li><a href="#About">About</a></li> 
     <li><a href="#Projects">Projects</a></li> 
     <li><a href="#Learning">Learning</a></li> 
    </ul> 
    </div> 

腳本:

//Set .active to current page link 

var url = location.hash.slice(1); 
if (url < 1) { 
    $("#menu li:nth-child(" + (location.hash.slice(1) - 1) + ") a ").addClass("active"); 
} 

//Slide effect 
    $('#logo').click(function() { 
     $('.slideshow').cycle(0); 
     return false; 
    }); 
    $('#menu li a').click(function() { 
     $('.slideshow').cycle($(this).parent().index()+1); 
     return false; 
    }); 
    $('.slideshow').cycle('pause'); 



    var index = 0, hash = window.location.hash; 
    if (hash) { 
     index = /\d+/.exec(hash)[0]; 
     index = (parseInt(index) || 1) - 1; // slides are zero-based 
    } 


    $('.slideshow').cycle({ 
     fx: 'blindY', // choose your transition type, ex: fade, scrollUp, shuffle, etc... 
     speed: 700, 
     startingSlide: index, 
     cleartype: 1, 
     timeout: 3000, 
     after: function(curr,next,opts) { 
      window.location.hash = opts.currSlide + 1; 
     } 

    }); 

是它有可能使其顯示divs #anchor而不是其索引?

預先感謝您... :-)

回答

1

最簡單的方法是改變的代碼行:

window.location.hash = opts.currSlide + 1; 

到:

window.location.hash = opts.currSlide + 1 + ': ' + 
      $("#menu li:nth-child(" + (opts.currSlide + 1) + ") a ").attr('href'); 

這將參考程序的URL如:http://www..........#1: #Projects。然後,您必須仔細閱讀使用哈希的代碼的其餘部分,並在使用剩餘部分之前去掉最後一位。

hash = window.location.hash.replace(/:[^0-9] .*$/, ''); 

問候 尼爾

+0

謝謝。我怎樣才能使其相反?所以當我輸入URL中的#about時,幻燈片會顯示?你知道嗎? – 2011-02-14 11:41:50

1

使它所以之前或之後各滑動它與循環

所以之後的回調:Onafter,

function Onafter() { window.location.hash = this.hash; } 
相關問題