vue列表滚动时固定表头或最左列的效果怎样做
Admin 2022-09-16 群英技术资讯 505 次浏览
今天我们来学习关于“vue列表滚动时固定表头或最左列的效果怎样做”的内容,下文有详解方法和实例,内容详细,逻辑清晰,有需要的朋友可以参考,希望大家阅读完这篇文章后能有所收获,那么下面就一起来了解一下吧。
在移动端开发中,会用到列表作为信息展示方式,一般希望上下滚动时,可以固定表头,左右滚动时,可以固定最左列。
1、列表可以使用数组循环遍历;
2、上下滚动时,可以固定表头在最顶端显示;
3、左右滚动时,可以固定左边一列或多列可以固定显示;
4、列表的列宽允许在数组中设置;
1、页面使用四个bom元素分别存储四种元素:
1)固定在左上角,完全不参与滚动表头元素;
2)固定在顶部,只允许左右滚动表头元素;
3)固定在左侧,只允许上下滚动列元素;
4)右下角,左右上下均可随意滚动列元素;
2、表头数组与列表数据数组之间互相联系,表头属性可以控制列表列排序、列表宽度、是否为固定列等;
3、四个dom之间增加联动,使用@scroll、scrollLeft、scrollTop;
<!-- 宽度增加动态设置 --> <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); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
一、说一下连接不同的数据库需要安装相应的插件,此demo使用mysql数据库,需自行安装mysql数据库软件。新建数据库webapp,新建表users:二、直接开码npminstallmysql--save注释:安装mysql依赖包,保存在本项目1、测试尝试连接数据库,并查询表users在app.js中,随便找个位置添加如下测试代码,测
vue Element左侧无限级菜单怎么实现?最近项目中,要用到element-ui的无限级分类菜单,根据角色生成不同的递归数据,查阅了网上很多资料,发现很多都不太完整并且没有很多的延伸性。
基于element-plus的二次封装数据双向绑定在实际开发中,经常需要基于element-plus封装一些自己的定制化组件,方便快速构建我们当前的业务。在vue2.0中父子组件数据的双向绑定通
在nodejs中我们如何向mysql数据库插入单条或多条数据呢?或者说nodejs如何向mysql批量插入数据呢? 我们都知道插入数据使用的是mysql的“ INSERT INTO ”语句,下面先来看看如何使用nodejs向mysql插入单条数据呢?请看nodejs mysql的使用示例,向customer表
这篇文章给大家分享的是怎样使用jquery实现一个简易仪表盘,其实要实现一个简单的仪表盘并不困难,但是要计算好标码的位置,实现效果和代码如下,感兴趣的朋友就接着看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008