页面css
#uprightsideBar{
font-size:12px;
font-family:Arial, Helvetica, sans-serif;
text-align:left;
position:fixed;
top:50px;
right:0px;
auto;
height: auto;
}
#sideBarTab{
float:left;
30px;
border:1px solid #e5e5e5;
border-right:none;
text-align:center;
background:#ffffff;
}
#sideBarContents{
float:left;
overflow:auto;
overflow-x:hidden;!important;
200px;
min-height:108px;
max-height:460px;
border:1px solid #e5e5e5;
border-right:none;
background:#ffffff;
}
#sideBarContents dl{
margin:0;
padding:0;
}
#sideBarContents dt{
margin-top:5px;
margin-left:5px;
}
#sideBarContents dd, dt {
cursor: pointer;
}
#sideBarContents dd:hover, dt:hover {
color:#A7995A;
}
页首html
<script type="text/javascript">
var BlogDirectory = {
getElementPosition:function (ele) {
var topPosition = 0;
var leftPosition = 0;
while (ele){
topPosition += ele.offsetTop;
leftPosition += ele.offsetLeft;
ele = ele.offsetParent;
}
return {top:topPosition, left:leftPosition};
},
getScrollBarPosition:function () {
var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop;
return scrollBarPosition;
},
moveScrollBar:function(finalpos, interval) {
if(!window.scrollTo) {
return false;
}
window.onmousewheel = function(){
return false;
};
if (document.body.movement) {
clearTimeout(document.body.movement);
}
var currentpos =BlogDirectory.getScrollBarPosition();
var dist = 0;
if (currentpos == finalpos) {
window.onmousewheel = function(){
return true;
}
return true;
}
if (currentpos < finalpos) {
dist = Math.ceil((finalpos - currentpos)/10);
currentpos += dist;
}
if (currentpos > finalpos) {
dist = Math.ceil((currentpos - finalpos)/10);
currentpos -= dist;
}
var scrTop = BlogDirectory.getScrollBarPosition();
window.scrollTo(0, currentpos);
if(BlogDirectory.getScrollBarPosition() == scrTop)
{
window.onmousewheel = function(){
return true;
}
return true;
}
var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")";
document.body.movement = setTimeout(repeat, interval);
},
htmlDecode:function (text){
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
},
createBlogDirectory:function (id, mt, st, interval){
var elem = document.getElementById(id);
if(!elem) return false;
var nodes = elem.getElementsByTagName("*");
var divSideBar = document.createElement('DIV');
divSideBar.className = 'uprightsideBar';
divSideBar.setAttribute('id', 'uprightsideBar');
var divSideBarTab = document.createElement('DIV');
divSideBarTab.setAttribute('id', 'sideBarTab');
divSideBar.appendChild(divSideBarTab);
var h2 = document.createElement('H2');
divSideBarTab.appendChild(h2);
var txt = document.createTextNode('目录导航');
h2.appendChild(txt);
var divSideBarContents = document.createElement('DIV');
divSideBarContents.style.display = 'none';
divSideBarContents.setAttribute('id', 'sideBarContents');
divSideBar.appendChild(divSideBarContents);
var dlist = document.createElement("dl");
divSideBarContents.appendChild(dlist);
var num = 0;
mt = mt.toUpperCase();
st = st.toUpperCase();
for(var i=0; i<nodes.length; i++)
{
if(nodes[i].nodeName == mt|| nodes[i].nodeName == st)
{
var nodetext = nodes[i].innerHTML.replace(/</?[^>]+>/g,"");
nodetext = nodetext.replace(/ /ig, "");
nodetext = BlogDirectory.htmlDecode(nodetext);
nodes[i].setAttribute("id", "blogTitle" + num);
var item;
switch(nodes[i].nodeName)
{
case mt:
item = document.createElement("dt");
break;
case st:
item = document.createElement("dd");
break;
}
var itemtext = document.createTextNode(nodetext);
item.appendChild(itemtext);
item.setAttribute("name", num);
item.onclick = function(){
var pos = BlogDirectory.getElementPosition(document.getElementById("blogTitle" + this.getAttribute("name")));
if(!BlogDirectory.moveScrollBar(pos.top, interval)) return false;
};
dlist.appendChild(item);
num++;
}
}
if(num == 0) return false;
divSideBarTab.onmouseenter = function(){
divSideBarContents.style.display = 'block';
}
divSideBar.onmouseleave = function() {
divSideBarContents.style.display = 'none';
}
document.body.appendChild(divSideBar);
}
};
window.onload=function(){
BlogDirectory.createBlogDirectory("cnblogs_post_body","h2","h3",20);
}
</script>