惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 田了你

[ZF]开心一下 - 田了你 [转]Java jdbc数据库连接池总结! - 田了你 一台机器部署N个server步骤 [转]软件开发者面试百问 09年的第一篇日志比已往的时候来得更早一些 - 田了你 - 博客园 从今天开始每天写一篇日志咯 Telerik RadGrid 翻页问题解决 - 田了你 果然是本命年 javascript注册事件的四种方式 - 田了你 - 博客园 Tomcat5.x的项目部署 又被老板欺骗了 - 田了你 .net的优点变缺点(郁闷了一天) - 田了你 2007.2.1 -----2007.4.1安排计划 - 田了你 关于NTKO_office的操作(从数据库中提取数据,写入到NTKO_office_Word中) - 田了你 [转]JS代码格式化工具(附源代码) 层的拖动 获得控件在页面上的绝对位置的方法 解决SQL2000乱码问题 ref与out
[转]关于锚点 - 田了你 - 博客园
田了你 · 2008-11-27 · via 博客园 - 田了你

关键字: 锚点 平滑 定位

对于定位到一个锚点,最常见的方法就是在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.   
  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为移动到指定锚点的毫秒数。