锚点(Anchor)相信很多人都不陌生,它方便访问者在页面的不同位置快速跳转,直接找到自己感兴趣的内容,如果说 RSS 是整个网站的摘要,那锚点就是一个页面的摘要,通常一个页面内容很多的时候,都会用锚点来定位。 但是锚点也有个问题,通常点击锚点后,页面会立即跳到目标位置,而本文介绍的方法,实现了锚点(Anchor)间平滑跳转,效果非常不错。 复制代码 代码如下: <BR>// 说明 :用 Javascript 实现锚点(Anchor)间平滑跳转 <BR>// 来源 :ThickBox 2.1 <BR>// 整理 :Yanfu Xie [xieyanfu@yahoo.com.cn] <BR>// 日期 :07.01.17 <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>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>// 获取滚动条信息 <BR>function getScroll() <BR>{ <BR>var t, l, w, h; <BR>if (document.documentElement && document.documentElement.scrollTop) { <BR>t = document.documentElement.scrollTop; <BR>l = document.documentElement.scrollLeft; <BR>w = document.documentElement.scrollWidth; <BR>h = document.documentElement.scrollHeight; <BR>} else if (document.body) { <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') { el = document.getElementById(el); } <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(){window.clearInterval(z.timer);z.timer=null}; <BR>z.t=(new Date).getTime(); <BR>z.step = function(){ <BR>var t = (new Date).getTime(); <BR>var p = (t - z.t) / duration; <BR>if (t >= duration + z.t) { <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)}; <BR>z.timer = window.setInterval(function(){z.step();},13); <BR>} <BR> 调用方式: 复制代码 代码如下: scroller(el, duration) el : 目标锚点 ID duration : 持续时间,以毫秒为单位,越小越快 HTML: 复制代码 代码如下: <BR>div.test { <BR>width:400px; <BR>margin:5px auto; <BR>border:1px solid #ccc; <BR>} <BR>div.test strong { <BR>font-size:16px; <BR>background:#fff; <BR>border-bottom:1px solid #aaa; <BR>margin:0; <BR>display:block; <BR>padding:5px 0; <BR>text-decoration:underline; <BR>color:#059B9A; <BR>cursor:pointer; <BR>} <BR>div.test p { <BR>height:400px; <BR>background:#f1f1f1; <BR>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 测试代码: 复制代码 代码如下: