列表三个元素排一行的布局问题
背景: 要求每三个元素占满一行,每个元素之间的间距固定,每个元素的宽度随着设备的分辨率大小适配。
解决方案:
- 将后端返回的一维数组列表转换成二维数组,每三个元素为一组。
类似于这样的数据结构:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| [ [{name: "han"},{name: "han"},{name: "han"}], [{name: "han"},{name: "han"},{name: "han"}], [{name: "han"},{name: "han"},{name: "han"}], [{name: "han"},{name: "han"},{name: "han"}] ]
initData () { this.list.forEach((item, index) => { let resIpList = [] for (let i=0; i < item.ipList.length; i+=3) { let sliceArr = item.ipList.slice(i, i+3) sliceArr.length == 2 && sliceArr.push({}) resIpList.push(sliceArr) } this.$set(item, 'ipListNew', resIpList) }); }
|
- js动态设置每个元素的宽度——
整个屏幕宽度减去间距/3
1 2 3 4 5 6 7 8
| this.$nextTick(() => { let allWidth = Q.$(".cabinet-area").width() let spaceWidth = Q.$(".cabinet-area").css("padding-left").split("px")[0] let itemWidth = (parseInt(allWidth) - parseInt(spaceWidth )* 6) / 3 Q.$(".ip-item").width(itemWidth) Q.$(".ip-item").height(itemWidth / 0.8) })
|
- css属性采用flex布局,默认不换行,元素间间距相等
space-between
1 2 3 4
| .ip-list { display: flex; justify-content: space-between; }
|
利用数组的并集交集思想解决复选框的正选反选问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| selectChange () { let selectArr = Object.keys(this.selectList) let filterArr = this.gameList.filter((item) => { return selectArr.indexOf(item.name) > -1 }) let res = {} for (let gameItem of filterArr) { gameItem.serverList.filter(item => { this.formItem.switch ? this.selectList[gameItme.name].includes(item.name) && (res[item.name] = JSON.parse(item.jsons)) : !this.selectList[gameItme.name].includes(item.name) && (res[item.name] = JSON.parse(item.jsons)) }) } this.serverStrs = res }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| if (true) { if (ffsUtils.is_ffsPhoneInfo() && Q.ua.IOS) { } else { Q.cookie.set('dm_no_change', 'show') Q.$("body").css("top", -window.scrollY + "px"); Q.$("body").css("position", "fixed"); } } else { if (ffsUtils.is_ffsPhoneInfo() && Q.ua.IOS) { } else { Q.cookie.set('dm_no_change', '') let scrollY = -parseInt(Q.$("body").css("top")) Q.$("body").css("position", "static"); Q.$("body").css("top", ""); window.scrollTo(0, scrollY); } }
prevent() { this.$refs["mask"].addEventListener( "touchmove", function (e) { e.preventDefault(); }, false ); },
|