复制代码 代码如下: 用 Javascript 实现锚点(Anchor)间平滑跳转 <BR> // 说明 :用 Javascript 实现锚点(Anchor)间平滑跳转 <BR> <BR> // 转换为数字 <BR> function intval(v) <BR> { <BR> v = parseInt(v); <BR> return isNaN(v) ? 0 : v; <BR> } <BR> // 获取元素信息 <BR> function getPos(e) <BR> { <BR> var l = 0; <BR> var t = 0; <BR> var w = intval(e.style.width); <BR> var h = intval(e.style.height); <BR> var wb = e.offsetWidth; <BR> var hb = e.offsetHeight; <BR> while (e.offsetParent) <BR> { <BR> l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0); <BR> t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0); <BR> e = e.offsetParent; <BR> } <BR> l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0); <BR> t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0); <BR> return {x:l, y:t, w:w, h:h, wb:wb, hb:hb}; } <BR> // 获取滚动条信息 <BR> function getScroll() <BR> { <BR> var t, l, w, h; <BR> if (document.documentElement && document.documentElement.scrollTop) <BR> { <BR> t = document.documentElement.scrollTop; <BR> l = document.documentElement.scrollLeft; <BR> w = document.documentElement.scrollWidth; <BR> h = document.documentElement.scrollHeight; <BR> } <BR> else if (document.body) <BR> { <BR> t = document.body.scrollTop; <BR> l = document.body.scrollLeft; <BR> w = document.body.scrollWidth; <BR> h = document.body.scrollHeight; <BR> } <BR> return { t: t, l: l, w: w, h: h }; <BR> } <BR> // 锚点(Anchor)间平滑跳转 <BR> function scroller(el, duration) <BR> { <BR> if(typeof el != 'object') <BR> { <BR> el = document.getElementById(el); <BR> } <BR> if(!el) return; <BR> var z = this; <BR> z.el = el; <BR> z.p = getPos(el); <BR> z.s = getScroll(); <BR> z.clear = function() <BR> { <BR> window.clearInterval(z.timer);z.timer=null <BR> }; <BR> z.t=(new Date).getTime(); <BR> z.step = function() <BR> { <BR> var t = (new Date).getTime(); <BR> var p = (t - z.t) / duration; <BR> if (t >= duration + z.t) <BR> { <BR> z.clear(); <BR> window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13); } <BR> else { <BR> st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t; <BR> sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l; <BR> z.scroll(st, sl); <BR> } <BR> }; <BR> z.scroll = function (t, l){window.scrollTo(l, t-20)}; <BR> z.timer = window.setInterval(function(){z.step();},13); <BR> } <BR> <BR> div.test <BR> { <BR> width:400px; margin:5px auto; border:1px solid #ccc; <BR> } <BR> div.test strong <BR> { font-size:16px; background:#fff; border-bottom:1px solid #aaa; margin:0; display:block; padding:5px 0; text-decoration:underline; color:#059B9A; cursor:pointer; } div.test p { height:400px; background:#f1f1f1; margin:0; <BR> } <BR> header_1 --> header_4 header_2 --> header_5 header_3 --> header_6 header_4 --> header_7 header_5 --> header_3 header_6 --> header_2 header_7 --> header_1