網站首頁 編程語言 正文
el-scrollbar 滾動條組件用于優化頁內滾動條的UI效果,使用時必須指定高度!
/*el-scrollbar 必須指定高度*/
.scrollMenuBox {
height: 200px;
width: 100px;
border: 1px solid red;
}
控制el-scrollbar內滾動條的方法與控制頁面的滾動條的方法基本一樣,只是獲取el-scrollbar節點的滾動條時,需使用? this.$refs.scrollMenuRef.wrap
<el-scrollbar ref="scrollMenuRef" class="scrollMenuBox">
<p v-for="i in titleTotal" :key="i">標題{{i}}</p>
</el-scrollbar>
獲取 el-scrollbar 內滾動條向下滾動的距離
this.$refs.scrollMenuRef.wrap.scrollTop
控制 el-scrollbar 內滾動條滾動到指定位置
?比如,向下滾動 100px
this.$refs.scrollMenuRef.wrap.scrollTop = 100
el-scrollbar 內內容的高度
this.$refs.scrollMenuRef.wrap.scrollHeight
監聽 el-scrollbar 內滾動條的滾動
this.$refs.scrollMenuRef.wrap.addEventListener("scroll", this.scrollMenu);
隱藏水平滾動條
?使用 scoped 時,需用 /deep/ 實現樣式穿透
/deep/ .el-scrollbar__wrap {
overflow-x: hidden;
}
el-scrollbar 內滾動條跟隨頁面一起滾動
mounted() {
// 監聽瀏覽器/頁面滾動條的滾動
window.addEventListener("scroll", this.scrollContent);
},
// 頁面滾動時觸發
scrollContent() {
// scrollTop為顯示屏頂部與整個文檔頂部間的距離
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
// 頁面滾動條滾動時,el-scrollbar內滾動條按比例跟隨一起滾動
this.$refs.scrollMenuRef.wrap.scrollTop = scrollTop / document.body.scrollHeight * this.$refs.scrollMenuRef.wrap.scrollHeight
},
?完整范例代碼
<template>
<div style="padding: 20px">
<div class="menuBox">
<button @click="scrollTo(25)">目錄滾動到標題25</button>
<br>
<button @click="getScrollTop">獲取目錄內滾動條向下滾動的距離</button>
<hr>
<p>目錄</p>
<el-scrollbar ref="scrollMenuRef" class="scrollMenuBox">
<p v-for="i in titleTotal" :key="i">標題{{i}}</p>
</el-scrollbar>
</div>
<div class="contentBox">
<p v-for="i in rowTotal" :key="i">第{{i}}行</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
// 總行數
titleTotal: 50,
// 總行數
rowTotal: 200
}
},
mounted() {
// 監聽瀏覽器/頁面滾動條的滾動
window.addEventListener("scroll", this.scrollContent);
// 監聽el-scrollbar內滾動條的滾動
this.$refs.scrollMenuRef.wrap.addEventListener("scroll", this.scrollMenu);
},
methods: {
// el-scrollbar內滾動條滾動時,打印滾動條向下滾動的距離
scrollMenu() {
console.log(this.$refs.scrollMenuRef.wrap.scrollTop)
},
// 頁面滾動時觸發
scrollContent() {
// scrollTop為顯示屏頂部與整個文檔頂部間的距離
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
// 頁面滾動條滾動時,el-scrollbar內滾動條按比例跟隨一起滾動
this.$refs.scrollMenuRef.wrap.scrollTop = scrollTop / document.body.scrollHeight * this.$refs.scrollMenuRef.wrap.scrollHeight
},
// el-scrollbar內滾動條滾動到指定的標題號
scrollTo(titleNo) {
this.$refs.scrollMenuRef.wrap.scrollTop = (titleNo - 1) / this.titleTotal * this.$refs.scrollMenuRef.wrap.scrollHeight
},
// 獲取el-scrollbar內滾動條向下滾動的距離
getScrollTop() {
alert(this.$refs.scrollMenuRef.wrap.scrollTop)
}
},
}
</script>
<style scoped>
/*目錄懸浮*/
.menuBox {
position: fixed;
}
/*內容居中*/
.contentBox {
width: 60%;
margin: auto;
}
/*el-scrollbar 必須指定高度*/
.scrollMenuBox {
height: 200px;
width: 100px;
border: 1px solid red;
}
/*隱藏水平滾動條*/
/deep/ .el-scrollbar__wrap {
overflow-x: hidden;
}
</style>
?
原文鏈接:https://blog.csdn.net/weixin_41192489/article/details/113875367
相關推薦
- 2023-05-06 python?字典的概念敘述和使用方法_python
- 2022-10-06 C++?pimpl機制詳細講解_C 語言
- 2022-07-29 Docker容器的加載分層原理及commit鏡像_docker
- 2024-04-08 Spring在多線程環境下如何確保事務一致性
- 2022-07-12 CSS樣式:行內元素 盒子模型
- 2022-06-30 C語言實例上手深入理解操作符的使用_C 語言
- 2022-05-15 Python獲取網絡圖片和視頻的示例代碼_python
- 2024-02-26 openJDK awt 字體支持
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支