markdown轉(zhuǎn)html自動(dòng)生成導(dǎo)航腳本

  1. markdownPad2導(dǎo)入腳本:Tools->Options->Advanced->HTML Head Editor
    2.markdownPad2導(dǎo)出html:File->Export->Export HTML
    3.腳本如下:

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
//是否顯示導(dǎo)航欄
var showNavBar = true;
//是否展開(kāi)導(dǎo)航欄
var expandNavBar = true;
var currentIndex = 0;
var currentScrollHigh = 0;
var currentContentScrollHigh = 0;
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}

$(window).onbeforeunload = function(){
currentIndex = 0;
}

(window).load(function(){ var h1s =("body").find("h1");
var h2s = ("body").find("h2"); var h3s =("body").find("h3");
var h4s = ("body").find("h4"); var h5s =("body").find("h5");
var h6s = $("body").find("h6");

var headCounts = [h1s.length, h2s.length, h3s.length, h4s.length, h5s.length, h6s.length];
var vH1Tag = null;  // 顯示的最高層級(jí)
var vH2Tag = null;   // 顯示的最低層級(jí)

var sum = 0;

for(var i = 0; i < headCounts.length; i++){
      if(headCounts[i] > 0){
      for( var y = 0; y < headCounts[i]; y++)
             sum = sum + 1;
      }
}
for(var i = 0; i < headCounts.length; i++){
    if(headCounts[i] > 0){
        if(vH1Tag == null){
            vH1Tag = 'h' + (i + 1);
        }else{
            vH2Tag = 'h' + (i + 1);
        }
    }
}
if(vH1Tag == null){
    return;
}

$("body").prepend('<div class="BlogAnchor">' + 
    '<span style="color:red;position:absolute;top:-6px;left:0px;cursor:pointer;" onclick="$(\'.BlogAnchor\').hide();">×</span>' +
    '<p>' + 
        '<b id="AnchorContentToggle" title="收起" style="cursor:pointer;">目錄▲</b>' + 
    '</p>' + 
    '<div class="AnchorContent" id="AnchorContent"> </div>' + 
'</div>' );

var vH1Index = 0;
var vH2Index = 0;


var vIndexH1 = 0;
var vIndexH2 = 0;
var vIndexH3 = 0;
var vIndexH4 = 0;
var vIndexH5 = 0;
var vIndexH6 = 0;
var headerALL = [];
var headerIDALL = [];
var headerHightALL = [];
$("body").find("h1,h2,h3,h4,h5,h6").each(function(i,item){

    var id = '';
    var name = '';
    var tag = $(item).get(0).tagName.toLowerCase();
    var className = '';
    // i=0 tag=h1          i=1 tag=h2     i=2 tag=h2

    if(tag == "h1"){
        id = name = ++vIndexH1;
vIndexH2 = 0;
vIndexH3 = 0;
vIndexH4 = 0;
vIndexH5 = 0;
vIndexH6 = 0;
className = 'item_h1';

//alert("tag ="+ tag +"---- i = "+ i + " name ="+ name +" className= "+ className);
//tag =h1---- i = 0 name =1 className= item_h1
}else if(tag == "h2"){

        id = vIndexH1 + '_' + ++vIndexH2;
        name = vIndexH1 + '.' + vIndexH2;
        className = 'item_h2'; 
vIndexH3 = 0;
vIndexH4 = 0;
vIndexH5 = 0;
vIndexH6 = 0;

   }else if(tag == "h3"){
        id = vIndexH1 + '_' + vIndexH2+ '_' + ++vIndexH3;
        name = vIndexH1 + '.' + vIndexH2+ '.' + +vIndexH3;
        className = 'item_h3'; 
vIndexH4 = 0;
vIndexH5 = 0;
vIndexH6 = 0;

   }

   else if(tag == "h4"){
        id = vIndexH1 + '_' + vIndexH2+ '_'  +vIndexH3 +'_'+ ++vIndexH4 ;
        name =  vIndexH1 + '.' + vIndexH2+ '.'  +vIndexH3 +'.'+  vIndexH4 ;
        className = 'item_h4';
vIndexH5 = 0;
vIndexH6 = 0; 
   }

   else if(tag == "h5"){
        id = vIndexH1 + '_' + vIndexH2+ '_'  +vIndexH3 +'_' +vIndexH4+'_'+ ++vIndexH5;
        name = vIndexH1 + '.' + vIndexH2+ '.'  +vIndexH3 +'.' +vIndexH4+'.'+ vIndexH5;
        className = 'item_h5'; 
        vIndexH6 = 0; 
   }

   else if(tag == "h6"){
        id = vIndexH1 + '_' + vIndexH2+ '_'  +vIndexH3 +'_' +vIndexH4+'_' +vIndexH5+'_'+ ++vIndexH6;
        name = vIndexH1 + '.' + vIndexH2+ '.'  +vIndexH3 +'.' +vIndexH4+'.' +vIndexH5+'.'+ vIndexH6;
        className = 'item_h6'; 

   }
    if(name.length > 2){

    var mFirstName = name.substring(0,2);
    while(mFirstName == "0."){
    name = name.substring(2,name.length);
    mFirstName = name.substring(0,2);
     }

    }
    $(item).attr("id","wow"+id+"_index_"+i);
    $(item).addClass("wow_head");
    var itemHeight = $(item).offset().top
    $("#AnchorContent").css('max-height', ($(document).height()) + 'px');
    $("#AnchorContent").css('height', ($(window).height()) + 'px');
    $("#AnchorContent").css('overflow','auto');
    $("#AnchorContent").append('<li><a class="nav_item '+className+' anchor-link"  href="#wow'+id+'_index_'+i+'" link="#wow'+id+'" index="'+i+'">'+name+"?"+$(this).text()+" "+'</a></li>');
    var str = "#wow"+id+"_index_"+i;
    headerALL.push($(item));
    headerIDALL.push(str);
    console.log("  i = "+ i +"   id =" + id +"  itemHeight = "+ itemHeight);
});

// 1
// 1.1
// 1.1.1
// 1.1.1.1
// 1.1.1.1.1
// 1.1.1.1.1.1
$("#AnchorContentToggle").click(function(){
    var text = $(this).html();
    if(text=="目錄▲"){
        $(this).html("目錄▼");
        $(this).attr({"title":"展開(kāi)"});
    }else{
        $(this).html("目錄▲");
        $(this).attr({"title":"收起"});
    }
    $("#AnchorContent").toggle();
});

$(".anchor-link").click(function(){

    //$("html,body").animate({scrollTop: $($(this).attr("link")).offset().top}, 10);
    var index  = $(this).attr("index");
            $(".BlogAnchor li .nav_item.current").removeClass('current');
            $(headerNavs[index]).addClass('current');
                  var scrollTop = $(window).scrollTop(); // 獲得將要到達(dá)的點(diǎn)離頂距離
                  currentScrollHigh = scrollTop;
                  currentContentScrollHigh = headerHightALL[index];
  var value = headerTops[index];
  currentIndex = value;
  console.log("index = "+ index+ "  headerTops["+index+"] ="+ value + "scrollTop="+ scrollTop);
});





var headerNavs = $(".BlogAnchor li .nav_item");
var headerTops = [];
var mHeight = 0;
$(".wow_head").each(function(i, n){
var value = $(n).offset().top;

    headerTops.push($(n).offset().top);
     console.log("i = "+ i+ "  offset().top ="+ value);
});
headerTops.push($(document).height());

window.onresize = function(){

headerTops = [];
.each(headerNavs, function(i, n){(n).trigger("click");
document.querySelector(headerIDALL[i]).scrollIntoView(true);
//var high = (n).offset().top; var scrollTop =(window).scrollTop();
headerTops.push(scrollTop);
console.log("headerNavs_index="+i +" scrollTop="+scrollTop +" headerTops="+headerTops[i]);
}
);
headerTops.push($(document).height());

//("#AnchorContent").height((window).height());
("#AnchorContent").css('height', ((window).height()) + 'px');
var xcontentWidth = ("#AnchorContent").width(); var xWidth =(window).width();
var xlength = xWidth - xcontentWidth;
("body").css("marginLeft",xcontentWidth+'px');("body").css("max-width",xlength);

$(headerNavs[currentIndex]).trigger("click");
document.querySelector(headerIDALL[i]).scrollIntoView(true);

}

(window).scroll(function(){ var scrollTop =(window).scrollTop(); // 獲得將要到達(dá)的點(diǎn)離頂距離

    $.each(headerTops, function(i, n){
        if( (scrollTop >= (headerTops[i])  && scrollTop < (headerTops[i+1] -1))  ){
            console.log("headerTops[i-1]"+headerTops[i-1]+"headerTops[i]"+headerTops[i]+"  scrollTop ="+ scrollTop+"  headerTops[i+1]= "+headerTops[i+1] +"  i ="+i);
            $(".BlogAnchor li .nav_item.current").removeClass('current');
            $(headerNavs[i]).addClass('current');
            currentScrollHigh = scrollTop;
            currentContentScrollHigh = headerHightALL[i];
            currentIndex = i;
            var mxxHeight = $("#AnchorContent").height()
            var mscrollTop1 = $("#AnchorContent").scrollTop();  // 當(dāng)前標(biāo)簽的高度
        console.log("zukgit2 currentIndex="+ currentIndex );
            console.log("zukgit2 windows.scrollTop="+ scrollTop );
            console.log("zukgit2 $(window).height()="+ $(window).height() );
            console.log("zukgit2 currentContentScrollHigh="+ currentContentScrollHigh );
            console.log("zukgit2 AnchorContent.mscrollTop="+ mscrollTop1 );
            console.log("zukgit2 AnchorContent.high="+ mxxHeight );
            if((currentContentScrollHigh - mscrollTop1)>  ($(window).height()/2)){ //↓ // 如果當(dāng)前index超出  界面的高度
            //var sum =   Math.floor(headerHightALL(i) / $(window).height());
            var dif = currentContentScrollHigh - mscrollTop1;
            $("#AnchorContent").animate({scrollTop: (currentContentScrollHigh)}, 50);
            console.log("  i(zukgit3) = "+ i +"   currentContentScrollHigh =" + currentContentScrollHigh +"  mscrollTop1 = "+ mscrollTop1);
            console.log(" $(window).height()="+ $(window).height() );

            } else if( ( mscrollTop1 - currentContentScrollHigh )>  0 ){  //↑
            $("#AnchorContent").animate({scrollTop: currentContentScrollHigh-($("#AnchorContent").height()/3)}, 50);
            console.log("  i(zukgit4) = "+ i +"   currentContentScrollHigh =" + currentContentScrollHigh +"  mscrollTop1 = "+ mscrollTop1);
            console.log(" $(window).height()="+ $(window).height() +"   $(#AnchorContent).height()="+mxxHeight);
            }

            return false;
        }
    }
    );
    if(scrollTop == 0){
    $("#AnchorContent").animate({scrollTop: 0}, 50);
    }
    if(scrollTop == $(document).height()){
    $("#AnchorContent").animate({scrollTop: headerHightALL[headerHightALL.length-1]}, 50);
    }
});

.each(headerNavs, function(i, n){ var high =(n).offset().top;
headerHightALL.push(high);
console.log("high"+high);
}
);

headerTops = [];
$.each(headerNavs, function(i, n){

$(n).trigger("click");
document.querySelector(headerIDALL[i]).scrollIntoView(true);

var scrollTop = $(window).scrollTop();

headerTops.push(scrollTop);
console.log("headerNavs_index="+i +" scrollTop="+scrollTop +" headerTops="+headerTops[i]);

}
);

headerTops.push((document).height());(headerNavs[0]).trigger("click");
document.querySelector(headerIDALL[0]).scrollIntoView(true);

var xcontentWidth = ("#AnchorContent").width(); var xWidth =(window).width();
var xlength = xWidth - xcontentWidth;
("body").css("marginLeft",xcontentWidth+'px');("body").css("max-width",xlength);

if(!showNavBar){
    $('.BlogAnchor').hide();
}
if(!expandNavBar){
    $(this).html("目錄▼");
    $(this).attr({"title":"展開(kāi)"});
    $("#AnchorContent").hide();
}

});

