vue實(shí)現(xiàn)列表固定列滾動(dòng)
vue+scss移動(dòng)端列表固定列滾動(dòng),供大家參考,具體內(nèi)容如下
功能介紹:
在移動(dòng)端開發(fā)中,會(huì)用到列表作為信息展示方式,一般希望上下滾動(dòng)時(shí),可以固定表頭,左右滾動(dòng)時(shí),可以固定最左列。
大致需求:
1、列表可以使用數(shù)組循環(huán)遍歷;
2、上下滾動(dòng)時(shí),可以固定表頭在最頂端顯示;
3、左右滾動(dòng)時(shí),可以固定左邊一列或多列可以固定顯示;
4、列表的列寬允許在數(shù)組中設(shè)置;
整體思路:
1、頁(yè)面使用四個(gè)bom元素分別存儲(chǔ)四種元素:
1)固定在左上角,完全不參與滾動(dòng)表頭元素;
2)固定在頂部,只允許左右滾動(dòng)表頭元素;
3)固定在左側(cè),只允許上下滾動(dòng)列元素;
4)右下角,左右上下均可隨意滾動(dòng)列元素;
2、表頭數(shù)組與列表數(shù)據(jù)數(shù)組之間互相聯(lián)系,表頭屬性可以控制列表列排序、列表寬度、是否為固定列等;
3、四個(gè)dom之間增加聯(lián)動(dòng),使用@scroll、scrollLeft、scrollTop;
具體實(shí)現(xiàn):
一、display:flex布局,分為四組容器布局:

