前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue实现顶部滚动条

Vue实现顶部滚动条

作者头像
世间万物皆对象
发布2024-03-20 20:07:44
740
发布2024-03-20 20:07:44
举报
文章被收录于专栏:startstart

当前项目使用vue3+vite搭建的

index.html
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Vite + Vue + TS</title>
  <style>
    .progress {
      position: fixed;
      top: 0;
      height: 3px;
      background: orange;
    }
  </style>
</head>

<body>
  <div class="progress"></div>
  <div id="app"></div>
  <script type="module" src="/src/main.ts"></script>
  <script>
    (function () {
      let bodyHeight = document.documentElement.scrollHeight; //页面高度
      let winHeight = document.body.clientHeight; //浏览器高度
      let scroolHeight = bodyHeight - winHeight; //滚动高度
      window.onscroll = function () {
        let scrollTop = document.documentElement.scrollTop; //获取滚动条
        document.querySelector('.progress').style.width = (scrollTop / scroolHeight) * 100 + "%";//计算width
      }
    })()
  </script>
</body>

</html>
demo.vue
代码语言:javascript
复制
<template>
	<div class="box">
		<p>1111111111111111111111111111</p>
	</div>
</template>

<script setup lang="ts"></script>

<style scoped>
p {
	font-size: 24px;
	height: 2000px;
}
</style>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • index.html
  • demo.vue
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档