现在的位置: 首页 > 综合 > 正文

[转]关于锚点

2012年10月20日 ⁄ 综合 ⁄ 共 2875字 ⁄ 字号 评论关闭

关键字: 锚点 平滑 定位

对于定位到一个锚点,最常见的方法就是在url后面加上“#和锚点的name值”, 
下面先介绍一种如何采用Javascript定位锚点的方法: 
window.location.hash="anchorname" 
比如一个锚点:<a name="anchor1"></a> 
那么采用window.location.hash="anchor1"就可以定位到锚点处。 

下面介绍一种平滑移动到指定锚点的方法,此方法是采自ThickBox: 
js代码:

 

Js代码 复制代码
  1. // 转换为数字   
  2. function intprase(v){  
  3.     v = parseInt(v);  
  4.     return isNaN(v) ? 0 : v;  
  5. }  
  6.   
  7. // 获取元素信息   
  8. function getInfo(e){  
  9.     var l = 0;  
  10.     var t = 0;  
  11.     var w = intprase(e.style.width);  
  12.     var h = intprase(e.style.height);  
  13.     var wb = e.offsetWidth;  
  14.     var hb = e.offsetHeight;  
  15.     while (e.offsetParent) {  
  16.         l += e.offsetLeft + (e.currentStyle ? intprase(e.currentStyle.borderLeftWidth) : 0);  
  17.         t += e.offsetTop + (e.currentStyle ? intprase(e.currentStyle.borderTopWidth) : 0);  
  18.         e = e.offsetParent;  
  19.     }  
  20.     l += e.offsetLeft + (e.currentStyle ? intprase(e.currentStyle.borderLeftWidth) : 0);  
  21.     t += e.offsetTop + (e.currentStyle ? intprase(e.currentStyle.borderTopWidth) : 0);  
  22.     return {  
  23.         x: l,  
  24.         y: t,  
  25.         w: w,  
  26.         h: h,  
  27.         wb: wb,  
  28.         hb: hb  
  29.     };  
  30. }  
  31.   
  32. // 获取滚动条信息   
  33. function getScroll(){  
  34.     var t, l, w, h;  
  35.     if (document.documentElement && document.documentElement.scrollTop) {  
  36.         t = document.documentElement.scrollTop;  
  37.         l = document.documentElement.scrollLeft;  
  38.         w = document.documentElement.scrollWidth;  
  39.         h = document.documentElement.scrollHeight;  
  40.     }  
  41.     else   
  42.         if (document.body) {  
  43.             t = document.body.scrollTop;  
  44.             l = document.body.scrollLeft;  
  45.             w = document.body.scrollWidth;  
  46.             h = document.body.scrollHeight;  
  47.         }  
  48.     return {  
  49.         t: t,  
  50.         l: l,  
  51.         w: w,  
  52.         h: h  
  53.     };  
  54. }  
  55.   
  56. // 锚点(Anchor)间平滑跳转   
  57. function glide(el, duration){  
  58.     if (typeof el != 'object') {  
  59.         el = document.getElementById(el);  
  60.     }  
  61.     if (!el)   
  62.         return;  
  63.     var z = this;  
  64.     z.el = el;  
  65.     z.p = getInfo(el);  
  66.     z.s = getScroll();  
  67.     z.clear = function(){  
  68.         window.clearInterval(z.timer);  
  69.         z.timer = null  
  70.     };  
  71.     z.t = (new Date).getTime();  
  72.     z.step = function(){  
  73.         var t = (new Date).getTime();  
  74.         var p = (t - z.t) / duration;  
  75.         if (t >= duration + z.t) {  
  76.             z.clear();  
  77.             window.setTimeout(function(){  
  78.                 z.scroll(z.p.y, z.p.x)  
  79.             }, 13);  
  80.         }  
  81.         else {  
  82.             st = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.y - z.s.t) + z.s.t;  
  83.             sl = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.x - z.s.l) + z.s.l;  
  84.             z.scroll(st, sl);  
  85.         }  
  86.     };  
  87.     z.scroll = function(t, l){  
  88.         window.scrollTo(l, t)  
  89.     };  
  90.     z.timer = window.setInterval(function(){  
  91.         z.step();  
  92.     }, 13);  
  93. }  

 

 

经过测试,这段JS代码在IE和FF中均可运行。

具体使用方法:glide(anchorid, millisecond)

其中,anchorid为锚点的id,millisecond为移动到指定锚点的毫秒数。

抱歉!评论已关闭.