</script>
<style>
/導(dǎo)航/
.BlogAnchor {
//background: #f1f1f1;
background: #ffffff;
//padding: 10px;
line-height: 180%;
position: fixed;
left: 0px; // right: 48px;
top: 0px;
border: 1px solid #aaaaaa;
width: 20%;
height:100%;
}
.BlogAnchor p {
font-size: 18px;
color: #15a230;
margin: 0 0 0.3rem 0;
text-align: right;
}
.BlogAnchor .AnchorContent {
//padding: 5px 0px;
overflow: auto;

}
.BlogAnchor li{
    text-indent: 0.5rem;
    font-size: 14px;
    list-style: none;
}
.BlogAnchor li .nav_item{
    padding: 3px;
}
.BlogAnchor li .item_h1{
    margin-left: 0rem;
}
.BlogAnchor li .item_h2{
    margin-left: 2rem;
    font-size: 0.8rem;
}

.BlogAnchor li .item_h3{
    margin-left: 4rem;
    font-size: 0.8rem;
}

.BlogAnchor li .item_h4{
    margin-left: 5rem;
    font-size: 0.8rem;
}

.BlogAnchor li .item_h5{
    margin-left: 6rem;
    font-size: 0.8rem;
}


.BlogAnchor li .item_h6{
    margin-left: 7rem;
    font-size: 0.8rem;
}

