vue实现列表固定列滚动

 更新时间:2022年07月14日 10:30:22   作者:iatkotworld  
这篇文章主要为大家详细介绍了vue实现列表固定列滚动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

vue+scss移动端列表固定列滚动,供大家参考,具体内容如下

功能介绍:

在移动端开发中,会用到列表作为信息展示方式,一般希望上下滚动时,可以固定表头,左右滚动时,可以固定最左列。

大致需求:

1、列表可以使用数组循环遍历;
2、上下滚动时,可以固定表头在最顶端显示;
3、左右滚动时,可以固定左边一列或多列可以固定显示;
4、列表的列宽允许在数组中设置;

整体思路:

1、页面使用四个bom元素分别存储四种元素:

1)固定在左上角,完全不参与滚动表头元素;
2)固定在顶部,只允许左右滚动表头元素;
3)固定在左侧,只允许上下滚动列元素;
4)右下角,左右上下均可随意滚动列元素;

2、表头数组与列表数据数组之间互相联系,表头属性可以控制列表列排序、列表宽度、是否为固定列等;

3、四个dom之间增加联动,使用@scroll、scrollLeft、scrollTop;

具体实现:

一、display:flex布局,分为四组容器布局:

<!-- 宽度增加动态设置 -->
<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;
? ? }
}

二、列表头部、内部数据绑定:

应用到v-for遍历表头、列表数据,并计算列表宽度:

<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';
? }
}

三、列表滚动联动:

除左上角元素外,其余三个元素均有联动滚动效果,增加滚动监听事件@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;
? }
}

四、去除头部、左侧列表滚动标签的滚动条:

.nomalHeadBox {
? ? >div {
? ? ? ? overflow: auto;
? ? ? ? height: calc(100% + 10px);
? ? }
}
.fixedListBox{
? ? >div {
? ? ? ? overflow: auto;
? ? ? ? height: 100%;
? ? ? ? width: calc(100% + 10px);
? ? }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

相关文章

  • Vue Promise解决回调地狱问题实现方法

    Vue Promise解决回调地狱问题实现方法

    这篇文章主要介绍了Vue Promise解决回调地狱问题,总的来说这并不是一道难题,那为什么要拿出这道题介绍?拿出这道题真正想要传达的是解题的思路,以及不断优化探寻最优解的过程。希望通过这道题能给你带来一种解题优化的思路
    2023-01-01
  • Vue3?路由页面切换动画?animate.css效果

    Vue3?路由页面切换动画?animate.css效果

    这篇文章主要介绍了Vue3路由页面切换动画animate.css效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • vue3中处理不同数据结构JSON的实现

    vue3中处理不同数据结构JSON的实现

    本文主要介绍了vue3中处理不同数据结构JSON的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Vue axios全局拦截 get请求、post请求、配置请求的实例代码

    Vue axios全局拦截 get请求、post请求、配置请求的实例代码

    这篇文章主要介绍了Vue axios全局拦截 get请求、post请求、配置请求的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • vue elementUI下拉框值无法选中问题及解决方案

    vue elementUI下拉框值无法选中问题及解决方案

    大家在写系统的时候,会有这样的需求:点击修改后把需要修改的数据放入表单,其中会有下拉单选框、下拉多选框,展示下拉框单选框内的数据只需要将所选id赋值给下拉框绑定的值就可以了,下面给大家分享vue elementUI下拉框值无法选中问题,感兴趣的朋友一起看看吧
    2024-03-03
  • 深入浅析Vue中的 computed 和 watch

    深入浅析Vue中的 computed 和 watch

    computed 计算属性是通过属性计算得来的属性,watch属性变化,就会触发监听的函数。下面通过本文给大家介绍Vue中的 computed 和 watch,感兴趣的朋友一起看看吧
    2018-06-06
  • Vue中Vue.extend()的使用及解析

    Vue中Vue.extend()的使用及解析

    这篇文章主要介绍了Vue中Vue.extend()的使用及解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • Vuex的使用及知识点笔记

    Vuex的使用及知识点笔记

    这篇文章主要介绍了Vuex的使用及知识点笔记,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • vue实现记事本案例

    vue实现记事本案例

    这篇文章主要为大家详细介绍了vue实现记事本案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • vue的for循环使用方法

    vue的for循环使用方法

    在本篇文章里小编给大家整理了关于vue的for循环使用方法和步骤,有需要的朋友们跟着学习下。
    2019-02-02

最新评论

?


http://www.vxiaotou.com