当前位置:Gxlcms > html代码 > html锚点三种实现方法

html锚点三种实现方法

时间:2021-07-01 10:21:17 帮助过:56人阅读

在网页中经常用到锚点,特别是在比较长的页面中锚点的使用会增加用户体验,现在Gxl网介绍html 锚点三种实现方法

1. 在同一页面中

<a name="add"></a><!-- 定义锚点 -->
<a href="#add">跳转到add</a>
2. 在不同页面中,锚点定位在a.html中,从另外一个页面的链接跳转到这个锚点
<a href="a.html#add">跳转到a.add</a>
3. 点击链接触发js事件,同时跳转到锚点,有两种处理方式:
第一种:
<a href="#add" onclick="add()">触发add函数并跳转到add锚点</a>
第二种:
<p id="pNode"><!-- contents --></p><!-- 假设一个需要跳转到的节点 -->
<a href="#" onclick="document.getElemetnById('pNode').scrollIntoView(true);return false;">通过scrollIntoView实现锚点效果</a>

在html中设置锚点定位有几种方法,使用id定位、使用name定位、使用js定位,这些方法不一定是最全的,只可以参考下

1、使用id定位:

  1. <a href="#1F" name="1F">锚点1</a>
  2. <p name="1F">
  3. <p>
  4. 11111111111
  5. </br>
  6. 11111111111
  7. </br>11111111111
  8. </br>11111111111
  9. </br>11111111111
  10. </br>
  11. </p>
  12. </p>

这样的定位可以针对任何标签来定位。

2、使用name定位:

  1. <a href="#5F">锚点5</a>
  2. </br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>
  3. <a name="5F">1111111</href>

使用name属性只能针对a标签来定位,而对p等其他标签就不能起到定位作用。

3、使用js定位

  1. <li class="" onclick="javascript:document.getElementById('here').scrollIntoView()"></li>

实例:

js 锚点平滑定位

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <head>
  3. <style type="text/css" mce_bogus="1">
  4. p.test {
  5. width: 400px;
  6. margin: 5px auto;
  7. border: 1px solid #ccc;
  8. }
  9. p.test strong {
  10. font-size: 16px;
  11. background: #fff;
  12. border-bottom: 1px solid #aaa;
  13. margin: 0;
  14. display: block;
  15. padding: 5px 0;
  16. text-decoration: underline;
  17. color: #059B9A;
  18. cursor: pointer;
  19. }
  20. p.test p {
  21. height: 400px;
  22. background: #f1f1f1;
  23. margin: 0;
  24. }
  25. </style>
  26. <script type="text/javascript">
  27. function intval(v){
  28. v = parseInt(v);
  29. return isNaN(v) ? 0 : v;
  30. } // ?取元素信息
  31. function getPos(e){
  32. var l = 0;
  33. var t = 0;
  34. var w = intval(e.style.width);
  35. var h = intval(e.style.height);
  36. var wb = e.offsetWidth;
  37. var hb = e.offsetHeight;
  38. while (e.offsetParent) {
  39. l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0);
  40. t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0);
  41. e = e.offsetParent;
  42. }
  43. l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0);
  44. t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0);
  45. return {
  46. x: l,
  47. y: t,
  48. w: w,
  49. h: h,
  50. wb: wb,
  51. hb: hb
  52. };
  53. } // ?取??条信息
  54. function getScroll(){
  55. var t, l, w, h;
  56. if (document.documentElement && document.documentElement.scrollTop) {
  57. t = document.documentElement.scrollTop;
  58. l = document.documentElement.scrollLeft;
  59. w = document.documentElement.scrollWidth;
  60. h = document.documentElement.scrollHeight;
  61. }
  62. else
  63. if (document.body) {
  64. t = document.body.scrollTop;
  65. l = document.body.scrollLeft;
  66. w = document.body.scrollWidth;
  67. h = document.body.scrollHeight;
  68. }
  69. return {
  70. t: t,
  71. l: l,
  72. w: w,
  73. h: h
  74. };
  75. } // ?点(Anchor)?平滑跳?
  76. function scroller(el, duration){
  77. if (typeof el != 'object') {
  78. el = document.getElementById(el);
  79. }
  80. if (!el)
  81. return;
  82. var z = this;
  83. z.el = el;
  84. z.p = getPos(el);
  85. z.s = getScroll();
  86. z.clear = function(){
  87. window.clearInterval(z.timer);
  88. z.timer = null
  89. };
  90. z.t = (new Date).getTime();
  91. z.step = function(){
  92. var t = (new Date).getTime();
  93. var p = (t - z.t) / duration;
  94. if (t >= duration + z.t) {
  95. z.clear();
  96. window.setTimeout(function(){
  97. z.scroll(z.p.y, z.p.x)
  98. }, 13);
  99. }
  100. else {
  101. st = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.y - z.s.t) + z.s.t;
  102. sl = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.x - z.s.l) + z.s.l;
  103. z.scroll(st, sl);
  104. }
  105. };
  106. z.scroll = function(t, l){
  107. window.scrollTo(l, t)
  108. };
  109. z.timer = window.setInterval(function(){
  110. z.step();
  111. }, 13);
  112. }
  113. </script>
  114. </head>
  115. <body>
  116. <p class="test">
  117. <a name="header_1" id="header_1"></a>
  118. <strong onclick="javascript:scroller('header_4', 800);">header_1 --> header_4</strong>
  119. <p>
  120. </p>
  121. </p>
  122. <p class="test">
  123. <a name="header_2" id="header_2"></a>
  124. <strong onclick="javascript:scroller('header_5', 800);">header_2 --> header_5</strong>
  125. <p>
  126. </p>
  127. </p>
  128. <p class="test">
  129. <a name="header_3" id="header_3"></a>
  130. <strong onclick="javascript:scroller('header_6', 800);">header_3 --> header_6</strong>
  131. <p>
  132. </p>
  133. </p>
  134. <p class="test">
  135. <a name="header_4" id="header_4"></a>
  136. <strong onclick="javascript:scroller('header_7', 800);">header_4 --> header_7</strong>
  137. <p>
  138. </p>
  139. </p>
  140. <p class="test">
  141. <a name="header_5" id="header_5"></a>
  142. <strong onclick="javascript:scroller('header_3', 800);">header_5 --> header_3</strong>
  143. <p>
  144. </p>
  145. </p>
  146. <p class="test">
  147. <a name="header_6" id="header_6"></a>
  148. <strong onclick="javascript:scroller('header_2', 800);">header_6 --> header_2</strong>
  149. <p>
  150. </p>
  151. </p>
  152. <p class="test">
  153. <a name="header_7" id="header_7"></a>
  154. <strong onclick="javascript:scroller('header_1', 800);">header_7 --> header_1</strong>
  155. <p>
  156. </p>
  157. </p>
  158. </body>
  159. </html>

以上就是html 锚点三种实现方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行