.BlogAnchor li .nav_item.current{
    color: white;
    background-color: #5cc26f;
}
#AnchorContentToggle {
    font-size: 13px;
    font-weight: normal;
    color: #FFF;
    display: inline-block;
    line-height: 20px;
    background: #5cc26f;
    font-style: normal;
    padding: 1px 8px;
}
.BlogAnchor a:hover {
    color: #5cc26f;
}
.BlogAnchor a {
    text-decoration: none;
}

</style>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市弯院,隨后出現(xiàn)的幾起案子贞瞒,更是在濱河造成了極大的恐慌,老刑警劉巖摆寄,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件失暴,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡微饥,警方通過(guò)查閱死者的電腦和手機(jī)逗扒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)欠橘,“玉大人矩肩,你說(shuō)我怎么就攤上這事∷嘈” “怎么了黍檩?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵叉袍,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我刽酱,道長(zhǎng)喳逛,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任棵里,我火速辦了婚禮润文,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘殿怜。我一直安慰自己典蝌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布头谜。 她就那樣靜靜地躺著骏掀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪乔夯。 梳的紋絲不亂的頭發(fā)上砖织,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音末荐,去河邊找鬼侧纯。 笑死,一個(gè)胖子當(dāng)著我的面吹牛甲脏,可吹牛的內(nèi)容都是我干的眶熬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼块请,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼娜氏!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起墩新,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤贸弥,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后海渊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體绵疲,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年臣疑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了盔憨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡讯沈,死狀恐怖郁岩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤问慎,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布萍摊,位于F島的核電站,受9級(jí)特大地震影響蝴乔,放射性物質(zhì)發(fā)生泄漏记餐。R本人自食惡果不足惜驮樊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一薇正、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧囚衔,春花似錦挖腰、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至肥哎,卻和暖如春辽俗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背篡诽。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工崖飘, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人杈女。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓朱浴,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親达椰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子翰蠢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容