<!-- 寬度增加動(dòng)態(tài)設(shè)置 -->
<div class="box">
? <div class="table-box">
? ? <div class="fixedHeadBox"?
? ? ? :style="{width: fixedWid}"></div>
? ? <div class="nomalHeadBox"
? ? ? style="{width: 'calc(100% - '+fixedWid+')'}"
? ? ></div>
? ? <div class="fixedListBox"?
? ? ? :style="{width: fixedWid}"></div>
? ? ? <div class="nomalListBox"
? ? ? ? :style="{width: 'calc(100% - '+fixedWid+')'}"
? ? ? ></div>
? ? </div>
? </div>
</div>export default {
? data() {
? ? return {
? ? ? fixedWid: ''
? ? };
? }
}.box {
? width: 100vw; height: 100vh;
? box-sizing: border-box;
? padding: 5vh 5vw;
? background: #000;
}
$headHei: 40px;
.table-box {
? width: 100%; height: 100%;
? display: flex;
? flex-wrap: wrap;
? overflow: hidden;
? ? .fixedHeadBox {
? ? ? background: pink;
? ? ? height: $headHei;
? ? }
? ? .nomalHeadBox {
? ? ? background: yellow;
? ? ? height: $headHei;
? ? ? overflow: hidden;
? ? }
? ? .fixedListBox{
? ? ? height: calc(100% - #{$headHei});
? ? ? background: lightblue;
? ? ? overflow: hidden;
? ? }
? ? .nomalListBox {
? ? ? background: #fff;
? ? ? height: calc(100% - #{$headHei});
? ? ? overflow: auto;
? ? }
}二、列表頭部、內(nèi)部數(shù)據(jù)綁定:
應(yīng)用到v-for遍歷表頭、列表數(shù)據(jù),并計(jì)算列表寬度:
<div class="fixedHeadBox" :style="{width: fixedWid}">
? <ul>
? ? <li v-for="(item, index) in fixedHead" :key="index"?
? ? ? :style="{width: item.width}">
? ? ? ? {{item.name}}
? ? </li>
? </ul>
</div>
<div class="nomalHeadBox"
? :style="{width: 'calc(100% - '+fixedWid+')'}">
? <div ref="nomalHeadBox">
? ? <ul :style="{width: nomalWid}">
? ? ? <li v-for="(item, index) in nomalHead"?
? ? ? ? :key="index" :style="{width: item.width}">
? ? ? ? {{item.name}}
? ? ? </li>
? ? </ul>
? </div>
</div>
<div class="fixedListBox" :style="{width: fixedWid}">
? <div ref="fixedListBox">
? ? <ul v-for="(item, index) in list" :key="index" >
? ? ? <li v-for="(it, index) in fixedHead" :key="index"?
? ? ? ? :style="{width: it.width}">
? ? ? ? {{item[it.prop]}}
? ? ? </li>
? ? </ul>
? </div>
</div>
<div class="nomalListBox" ref="nomalListBox"
? :style="{width: 'calc(100% - '+fixedWid+')'}">
? <ul :style="{width: nomalWid}"?
? ? v-for="(item, index) in list" :key="index">
? ? <li v-for="(it, index) in nomalHead" :key="index"?
? ? ? :style="{width: it.width}">
? ? ? {{item[it.prop]}}
? ? </li>
? </ul>
</div>data() {
? return {
? ? tableHead: [
? ? ? { name: '', prop: 'a', width: '100px', isfixed: true },
? ? ? { name: '', prop: 'b', width: '80px' },
? ? ? { name: '', prop: 'c', width: '80px' },
? ? ? { name: '', prop: 'd', width: '100px' },
? ? ? { name: '', prop: 'e', width: '100px' },
? ? ? { name: '', prop: 'f', width: '100px' },
? ? ? { name: '', prop: 'g', width: '120px' }
? ? ],
? ? list: [
? ? ? { a: '', b: '', c: '', d: '', e: '', f: '', g: '' }
? ? ],
? ? fixedHead: [],
? ? nomalHead: [],
? ? fixedWid: '',
? ? nomalWid: ''
? };
},
mounted() {
? this.initData();
},
methods: {
? initData() {
? ? this.fixedHead = this.tableHead.filter((item) => {
? ? ? return item.isfixed
? ? });
? ? this.nomalHead = this.tableHead.filter((item) => {
? ? ? return !item.isfixed
? ? });
? ? this.initSize();
? },
? initSize() {
? ? let fwid = 0; let nwid = 0;
? ? this.fixedHead.forEach((item) => {
? ? ? // 此處以px單位為例
? ? ? const len = item.width.length - 2;
? ? ? const width = item.width.substring(0, len) - 0;
? ? ? fwid += width;
? ? });
? ? this.nomalHead.forEach((item) => {
? ? ? const len = item.width.length - 2;
? ? ? const width = item.width.substring(0, len) - 0;
? ? ? nwid += width;
? ? });
? ? this.fixedWid = fwid + 'px';
? ? this.nomalWid = nwid + 'px';
? }
}三、列表滾動(dòng)聯(lián)動(dòng):
除左上角元素外,其余三個(gè)元素均有聯(lián)動(dòng)滾動(dòng)效果,增加滾動(dòng)監(jiān)聽事件@scroll。
<div class="nomalHeadBox"
? ? :style="{width: 'calc(100% - '+fixedWid+')'}">
? ? <div ref="nomalHeadBox" @scroll="scrollHList">
? ? ? ? ......
? ? </div>
</div>
<div class="fixedListBox" :style="{width: fixedWid}">
? ? <div ref="fixedListBox" @scroll="scrollFList">
? ? ? ? ......
? ? </div>
</div>
<div class="nomalListBox" ref="nomalListBox"
? ? @scroll="scrollList"
? ? :style="{width: 'calc(100% - '+fixedWid+')'}">
? ? ......
</div>methods: {
? scrollHList() {
? ? this.$refs.nomalListBox.scrollLeft =
? ? ? this.$refs.nomalHeadBox.scrollLeft;
? },
? scrollFList() {
? ? this.$refs.nomalListBox.scrollTop =
? ? ? this.$refs.fixedListBox.scrollTop;
? },
? scrollList() {
? ? this.$refs.fixedListBox.scrollTop =
? ? ? this.$refs.nomalListBox.scrollTop;
? ? this.$refs.nomalHeadBox.scrollLeft =
? ? ? this.$refs.nomalListBox.scrollLeft;
? }
}四、去除頭部、左側(cè)列表滾動(dòng)標(biāo)簽的滾動(dòng)條:
.nomalHeadBox {
? ? >div {
? ? ? ? overflow: auto;
? ? ? ? height: calc(100% + 10px);
? ? }
}
.fixedListBox{
? ? >div {
? ? ? ? overflow: auto;
? ? ? ? height: 100%;
? ? ? ? width: calc(100% + 10px);
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)循環(huán)滾動(dòng)列表
- Vue.js 無限滾動(dòng)列表性能優(yōu)化方案
- vue實(shí)現(xiàn)列表無縫循環(huán)滾動(dòng)
- vue實(shí)現(xiàn)列表滾動(dòng)的過渡動(dòng)畫
- 基于vue實(shí)現(xiàn)循環(huán)滾動(dòng)列表功能
- vue實(shí)現(xiàn)列表無縫滾動(dòng)效果
- 基于Vue3實(shí)現(xiàn)列表虛擬滾動(dòng)效果
- vue實(shí)現(xiàn)列表無縫滾動(dòng)
- Vue3封裝自動(dòng)滾動(dòng)列表指令(含網(wǎng)頁(yè)縮放滾動(dòng)問題)
相關(guān)文章
基于Vue3創(chuàng)建一個(gè)簡(jiǎn)單的倒計(jì)時(shí)組件
這篇文章主要給大家介紹了基于Vue3創(chuàng)建一個(gè)簡(jiǎn)單的倒計(jì)時(shí)組件的代碼示例,文中通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11
vue點(diǎn)擊按鈕實(shí)現(xiàn)簡(jiǎn)單頁(yè)面的切換
這篇文章主要為大家詳細(xì)介紹了vue點(diǎn)擊按鈕實(shí)現(xiàn)簡(jiǎn)單頁(yè)面的切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Vue?echarts@4.x中國(guó)地圖及AMap相關(guān)API使用詳解
這篇文章主要為大家介紹了Vue使用echarts@4.x中國(guó)地圖及AMap相關(guān)API使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
解決uniapp項(xiàng)目在微信開發(fā)工具里打開報(bào)錯(cuò)Error:app.json:在項(xiàng)目根目錄未找到app.json
這篇文章主要給大家介紹了關(guān)于解決uniapp項(xiàng)目在微信開發(fā)工具里打開報(bào)錯(cuò)Error:app.json:在項(xiàng)目根目錄未找到app.json的相關(guān)資料,文中通過圖文將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
vue-router路由懶加載及實(shí)現(xiàn)的3種方式
這篇文章主要給大家介紹了關(guān)于vue-router路由懶加載及實(shí)現(xiàn)的3種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
ElementUI表單驗(yàn)證validate和validateField的使用及區(qū)別
Element-UI作為前端框架,最常使用到的就是表單驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于ElementUI表單驗(yàn)證validate和validateField的使用及區(qū)別,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Vue-cli3項(xiàng)目配置Vue.config.js實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于Vue-cli3項(xiàng)目配置Vue.config.js的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

