首页 web前端 html教程 html 锚点三种实现方法

html 锚点三种实现方法

Apr 03, 2017 pm 04:57 PM
html 锚点

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

1. 在同一页面中


跳转到add
2. 在不同页面中,锚点定位在a.html中,从另外一个页面的链接跳转到这个锚点
跳转到a.add
3. 点击链接触发js事件,同时跳转到锚点,有两种处理方式:
第一种:
触发add函数并跳转到add锚点
第二种:


通过scrollIntoView实现锚点效果  

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

1、使用id定位: 

<a href="#1F" name="1F">锚点1</a> 
<p name="1F"> 
<p> 
11111111111 
</br> 
11111111111 
</br>11111111111 
</br>11111111111 
</br>11111111111 
</br> 
</p> 
</p>
登录后复制

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

2、使用name定位:

<a href="#5F">锚点5</a> 
</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br> 
<a name="5F">1111111</href>
登录后复制

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

3、使用js定位

<li class="" onclick="javascript:document.getElementById(&#39;here&#39;).scrollIntoView()"></li>
登录后复制

实例:

js 锚点平滑定位

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <head>
        <style type="text/css" mce_bogus="1">
            p.test {
                width: 400px;
                margin: 5px auto;
                border: 1px solid #ccc;
            }
             
            p.test strong {
                font-size: 16px;
                background: #fff;
                border-bottom: 1px solid #aaa;
                margin: 0;
                display: block;
                padding: 5px 0;
                text-decoration: underline;
                color: #059B9A;
                cursor: pointer;
            }
             
            p.test p {
                height: 400px;
                background: #f1f1f1;
                margin: 0;
            }
        </style>
        <script type="text/javascript">
             
            function intval(v){
                v = parseInt(v);
                return isNaN(v) ? 0 : v;
            } // ?取元素信息   
            function getPos(e){
                var l = 0;
                var t = 0;
                var w = intval(e.style.width);
                var h = intval(e.style.height);
                var wb = e.offsetWidth;
                var hb = e.offsetHeight;
                while (e.offsetParent) {
                    l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0);
                    t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0);
                    e = e.offsetParent;
                }
                l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0);
                t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0);
                return {
                    x: l,
                    y: t,
                    w: w,
                    h: h,
                    wb: wb,
                    hb: hb
                };
            } // ?取??条信息   
            function getScroll(){
                var t, l, w, h;
                if (document.documentElement && document.documentElement.scrollTop) {
                    t = document.documentElement.scrollTop;
                    l = document.documentElement.scrollLeft;
                    w = document.documentElement.scrollWidth;
                    h = document.documentElement.scrollHeight;
                }
                else
                    if (document.body) {
                        t = document.body.scrollTop;
                        l = document.body.scrollLeft;
                        w = document.body.scrollWidth;
                        h = document.body.scrollHeight;
                    }
                return {
                    t: t,
                    l: l,
                    w: w,
                    h: h
                };
            } // ?点(Anchor)?平滑跳?   
            function scroller(el, duration){
                if (typeof el != &#39;object&#39;) {
                    el = document.getElementById(el);
                }
                if (!el)
                    return;
                var z = this;
                z.el = el;
                z.p = getPos(el);
                z.s = getScroll();
                z.clear = function(){
                    window.clearInterval(z.timer);
                    z.timer = null
                };
                z.t = (new Date).getTime();
                z.step = function(){
                    var t = (new Date).getTime();
                    var p = (t - z.t) / duration;
                    if (t >= duration + z.t) {
                        z.clear();
                        window.setTimeout(function(){
                            z.scroll(z.p.y, z.p.x)
                        }, 13);
                    }
                    else {
                        st = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.y - z.s.t) + z.s.t;
                        sl = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.x - z.s.l) + z.s.l;
                        z.scroll(st, sl);
                    }
                };
                z.scroll = function(t, l){
                    window.scrollTo(l, t)
                };
                z.timer = window.setInterval(function(){
                    z.step();
                }, 13);
            }
        </script>
    </head>
    <body>
        <p class="test">
            <a name="header_1" id="header_1"></a>
            <strong onclick="javascript:scroller(&#39;header_4&#39;, 800);">header_1 --> header_4</strong>
            <p>
            </p>
        </p>
        <p class="test">
            <a name="header_2" id="header_2"></a>
            <strong onclick="javascript:scroller(&#39;header_5&#39;, 800);">header_2 --> header_5</strong>
            <p>
            </p>
        </p>
        <p class="test">
            <a name="header_3" id="header_3"></a>
            <strong onclick="javascript:scroller(&#39;header_6&#39;, 800);">header_3 --> header_6</strong>
            <p>
            </p>
        </p>
        <p class="test">
            <a name="header_4" id="header_4"></a>
            <strong onclick="javascript:scroller(&#39;header_7&#39;, 800);">header_4 --> header_7</strong>
            <p>
            </p>
        </p>
        <p class="test">
            <a name="header_5" id="header_5"></a>
            <strong onclick="javascript:scroller(&#39;header_3&#39;, 800);">header_5 --> header_3</strong>
            <p>
            </p>
        </p>
        <p class="test">
            <a name="header_6" id="header_6"></a>
            <strong onclick="javascript:scroller(&#39;header_2&#39;, 800);">header_6 --> header_2</strong>
            <p>
            </p>
        </p>
        <p class="test">
            <a name="header_7" id="header_7"></a>
            <strong onclick="javascript:scroller(&#39;header_1&#39;, 800);">header_7 --> header_1</strong>
            <p>
            </p>
        </p>
    </body>
</html>
登录后复制

以上是html 锚点三种实现方法的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

HTML 中的表格边框 HTML 中的表格边框 Sep 04, 2024 pm 04:49 PM

HTML 表格边框指南。在这里,我们以 HTML 中的表格边框为例,讨论定义表格边框的多种方法。

HTML 中的嵌套表 HTML 中的嵌套表 Sep 04, 2024 pm 04:49 PM

这是 HTML 中嵌套表的指南。这里我们讨论如何在表中创建表以及相应的示例。

HTML 左边距 HTML 左边距 Sep 04, 2024 pm 04:48 PM

HTML 左边距指南。在这里,我们讨论 HTML margin-left 的简要概述及其示例及其代码实现。

HTML 表格布局 HTML 表格布局 Sep 04, 2024 pm 04:54 PM

HTML 表格布局指南。在这里,我们详细讨论 HTML 表格布局的值以及示例和输出。

HTML 输入占位符 HTML 输入占位符 Sep 04, 2024 pm 04:54 PM

HTML 输入占位符指南。在这里,我们讨论 HTML 输入占位符的示例以及代码和输出。

在 HTML 中移动文本 在 HTML 中移动文本 Sep 04, 2024 pm 04:45 PM

HTML 中的文本移动指南。在这里我们讨论一下marquee标签如何使用语法和实现示例。

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在这里我们还分别讨论了 HTML 有序列表和类型的介绍以及它们的示例

HTML onclick 按钮 HTML onclick 按钮 Sep 04, 2024 pm 04:49 PM

HTML onclick 按钮指南。这里我们分别讨论它们的介绍、工作原理、示例以及各个事件中的onclick事件。

See all articles