基于Jquery實(shí)現(xiàn)仿百度百科右側(cè)導(dǎo)航代碼附源碼下載
先給大家展示下效果圖,看看是不是親想要實(shí)現(xiàn)的效果,如果還滿意的話請(qǐng)查看本文詳情,同時(shí)大家也可以下載源碼哦。
效果圖:

代碼說明:
仿百度百科右側(cè)導(dǎo)航代碼jquery插件,這個(gè)仿百科右側(cè)導(dǎo)航j(luò)s代碼,在很久以前就像搞個(gè)用了,因?yàn)檫@個(gè)導(dǎo)航特別適合長篇文檔使用,花了半天時(shí)間寫了這個(gè)仿百科導(dǎo)航插件,不過和百度百科比起來還是有點(diǎn)弱,沒有實(shí)現(xiàn)右側(cè)導(dǎo)航區(qū)域滾動(dòng)的功能,如果您的文檔不是超級(jí)變態(tài)長,應(yīng)該夠用。
如果你的導(dǎo)航超級(jí)長,可能要用到側(cè)邊導(dǎo)航頁可以滾動(dòng),一般情況下還是用不到,等過些時(shí)候有好的實(shí)現(xiàn)思路再搞吧,暫時(shí)沒想到實(shí)現(xiàn)右側(cè)導(dǎo)航區(qū)域和左側(cè)內(nèi)容的滾動(dòng)并且聯(lián)動(dòng)的好辦法。
注意:var directoryNav = new DirectoryNav($("h2,h3"),{}); 中的"h2,h3"就是生成兩級(jí)導(dǎo)航的內(nèi)容節(jié)點(diǎn)
/*
*仿百度百科右側(cè)導(dǎo)航代碼 - 頁面目錄結(jié)構(gòu)導(dǎo)航 v0.01
* 只寫了兩級(jí),無限級(jí)別也可以,是邏輯上的級(jí)別,html結(jié)構(gòu)全是同一級(jí)別
* 滑標(biāo)動(dòng)畫用的css3過渡動(dòng)畫,不支持的瀏覽器就沒動(dòng)畫效果了
* 和百度百科比起來還是比較弱,沒有實(shí)現(xiàn)右邊也可以滾動(dòng)的功能
*/
function DirectoryNav($h,config){
this.opts = $.extend(true,{
scrollThreshold:0.5, //滾動(dòng)檢測(cè)閥值 0.5在瀏覽器窗口中間部位
scrollSpeed:700, //滾動(dòng)到指定位置的動(dòng)畫時(shí)間
scrollTopBorder:500, //滾動(dòng)條距離頂部多少的時(shí)候顯示導(dǎo)航
easing: 'swing', //不解釋
delayDetection:200, //延時(shí)檢測(cè),避免滾動(dòng)的時(shí)候檢測(cè)過于頻繁
scrollChange:function(){}
},config);
this.$win = $(window);
this.$h = $h;
this.$pageNavList = "";
this.$pageNavListLis ="";
this.$curTag = "";
this.$pageNavListLiH = "";
this.offArr = [];
this.curIndex = 0;
this.scrollIng = false;
this.init();
}
DirectoryNav.prototype = {
init:function(){
this.make();
this.setArr();
this.bindEvent();
},
make:function(){
//生成導(dǎo)航目錄結(jié)構(gòu),這是根據(jù)需求自己生成的。如果你直接在頁面中輸出一個(gè)結(jié)構(gòu)那也挺好不用 搞js
$("body").append('<div class="directory-nav" id="directoryNav"><ul></ul><span class="cur-tag"></span><span class="c-top"></span><span class="c-bottom"></span><span class="line"></span></div>>');
var $hs = this.$h,
$directoryNav = $("#directoryNav"),
temp = [],
index1 = 0,
index2 = 0;
$hs.each(function(index){
var $this = $(this),
text = $this.text();
if(this.tagName.toLowerCase()=='h2'){
index1++;
if(index1%2==0) index2 = 0;
temp.push('<li class="l1"><span class="c-dot"></span>'+index1+'. <a class="l1-text">'+text+'</a></li>');
}else{
index2++;
temp.push('<li class="l2">'+index1+'.'+index2+' <a class="l2-text">'+text+'</a></li>');
}
});
$directoryNav.find("ul").html(temp.join(""));
//設(shè)置變量
this.$pageNavList = $directoryNav;
this.$pageNavListLis = this.$pageNavList.find("li");
this.$curTag = this.$pageNavList.find(".cur-tag");
this.$pageNavListLiH = this.$pageNavListLis.eq(0).height();
if(!this.opts.scrollTopBorder){
this.$pageNavList.show();
}
},
setArr:function(){
var This = this;
this.$h.each(function(){
var $this = $(this),
offT = Math.round($this.offset().top);
This.offArr.push(offT);
});
},
posTag:function(top){
this.$curTag.css({top:top+'px'});
},
ifPos:function(st){
var offArr = this.offArr;
//console.log(st);
var windowHeight = Math.round(this.$win.height() * this.opts.scrollThreshold);
for(var i=0;i<offArr.length;i++){
if((offArr[i] - windowHeight) < st) {
var $curLi = this.$pageNavListLis.eq(i),
tagTop = $curLi.position().top;
$curLi.addClass("cur").siblings("li").removeClass("cur");
this.curIndex = i;
this.posTag(tagTop+this.$pageNavListLiH*0.5);
//this.curIndex = this.$pageNavListLis.filter(".cur").index();
this.opts.scrollChange.call(this);
}
}
},
bindEvent:function(){
var This = this,
show = false,
timer = 0;
this.$win.on("scroll",function(){
var $this = $(this);
clearTimeout(timer);
timer = setTimeout(function(){
This.scrollIng = true;
if($this.scrollTop()>This.opts.scrollTopBorder){
if(!This.$pageNavListLiH) This.$pageNavListLiH = This.$pageNavListLis.eq(0).height();
if(!show){
This.$pageNavList.fadeIn();
show = true;
}
This.ifPos( $(this).scrollTop() );
}else{
if(show){
This.$pageNavList.fadeOut();
show = false;
}
}
},This.opts.delayDetection);
});
this.$pageNavList.on("click","li",function(){
var $this = $(this),
index = $this.index();
This.scrollTo(This.offArr[index]);
})
},
scrollTo: function(offset,callback) {
var This = this;
$('html,body').animate({
scrollTop: offset
}, this.opts.scrollSpeed, this.opts.easing, function(){
This.scrollIng = false;
//修正彈兩次回調(diào) 蛋疼
callback && this.tagName.toLowerCase()=='body' && callback();
});
}
};
//調(diào)用實(shí)例化
var directoryNav = new DirectoryNav($("h2,h3"),{
scrollTopBorder:0 //滾動(dòng)條距離頂部多少的時(shí)候顯示導(dǎo)航,如果為0,則一直顯示
});
在寫百度百科右側(cè)導(dǎo)航j(luò)s代碼的時(shí)候有個(gè)想法,就是讓右側(cè)導(dǎo)航區(qū)域的滾動(dòng)條和左側(cè)內(nèi)容的滾動(dòng)條用一個(gè)計(jì)算公式讓他們關(guān)聯(lián)起來,實(shí)現(xiàn)同步滾動(dòng)。
相關(guān)文章
jquery實(shí)現(xiàn)類似淘寶星星評(píng)分功能有截圖
本節(jié)主要介紹了jquery實(shí)現(xiàn)類似淘寶星星評(píng)分功能,經(jīng)測(cè)試效果相當(dāng)不錯(cuò),有圖有真相,喜歡的朋友可以參考下2014-09-09
可兼容IE的獲取及設(shè)置cookie的jquery.cookie函數(shù)方法
在使用IE來測(cè)試的時(shí)候,發(fā)現(xiàn)Discuz中的common.js里面的getcookie和setcookie這兩個(gè)方法子啊IE下不起作用,因此有了jquery.cookie.js的由來,感興趣的朋友可以參考下2013-09-09
jQuery對(duì)指定元素中指定字符串進(jìn)行替換的方法
這篇文章主要介紹了jQuery對(duì)指定元素中指定字符串進(jìn)行替換的方法,涉及jQuery針對(duì)字符串的匹配與替換的操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
jquery數(shù)組封裝使用方法分享(jquery數(shù)組遍歷)
JQuery對(duì)數(shù)組的處理非常便捷并且功能強(qiáng)大齊全,一步到位的封裝了很多原生js數(shù)組不能企及的功能。下面來看看JQuery數(shù)組的強(qiáng)大之處在哪。2014-03-03

