迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中设置滚动位置

作者:迹忆客 最近更新:2024/03/18 浏览次数:

本教程将介绍如何在 JavaScript 中设置界面中的滚动位置。


在 JavaScript 中使用 scrollTo() 方法设置滚动位置

在本例中,我们将首先设置 body 元素的尺寸。下一步,我们将使用带有 onclick 属性的 button 元素来触发函数 scrollL()

我们还将将此按钮的位置设置为固定。我们将在脚本标签中添加函数定义,并且会有命令 window.scrollTo()

scrollTo() 方法将操作窗口对象。具体来说,它会左跳到 20 个单位,因为我们已经设置了这个值。

前一个参数用于水平移动,也称为左滚动,后者用于垂直移动,顶部滚动。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
    <style>
        body {
        width: 2000px;
        height: 2000px;}
    </style>
</head>
<body>
    <p>Example of ScrollTo</p>
    <button onclick="scrollL()" style="position:fixed">Scroll Horizontally!</button><br><br>
    <script>
        function scrollL() {
        window.scrollTo(20, 0);
    }
    </script>
</body>
</html>

输出:

由于我们已将其设置为最多移动 20 个单位,因此窗口对象将在输出中水平移动。

由于 scrollTo() 方法只执行一次任务,因此该按钮将使视口仅移动一次。

正如我们所见,我们将视口框架从 (0,0) 移动到 (20,0)。同样,我们可以设置垂直位移的值。


在 JavaScript 中使用 scrollBy() 方法设置滚动位置

scrollBy() 方法的工作方式与 scrollTo() 方法类似。但是在这种情况下,移动不是基于一步的;相反,它会尽可能多地滚动。

在下面的代码中,我们将水平值设置为 10。这意味着框架将从 (0,0) 移动到 (10,0)。

此外,每次按下按钮元素后,我们将获得 10 个单位 (px) 的位移。

检查提供的示例以获得更好的理解。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
    <style>
        body {
            width: 2000px;
        }
    </style>
</head>
<body>
    <p>Example of ScrollBy</p>
    <button onclick="scrollL()" style="position:fixed">Scroll Horizontally!</button><br><br>
    <script>
        function scrollL() {
        window.scrollBy(10, 0);
        }
    </script>
</body>
</html>

输出:


使用 scrollLeftscrollTop 属性以及 HTML 属性 onscroll

我们将在希望添加滚动位置的元素上使用这两个属性。

在我们的示例中,父 div=div1 的维度小于子 div=content。这会造成溢出,所以我们必须设置属性。

另外,请注意调用函数 keepUpTheScroll() 的 HTML 属性 onscroll

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
    <style>
        #div1 {
            height: 250px;
            width: 250px;
            overflow: auto;
        }
        #content{
            width:500px;
            height:1250px;
            background:black;
            color:white;
        }
    </style>
</head>
<body>
    <div id="div1" onscroll="keepUpTheScroll()">
    <div id="content"><br>&nbsp;&nbsp;&nbsp;Scroll!</div>
    </div>
    <p id="show"></p>
    <script>
        function keepUpTheScroll(){
            var div = document.getElementById('div1');
            var x = div.scrollLeft;
            var y = div.scrollTop;
            document.getElementById ("show").innerHTML = "Horizontally: " + x + "px<br>Vertically: " + y + "px";
        }
    </script>
</body>
</html>

输出:

scrollLeftscrollTop 返回确切的帧位置,也就是相对于其他维度的滚动位置。在我们的输出中,我们得到水平滚动 9.6 和垂直滚动 14.39。

尽管 scrollTo()scrollBy() 需要最少的代码行来实现,但这些属性会将整个面板转移到你想要的位置。


在 JavaScript 中使用 eventListener 设置滚动位置

对于 addEventListener(),我们将再次使用 scrollLeftscrollTop 属性来处理滚动位置值。该方法使用滚动来应用功能。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
    <style>
        #div1 {
            height: 250px;
            width: 250px;
            overflow: auto;
        }
        #content{
            width:500px;
            height:1250px;
            background:purple;
            color:white;
        }
    </style>
</head>
<body>
    <div id="div1" onscroll="keepUpTheScroll()">
    <div id="content"><br>&nbsp;&nbsp;&nbsp;Scroll!</div>
    </div>
    <p id="show"></p>
    <script>
        var div = document.getElementById('div1');
        var show = document.getElementById('show');
        div.addEventListener("scroll", event =>{
            show.innerHTML = `scrollTop: ${div.scrollTop} <br>
            scrollLeft: ${div.scrollLeft}`;
        });
    </script>
</body>
</html>

输出:

在示例中,我们考虑了 div 元素并对其进行操作以将水平移动值设为 4,将垂直移动值设为 9.6。

然而,我们对 scrollLeftscrollTop 使用了 jQuery 命令的一小部分。另一种方法是使用功能类似于 scrollTo()scroll() 方法。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便