markdown转html自动生成导航脚本

  1. markdownPad2导入脚本:Tools->Options->Advanced->HTML Head Editor
    2.markdownPad2导出html:File->Export->Export HTML
    3.脚本如下:

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
//是否显示导航栏
var showNavBar = true;
//是否展开导航栏
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;  // 显示的最高层级
var vH2Tag = null;   // 显示的最低层级

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":"展开"});
    }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(); // 获得将要到达的点离顶距离
                  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(); // 获得将要到达的点离顶距离

    $.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();  // 当前标签的高度
        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)){ //↓ // 如果当前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":"展开"});
    $("#AnchorContent").hide();
}

});

</script>
<style>
/导航/
.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>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355

推荐阅读更多精彩内容