vue實(shí)現(xiàn)簡(jiǎn)單無(wú)縫滾動(dòng)效果
本文實(shí)例為大家分享了vue實(shí)現(xiàn)簡(jiǎn)單無(wú)縫滾動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
效果
實(shí)現(xiàn)思路
在vue中如何復(fù)制一份列表出來(lái)呢且不能丟失綁定的事件,很簡(jiǎn)單使用slot插槽,使用兩個(gè)插槽我們就擁有了兩個(gè)列表
<div class="listScroll" ref="box"> ? ? <slot></slot> ? ? <slot></slot> </div>
組件完整代碼
<template> ? <div class="listScroll" ref="box"> ? ? <slot></slot> ? ? <slot></slot> ? </div> </template> <script> export default { ? name: "listScroll", ? created() {}, ? mounted() { ? ? //在盒子內(nèi)容高度小于可視高度時(shí)不滾動(dòng) ? ? if (this.boxHeight < this.ele0.clientHeight) { ? ? ? this.start(this.height); ? ? ? this.setEvet(); ? ? } else { ? ? ? this.isScroll = false; ? ? } ? }, ? props: { ? ? speed: { ? ? ? default: 1, ? ? ? type: Number, ? ? }, ? }, ? computed: { ? ? //第一個(gè)slot ? ? ele0() { ? ? ? return this.$refs.box.children[0]; ? ? }, ? ? //第二個(gè)slot ? ? ele1() { ? ? ? return this.$refs.box.children[1]; ? ? }, ? ? //盒子的可視高度 ? ? boxHeight() { ? ? ? return this.$refs.box.clientHeight; ? ? }, ? }, ? data() { ? ? return { ? ? ? height: 0, ? ? ? isScroll: true, ? ? }; ? }, ? methods: { ? ? //鼠標(biāo)移入停止?jié)L動(dòng) 移出繼續(xù)滾動(dòng) ? ? setEvet() { ? ? ? this.$refs.box.onmouseenter = () => { ? ? ? ? this.isScroll = false; ? ? ? ? // this.height = 0; ? ? ? }; ? ? ? this.$refs.box.onmouseleave = () => { ? ? ? ? this.isScroll = true; ? ? ? ? this.$nextTick(() => { ? ? ? ? ? this.start(this.height); ? ? ? ? }); ? ? ? }; ? ? }, ? ? //滾動(dòng)方法 ? ? start(height) { ? ? ? this.ele0.style = `transform:translateY(-${height}px);`; ? ? ? this.ele1.style = `height:${this.boxHeight}px;transform:translateY(-${height}px);overflow: hidden;`; ? ? ? if (height >= this.ele0.clientHeight) { ? ? ? ? this.height = 0; ? ? ? } else { ? ? ? ? this.height += this.speed; ? ? ? } ? ? ? if (!this.isScroll) return; ? ? ? window.requestAnimationFrame(() => { ? ? ? ? this.start(this.height); ? ? ? }); ? ? }, ? }, }; </script> <style lang="less" scoped> .listScroll { ? overflow: hidden; } .hover { ? overflow-y: auto; } .hide { ? display: none; } </style>
使用
<template> ? <div class="scroll"> ? ? <list-scroll class="box" :speed="1"> ? ? ? <div class="list"> ? ? ? ? <div class="item" v-for="item in list" :key="item.xh"> ? ? ? ? ? <span>{{ item.xh }}</span ? ? ? ? ? ><span>{{ item.label }}</span> ? ? ? ? </div> ? ? ? </div> ? ? </list-scroll> ? </div> </template> <script> import ListScroll from "@/components/listScroll"; export default { ? name: "scroll", ? components: { ListScroll }, ? data() { ? ? return { ? ? ? list: new Array(10) ? ? ? ? .fill(1) ? ? ? ? .map((s, i) => ({ xh: i + 1, label: "hello wrold" })), ? ? }; ? }, }; </script> <style lang="less" scoped> .box { ? height: 300px; } .list { ? padding: 0 10px; ? width: 300px; ? .item { ? ? display: flex; ? ? justify-content: space-between; ? ? padding: 5px 0; ? ? cursor: pointer; ? ? &:hover { ? ? ? background-color: #95a5a6; ? ? } ? } } </style>
至此一個(gè)簡(jiǎn)單的無(wú)縫滾動(dòng)就完成了(vue2和vue3通用)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue條件渲染列表渲染原理示例詳解
- antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式
- vue3實(shí)現(xiàn)無(wú)縫滾動(dòng)組件的示例代碼
- vue實(shí)現(xiàn)無(wú)縫滾動(dòng)的示例詳解
- vue實(shí)現(xiàn)無(wú)縫滾動(dòng)手摸手教程
- vue實(shí)現(xiàn)消息向上無(wú)縫滾動(dòng)效果
- vue實(shí)現(xiàn)無(wú)限消息無(wú)縫滾動(dòng)
- vue實(shí)現(xiàn)列表無(wú)縫滾動(dòng)效果
- vue實(shí)現(xiàn)列表垂直無(wú)縫滾動(dòng)
- vue實(shí)現(xiàn)列表無(wú)縫滾動(dòng)
- el-table動(dòng)態(tài)渲染列、可編輯單元格、虛擬無(wú)縫滾動(dòng)的實(shí)現(xiàn)
相關(guān)文章
vue項(xiàng)目index.html中使用環(huán)境變量的代碼示例
在Vue3中使用環(huán)境變量的方式與Vue2基本相同,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目index.html中使用環(huán)境變量的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02vue項(xiàng)目中引入vue-datepicker插件的詳解
這篇文章主要介紹了vue項(xiàng)目中引入vue-datepicker插件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05vuex 第三方包實(shí)現(xiàn)數(shù)據(jù)持久化的方法
本文主要介紹了vuex 第三方包實(shí)現(xiàn)數(shù)據(jù)持久化的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09如何解決element-ui動(dòng)態(tài)加載級(jí)聯(lián)選擇器默認(rèn)選中問(wèn)題
這篇文章主要介紹了如何解決element-ui動(dòng)態(tài)加載級(jí)聯(lián)選擇器默認(rèn)選中問(wèn)題,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09Vant?Weapp組件picker選擇器初始默認(rèn)選中問(wèn)題
這篇文章主要介紹了Vant?Weapp組件picker選擇器初始默認(rèn)選中問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01vue可ctrl,shift多選,可添加標(biāo)記日歷組件詳細(xì)
這篇文章主要介紹了vue可ctrl,shift多選,可添加標(biāo)記日歷組件詳細(xì),文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09