JavaScript 中的 scrollY 属性的作用是什么?
htmljavascriptprogramming scripts更新于 2026/1/13 16:12:17
JavaScript 中的 scrollY 属性的作用与 pageYoffset 属性相同。如果您想要获取文档从窗口左上角滚动到的像素,则使用 scrollY 属性获取垂直像素。
示例
您可以尝试运行以下代码来了解如何在 JavaScript 中使用 scrollY 属性。
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: yellow;
height: 1500px;
width: 1500px;
}
</style>
</head>
<body>
<script>
function scrollFunc() {
window.scrollBy(200, 200);
alert("Vertical: " + window.scrollY);
}
</script>
<button onclick="scrollFunc()" style="position:fixed;">Scroll</button>
<br><br><br>
<div>
</div>
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

