vue-seamless-scroll 實現(xiàn)簡單自動無縫滾動且添加對應(yīng)點擊事件的簡單整理
Vue 之 vue-seamless-scroll 實現(xiàn)簡單自動無縫滾動,且添加對應(yīng)點擊事件的簡單整理
一、簡單介紹
Vue 開發(fā)的一些知識整理,方便后期遇到類似的問題,能夠及時查閱使用。
本節(jié)介紹,vue 中添加 vue-seamless-scroll,簡單實現(xiàn)自動無縫滾動的效果,并對應(yīng)添加點擊事件 ,如果有不足之處,歡迎指出,或者你有更好的方法,歡迎留言。
vue-seamless-scroll 是一個基于Vue.js的簡單無縫滾動組件, 基于requestAnimationFrame實現(xiàn),配置多滿足多樣需求。目前支持上下左右無縫滾動,單步滾動,以及支持水平方向的手動切換功能。
vue-seamless-scroll 配置項:
| key | description | default | val |
|---|---|---|---|
step | 數(shù)值越大速度滾動越快 | 1 | Number |
limitMoveNum | 開啟無縫滾動的數(shù)據(jù)量 | 5 | Number |
hoverStop | 是否啟用鼠標hover控制 | true | Boolean |
direction | 方向 0 往下 1 往上 2向左 3向右 | 1 | Number |
openTouch | 移動端開啟touch滑動 | true | Boolean |
singleHeight | 單步運動停止的高度(默認值0是無縫不停止的滾動) direction => 0/1 | 0 | Number |
singleWidth | 單步運動停止的寬度(默認值0是無縫不停止的滾動) direction => 2/3 | 0 | Number |
waitTime | 單步停止等待時間(默認值1000ms) | 1000 | Number |
switchOffset | 左右切換按鈕距離左右邊界的邊距(px) | 30 | Number |
autoPlay | 1.1.17版本前手動切換時候需要置為false | true | Boolean |
switchSingleStep | 手動單步切換step值(px) | 134 | Number |
switchDelay | 單步切換的動畫時間(ms) | 400 | Number |
switchDisabledClass | 不可以點擊狀態(tài)的switch按鈕父元素的類名 | disabled | String |
isSingleRemUnit | singleHeight and singleWidth是否開啟rem度量 | false | Boolean |
navigation | 左右方向的滾動是否顯示控制器按鈕,true的時候autoPlay自動變?yōu)閒alse | false | Boolean |
操作環(huán)境:
| name | description | calback params |
|---|---|---|
ScrollEnd | 一次滾動完成的回調(diào)事件 | null |
操作環(huán)境
- win 10
- node v14.20.0
- npm 8.5.3
- @vue/cli 5.0.6
- vue 2.6.14
- vue-seamless-scroll 1.1.23
二、安裝和使用
1、npm
npm install vue-seamless-scroll --save
2、yarn
yarn add vue-seamless-scroll
3、cdn
https://cdn.jsdelivr.net/npm/vue-seamless-scroll@latest/dist/vue-seamless-scroll.min.js
4、使用
// 全局注冊
import Vue from 'vue'
import scroll from 'vue-seamless-scroll'
Vue.use(scroll)
//或者
//Vue.use(scroll,{componentName: 'scroll-seamless'})
// 局部注冊
import vueSeamless from 'vue-seamless-scroll'
export default {
components: {
vueSeamless
}
}
// 使用
<div id="app">
<vue-seamless-scroll></vue-seamless-scroll>
</div>三、效果圖
自動無縫滾動,且點擊對應(yīng)的每條都會顯示點中的索引,和顯示標題,如圖


四、vue-seamless-scroll 點擊事件實現(xiàn)原理
1、在 vue-seamless-scroll 包一層 div ,然后添加對應(yīng)點擊事件獲取 $event
<div @click="handleButton($event)">
2、添加 tr 每組數(shù)據(jù) class 和 id 標記
<tr v-for='(item, i) in listData' :key='i' class='labelIndex' :id='i'>
3、點擊事件處理 event ,得到點擊標記的 class,最終獲得點擊 id
// 處理鼠標點擊到哪條,可添加跳轉(zhuǎn)
handleButton(e) {
// let InfoAnync = JSON.parse(e.target.dataset.obj)
// console.log(InfoAnync,' =====> InfoAnync')
console.log(e, ' =====> e')
console.log(e.path, ' =====> e.path')
let length = e.path.length
let labelIndex = -1
for(let i=0;i < length; i++){
if(e.path[i].className === 'labelIndex'){
labelIndex = i;
break
}
}
if(labelIndex !== -1){
console.log(e.path[labelIndex].innerText, ' =====> e.path[labelIndex].innerText')
alert('labelIndex.id = ' + e.path[labelIndex].id + ',title: ' + this.listData[e.path[labelIndex].id].title)
}else{
alert('未找到數(shù)據(jù),請檢查')
}
}五、簡單實現(xiàn)
1、首先創(chuàng)建一個 vue 文件,添加標題和標題欄

