網站首頁 Vue 正文
本文實例為大家分享了vue實現左右點擊滾動,效果如圖
涉及功能點
1、在created中使用r e f s 結 合 refs結合refs結合nextTick仍然無法獲取到元素的問題:添加定時器
2、左右按鈕是否可點擊根據數據以及當前分辨率可放下的個數確認
3、可適應不同分辨率下的情況
代碼
<!-- ?--> <template> ? <div> ? ? <div class="ProgressBoxTool" v-if="progressList && progressList.length"> ? ? ? <div class="processBox"> ? ? ? ? <div :class="currentClickNumber > 0 ? 'arrow' : 'arrow arrowOpacity'" @click="fnPrev()"> ? ? ? ? ? <img :src="arrowL" alt="" /> ? ? ? ? </div> ? ? ? ? <div class="fixedBox" :ref="`fixedBox`"> ? ? ? ? ? <div ? ? ? ? ? ? class="centerScroll" ? ? ? ? ? ? :style=" ? ? ? ? ? ? ? `width:${signleWidth * ? ? ? ? ? ? ? ? progressList.length}px;transform:translate(${scrollResultWidth}px,0);transition:1s;` ? ? ? ? ? ? " ? ? ? ? ? > ? ? ? ? ? ? <div ? ? ? ? ? ? ? class="signleTab" ? ? ? ? ? ? ? v-for="(itemP, indexP) in progressList" ? ? ? ? ? ? ? :key="indexP + 'progress'" ? ? ? ? ? ? > ? ? ? ? ? ? ? <div class="leftIcon"> ? ? ? ? ? ? ? ? <img class="pregressIcon" :src="icon" alt="" /> ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? <!-- 最后一個不展示箭頭 --> ? ? ? ? ? ? ? <img ? ? ? ? ? ? ? ? v-if="progressList.length > indexP + 1" ? ? ? ? ? ? ? ? :src="iconArrow" ? ? ? ? ? ? ? ? alt="" ? ? ? ? ? ? ? ? class="arrowSquare" ? ? ? ? ? ? ? /> ? ? ? ? ? ? </div> ? ? ? ? ? </div> ? ? ? ? </div> ? ? ? ? <div :class="noScrollRight ? 'arrow' : 'arrow arrowOpacity'" @click="fnNext(activeName)"> ? ? ? ? ? <img :src="arrowR" alt="" /> ? ? ? ? </div> ? ? ? </div> ? ? </div> ? </div> </template> <script> import arrowL from '@/assets/images/emergency/arrowL.png'; import arrowR from '@/assets/images/emergency/arrowR.png'; import icon from '@/assets/images/emergency/icon.png'; import iconArrow from '@/assets/images/emergency/iconArrow.png'; export default { ? components: {}, ? data() { ? ? return { ? ? ? progressList: [ ? ? ? ? { type: '1' }, ? ? ? ? { type: '2' }, ? ? ? ? { type: '1' }, ? ? ? ? { type: '2' }, ? ? ? ? { type: '1' }, ? ? ? ? { type: '2' }, ? ? ? ? { type: '1' }, ? ? ? ? { type: '2' }, ? ? ? ? { type: '1' }, ? ? ? ? { type: '2' } ? ? ? ], ? ? ? arrowL, ? ? ? arrowR, ? ? ? icon, ? ? ? iconArrow, ? ? ? currentProgressId: '', ? ? ? scrollResultWidth: 0, //transform滾動的距離 ? ? ? signleWidth: 215, //單個流程的寬度 ? ? ? activeName: 0, ? ? ? currentClickNumber: 0, ? ? ? noScrollRight: true ? ? }; ? }, ? created() { ? ? this.$nextTick(() => { ? ? ? setTimeout(() => { ? ? ? ? this.initgoRightArrow(); ? ? ? }); ? ? }); ? }, ? methods: { ? ? //初始化判斷是否可以向右滾動 ? ? initgoRightArrow() { ? ? ? const currentScrollWidth = this.$refs[`fixedBox`].clientWidth; ? ? ? const canNumber = Math.floor(currentScrollWidth / this.signleWidth); //可以放下的個數 ? ? ? //如果最后一個流程圖標已經展示出來,則停止滾動 ? ? ? if (this.currentClickNumber + canNumber >= this.progressList.length) { ? ? ? ? this.noScrollRight = false; ? ? ? ? return; ? ? ? } ? ? }, ? ? //點擊上一個 ? ? fnPrev() { ? ? ? //如果右點擊的次數大于0,才可以左滾 ? ? ? if (this.currentClickNumber > 0) { ? ? ? ? this.currentClickNumber -= 1; ? ? ? ? this.noScrollRight = true; ? ? ? ? this.fnScrollWidth('reduce'); ? ? ? } else { ? ? ? ? return false; ? ? ? } ? ? }, ? ? //點擊下一個 ? ? fnNext() { ? ? ? const currentScrollWidth = this.$refs[`fixedBox`].clientWidth; ? ? ? const canNumber = Math.floor(currentScrollWidth / this.signleWidth); //可以放下的個數 ? ? ? //如果最后一個流程圖標已經展示出來,則停止滾動 ? ? ? if (this.currentClickNumber + canNumber >= this.progressList.length) { ? ? ? ? return; ? ? ? } ? ? ? //說明放不下有滾動條 ? ? ? if (this.progressList.length > canNumber) { ? ? ? ? this.currentClickNumber += 1; ? ? ? ? if (this.currentClickNumber + canNumber >= this.progressList.length) { ? ? ? ? ? this.noScrollRight = false; ? ? ? ? } ? ? ? ? this.fnScrollWidth('add'); ? ? ? } ? ? }, ? ? //translate的寬度 ? ? fnScrollWidth(type) { ? ? ? let result = 0; ? ? ? if (type === 'reduce') { ? ? ? ? result = 215; ? ? ? } else if (type === 'add') { ? ? ? ? result = -215; ? ? ? } else { ? ? ? ? result = 0; ? ? ? } ? ? ? this.scrollResultWidth += result; ? ? }, ? } }; </script> <style lang="scss" scoped> //中間的時間發展部分 .processBox { ? display: flex; ? align-items: center; ? justify-content: space-between; ? .arrow { ? ? width: 60px; ? ? cursor: pointer; ? } ? .arrowOpacity { ? ? cursor: default; ? ? opacity: 0.4; ? } ? .fixedBox { ? ? flex: 1; ? ? overflow: hidden; ? } ? .centerScroll { ? ? // flex: 1; ? ? box-sizing: border-box; ? ? padding: 20px 0; ? ? white-space: nowrap; ? ? // width: calc(100% - 120px); ? ? // overflow-x: auto; ? ? .signleTab { ? ? ? width: 215px; ? ? ? position: relative; ? ? ? display: inline-block; ? ? ? .leftIcon { ? ? ? ? width: 150px; ? ? ? ? text-align: center; ? ? ? ? cursor: pointer; ? ? ? ? & > .pregressIcon { ? ? ? ? ? width: 60px; ? ? ? ? ? height: 60px; ? ? ? ? } ? ? ? } ? ? ? & > .arrowSquare { ? ? ? ? position: absolute; ? ? ? ? top: 25px; ? ? ? ? right: 0; ? ? ? } ? ? } ? } } </style>
原文鏈接:https://blog.csdn.net/baidu_41604826/article/details/121957250
相關推薦
- 2023-05-15 GoLang中的加密方法小結_Golang
- 2022-09-14 Android?Activity通用懸浮可拖拽View封裝的思路詳解_Android
- 2022-09-15 python安裝whl文件的實戰步驟_python
- 2022-06-10 ASP.NET?Core為Ocelot網關配置Swagger_實用技巧
- 2022-03-19 關于docker中?WSL?配置與修改問題_docker
- 2022-05-06 C#迭代器方法介紹_C#教程
- 2022-10-13 pytorch和tensorflow計算Flops和params的詳細過程_python
- 2022-08-01 詳解Python圖像形態學處理(開運算,閉運算,梯度運算)_python
- 最近更新
-
- 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同步修改后的遠程分支