如何让a标签跳转时平滑滚动?

第一种方法:添加css样式scroll-behavior: smooth;

        为一个滚动框指定滚动行为,smooth指的是平滑滚动,一般用于根标签,让视窗内的锚点跳转更平滑。

第二种方法:利用元素的属性scrollTo、scrollIntoView进行滚动设置

滚动到顶部

window.scrollTo({top: 0, left: 0, behavior: "smooth"})

滚动到底部

window.scrollTo({
    top: document.documentElement.offsetHeight,
    left: 0,
    behavior: "smooth",
})

滚动到该元素的可视区域

element.scrollIntoView({ behavior: "smooth", })

 

举例: 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,body {
            /* 方法一 */
            scroll-behavior: smooth;
        }
        .header {
            width: 100%;
            height: 100px;
            background-color: lightblue;
        }
        .header>a {
            width: 100px;
            height: 80px;
            line-height: 80px;
            color: red;
        }
        .main {
            width: 100%;
            background-color: rgb(29, 29, 92);
            /* 方法一 */
            /* scroll-behavior: smooth; */
        }
        .main>a {
            display: block;
            width: 100%;
            height: 1000px;
            color: #fff;
        }    
        .footer {
            width: 100%;
            height: 80px;
            background-color: rgb(3, 3, 26);
        }
    </style>
</head>

<body>
    <!-- 平滑滚动样式 -->
    <header class="header">
        <a href="#a1">1</a>
        <a href="#a2">2</a>
        <!-- <a href="#a3">3</a> -->
        <a onclick="smoothScroll(document.getElementById('a3'))">3</a>
        <button onclick="test()">测试</button>
        <button onclick="scrollToBottom()">滚到底</button>
    </header>
    <main class="main">
        <a href="" id="a1">1</a>
        <a href="" id="a2">2</a>
        <a href="" id="a3">3</a>
    </main>
    <footer class="footer">
        <button onclick="scrollToTop()">滚到顶</button>
    </footer>
    <script>
        // 方法二
        // 补充页面滚动顶部
            const scrollToTop = () => {
                window.scrollTo({ top: 0, left: 0, behavior: "smooth" });
            }
        // 页面滚到底部
            const scrollToBottom = () => {
                window.scrollTo({
                    top: document.documentElement.offsetHeight,
                    left: 0,
                    behavior: "smooth",
                })
            }
        // 页面滚动到可见区域
            const smoothScroll = (element) => {
                element.scrollIntoView({
                    behavior: "smooth",
                })
            }
       //测试
            function test() {
                const a3 = document.querySelector("#a3");
                a3.scrollIntoView({
                    behavior: 'smooth'
            });
        }
    </script>
</body>

</html>

### HTML A标签实现页面内跳转指定位置 为了实现在同一页面内的锚点跳转,可以利用`<a>`标签的`href`属性配合其他HTML元素的`id`属性来完成。当用户点击带有特定`href`值(以`#`开头并跟随目标元素ID)的超链接,浏览器会自动滚动至该ID所关联的内容区域。 #### 基础示例 下面是一个简单的例子展示如何创建这样的内部导航: ```html <a href="#section1">前往第一部分</a> <!-- ... 中间有大量内容 --> <div id="section1"> <h2>这是第一部分内容</h2> <!-- 这里放置具体的信息 --> </div> ``` 在这个案例中,通过点击“前往第一部分”的链接,页面将会平滑地移动到具有`id="section1"`的`<div>`容器所在的位置[^1]。 #### 平滑滚动效果增强用户体验 为了让这种跳转更加友好,默认情况下大多数现代浏览器支持CSS中的`scroll-behavior`属性,将其应用于`body`样式上可使每次发生锚点变化都伴有柔和过渡而非突兀跳跃: ```css /* 添加到全局样式表 */ body { scroll-behavior: smooth; } ``` 这样不仅提升了视觉上的连续感,也改善了用户的浏览体验[^2]。 #### 动态获取下一匹配项进行跳转 对于更复杂的需求比如按顺序访问相同类名下的不同实例,则可以通过JavaScript编写逻辑处理程序,在首次触发后记录当前位置以便于后续操作找到下一个同类别对象继续执行相似动作。这里给出一段基于此思路的基础脚本片段作为参考: ```javascript document.querySelectorAll('.relationTradeErr').forEach((item, index) => { item.addEventListener('click', function () { let elements = document.getElementsByClassName('relationTradeErr'); window.location.href = `#${elements[(index + 1) % elements.length].id}`; }); }); ``` 上述代码实现了每当`.relationTradeErr`被点击一次就切换到列表里的下一项直至循环结束再回到起点的效果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.MUXIAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值