2、引入 vue-seamless-scroll ,使用并且傳遞數(shù)據(jù),然后 v-for 添加顯示數(shù)據(jù)


3、在 vue-seamless-scroll 中,添加點擊事件,首先外包一個 div,添加一個點擊事件

4、接著,給 tr 添加 class 和 id ,到時點擊事件會根據(jù)此 class 和 id 進行對應(yīng)的判斷

5、點擊事件處理,通過對應(yīng) e.path[i].className 獲取之前標記的 class,然后在獲取到對應(yīng)綁定的 id 值,最后即可通過數(shù)據(jù)列表獲取到,對應(yīng)的信息,即可對應(yīng)進行相關(guān)點擊事件的處理了

6、vue-seamless-scroll 簡單optionSetting設(shè)置如下

7、vue-seamless-scroll 簡單數(shù)據(jù)展示如下

8、運行顯示,瀏覽器效果如圖

六、關(guān)鍵代碼
<template>
<div class="wrap-container sn-container">
<div class="sn-content">
<div class="sn-title">消息自動滾動播放</div>
<div class="sn-body">
<div class="wrap-container">
<div class="table">
<table border="0" cellpadding='0' cellspacing='0' class="table-header">
<tbody>
<tr>
<td width='60%'>
<span>標 題</span>
</td>
<td width='20%'>
<span>日 期</span>
</td>
<td width='20%'>
<span>熱 度</span>
</td>
</tr>
</tbody>
</table>
<div @click="handleButton($event)">
<vue-seamless-scroll :data='listData' class="seamless-warp" :class-option="optionSetting">
<table border='0' cellpadding='0' cellspacing='0' class='item'>
<tbody>
<tr v-for='(item, i) in listData' :key='i' class='labelIndex' :id='i'>
<td width='100%' class="title">
<span>{{item.title}}</span>
</td>
<td width='20%'>
<span>{{item.date}}</span>
</td>
<td width='20%'>
// 顯示熱度,且根據(jù)不同數(shù)值,顯示不同顏色
<span
:class="{colorRed: item.hot > 2555,colorOrange: item.hot < 10}"
>{{item.hot}}</span>
</td>
</tr>
</tbody>
</table>
</vue-seamless-scroll>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import vueSeamlessScroll from 'vue-seamless-scroll'
export default {
name: 'seamless-scroll',
components: {
vueSeamlessScroll
},
data() {
return {
// 數(shù)據(jù)
listData: [{
title: '錢花哪了?一圖帶你讀懂年輕人的消費觀',
date: '2020-05-05',
hot: 2306
}, {
title: '“五一”假期前三天國內(nèi)旅游收入超350億元',
date: '2020-05-02',
hot: 5689
}, {
title: '滴滴、美團、哈啰交戰(zhàn),本地生活會是終局?',
date: '2020-05-02',
hot: 9
}, {
title: '“互聯(lián)網(wǎng)+文化”逆勢上行文娛消費云端真精彩',
date: '2020-04-25',
hot: 288
}, {
title: '樂觀還是悲觀?巴菲特是個現(xiàn)實主義者!',
date: '2020-04-21',
hot: 158
}, {
title: 'B站的后浪,會把愛優(yōu)騰拍在沙灘上嗎?',
date: '2020-04-20',
hot: 889
}, {
title: '華為如何做投資的:先給兩億訂單一年上市',
date: '2020-04-01',
hot: 5800
}, {
title: '馬斯克:特斯拉股價太高了,我要賣豪宅',
date: '2020-03-25',
hot: 6
}, {
title: '人民日報海外版:云模式巧解“就業(yè)難”',
date: '2020-03-16',
hot: 88
}, {
title: '剛剛港股"崩了":狂跌近1000點!',
date: '2020-03-12',
hot: 88
}, {
title: '個人健康信息碼國家標準發(fā)布',
date: '2020-02-28',
hot: 5
}, {
title: '傳軟銀國際裁員約10%兩名管理合伙人離職',
date: '2020-02-15',
hot: 258
}, {
title: '27萬個崗位:區(qū)塊鏈、人工智能等專場招聘',
date: '2020-02-10',
hot: 198
}, {
title: '一季度電商發(fā)展勢頭迅猛農(nóng)村電商破1300萬家',
date: '2020-02-08',
hot: 19
}]
}
},
computed:{
optionSetting(){
return{
step: 0.5, // 數(shù)值越大速度滾動越快
limitMoveNum: 2, // 開始無縫滾動的數(shù)據(jù)量 this.dataList.length
hoverStop: true, // 是否開啟鼠標懸停stop
direction: 1, // 0向下 1向上 2向左 3向右
autoPlay: true, // 是否自動滾動
openWatch: true, // 開啟數(shù)據(jù)實時監(jiān)控刷新dom
singleHeight: 0, // 單步運動停止的高度(默認值0是無縫不停止的滾動) direction => 0/1
singleWidth: 0, // 單步運動停止的寬度(默認值0是無縫不停止的滾動) direction => 2/3
waitTime: 1000 // 單步運動停止的時間(默認值1000ms)
}
}
},
methods:{
// 處理鼠標點擊到哪條,可添加跳轉(zhuǎn)
handleButton(e) {
// let InfoAnync = JSON.parse(e.target.dataset.obj)
// console.log(InfoAnync,' =====> InfoAnync')
console.log(e, ' =====> e')
console.log(e.path, ' =====> e.path')
let length = e.path.length
let labelIndex = -1
for(let i=0;i < length; i++){
if(e.path[i].className === 'labelIndex'){
labelIndex = i;
break
}
}
if(labelIndex !== -1){
console.log(e.path[labelIndex].innerText, ' =====> e.path[labelIndex].innerText')
alert('labelIndex.id = ' + e.path[labelIndex].id + ',title: ' + this.listData[e.path[labelIndex].id].title)
}else{
alert('未找到數(shù)據(jù),請檢查')
}
}
}
}
</script>
<style lang="scss" scoped>
.sn-title{
text-align: center;
}
.sn-container{
position: absolute;
left: 30%;
width: 600px;
height: 800px;
%table-style{
width: 100%;
height: 35px;
table-layout: fixed;
tr {
td {
padding: 10px 5px;
text-align: center;
background-color: transparent;
white-space: nowrap;
overflow: hidden;
color: #e200ff;
font-size: 14px;
}
}
}
.table{
.table-header{
@extend %table-style;
}
.seamless-warp{
height: 400px;
overflow: hidden;
visibility: visible;
.colorRed {
color: #FF4669;
}
.colorOrange {
color: #FFC956;
}
.item{
height: auto;
@extend %table-style;
tr{
td{
&.title{
text-overflow: ellipsis;
display: inline-block;
}
}
}
}
}
}
}
</style>
以上內(nèi)容到此結(jié)束,下面補充介紹vue-seamless-scroll 動態(tài)開啟和關(guān)閉滾動
<vue-seamless-scroll
:data="threeList"
class="warp"
:class-option="defaultOption"
ref="scroll3"
>- 綁定一個ref,
- 通過
this.$refs.scroll3._cancle()方法停止?jié)L動 - 通過
this,$ref.scroll3._startMove()方法再次啟動
到此這篇關(guān)于vue-seamless-scroll 實現(xiàn)簡單自動無縫滾動,且添加對應(yīng)點擊事件的簡單整理的文章就介紹到這了,更多相關(guān)vue-seamless-scroll 無縫滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用keep-alive無效以及include和exclude用法解讀
這篇文章主要介紹了vue使用keep-alive無效以及include和exclude用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
uni-app項目中引入Vant?UI組件庫完美避坑指南(純凈版)
網(wǎng)上百度uniapp使用vant時,很多答案都是在根路徑下創(chuàng)建文件夾,而且都是基于小程序環(huán)境的,其實uniapp可以直接使用的,這篇文章主要給大家介紹了關(guān)于uni-app項目中引入Vant?UI組件庫完美避坑指南的相關(guān)資料,需要的朋友可以參考下2024-02-02
element plus中el-upload實現(xiàn)上傳多張圖片的示例代碼
最近寫項目的時候需要一次上傳多張圖片,本文主要介紹了element plus中el-upload實現(xiàn)上傳多張圖片的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-01-01
vue主動刷新頁面及列表數(shù)據(jù)刪除后的刷新實例
今天小編就為大家分享一篇vue主動刷新頁面及列表數(shù)據(jù)刪除后的刷新實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
在Vue+Ts+Vite項目中配置別名指向不同的目錄并引用的案例詳解
這篇文章主要介紹了在Vue+Ts+Vite項目中配置別名指向不同的目錄并引用,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
vue通過v-html指令渲染的富文本無法修改樣式的解決方案
這篇文章主要介紹了vue通過v-html指令渲染的富文本無法修改樣式的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
elementUI中el-dropdown的command實現(xiàn)傳遞多個參數(shù)
這篇文章主要介紹了elementUI中el-dropdown的command實現(xiàn)傳遞多個參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

