1. 程式人生 > 其它 >flex 佈局,元素不固定處理

flex 佈局,元素不固定處理

最近用flex佈局,但是每個欄目數目不固定,佈局起來,有些很緊密,有些間距才超級大,與設計稿的效果相差太遠太遠了。

之前我處理,是寫一堆樣式,把可能的情況都包含,勉強可以實現。

 

今天又碰到類似的問題,但是我不想那樣寫了。於是網上搜索了。終於找到一個我滿意的。

連結:https://www.csdn.net/tags/NtjaYgxsMjAyNTMtYmxvZwO0O0OO0O0O.html

我實現的效果

 

 

 關鍵樣式程式碼

    .chy-test:last-of-type { //這個才是取同類最後一個 https://www.csdn.net/tags/NtjaYgxsMjAyNTMtYmxvZwO0O0OO0O0O.html
                     margin-right: calc(26% + 22% / 2)
; } .chy-test:nth-child(3n) { //第3個和3n元素的處理 margin-right: 0; }

我的頁面程式碼

<template>
    <view class="chy-page ">
        <u-navbar class="custom-header" title="編輯客戶標籤" height="44" :autoBack="true">
            <view 
slot="right"> <view>確定</view> </view> </u-navbar> <view class="chy-lable-box"> <block v-for="(tag,index) in tempCustomerLabelJsonList" :key="index"> <view class="chy-item " :style="{background:tag && tag.split('_')[2],color:'#FFFFFF'}"
> {{(tag&&tag.split('_')[0])}} </view> </block> <!-- <block v-for="tag in customerInfo.customerLabelJsonList && customerInfo.customerLabelJsonList" :key="tag && tag.split('_')[0]"> <u-tag :text="`${(tag&&tag.split('_')[0])}` " plain size="large" shape="circle" class="chy-tag" color="#ffffff" :bgColor="tag && tag.split('_')[2]"></u-tag> </block> --> </view> <view class="chy-lable-biglist-box"> <block v-for="(item,index) in groupLabelAllList" :key="index"> <view class="chy-lable-item"> <view class="chy-lable-title"> {{item.groupName}} {{ item.selectedType == 1 ? "單選" : "多選"}} </view> <view class="chy-lable-list-box"> <block v-for="(itemSon,indexSon) in item.labelJsonList" :key="indexSon"> <view class="chy-test"> <view class="chy-item" :style="{background:item.labelColor,color:'#FFFFFF'}" @click="itemSon.isEdit ? toggleLabel(itemSon, item): ''"> <view class=" chy-item-left">{{itemSon.lableName}} </view> <u-icon name="checkmark" color="#FFFFFF" class="chy-item-right" v-if="itemSon.selected"> </u-icon> </view> </view> </block> </view> </view> </block> </view> </view> </template> <script> import { test } from '../../../common/crm/xxxx.js' import { demo } from '../../../common/crm/xxxxxxx.js' export default { data() { return { tempCustomerLabelJsonList: [], //已經擁有的標籤 groupLabelAllList: [], //所有標籤 flag: true, //表示和擁有的沒有匹配上 } }, onLoad() { let that = this // #ifndef APP-NVUE const eventChannel = this.getOpenerEventChannel(); // #endif // 監聽acceptDataFromOpenerPage事件,獲取上一頁面通過eventChannel傳送到當前頁面的資料 eventChannel.on('sendToeLableData', function(res) { //console.log(res, "sendToeLableData") that.tempCustomerLabelJsonList = res.data; that.getDemoXXXXX(); console.log(that.groupLabelAllList, "groupLabelAllList") }) }, methods: { /** 所有標籤資訊 */ getDemoXXX() { let that = this; that.groupLabelAllList = []; demoXXX({}).then((response) => { let oldData = response.data; that.mergerGroupLableNew(oldData); }); }, //資料處理 mergerGroupLableNew(oldData) { let that = this; oldData.forEach(function(item) { // if(item.groupName=='愛好'){ that.getTargetDataNew(item); // } }); }, getTargetDataNew(item) { let that = this; that.flag = true; let bData = { groupId: item.groupId, groupName: item.groupName, selectedType: item.selectedType, labelColor: item.labelColor, labelJsonList: [], }; if (that.tempCustomerLabelJsonList.length == 0) { //擁有的標籤迴圈完畢了,列表標籤還沒有迴圈完成的 //alert("d") item.labelJsonList.forEach(function(itemLable) { //自身迴圈 bData.labelJsonList.push({ lableName: itemLable.trim(), selected: false, //未選中 isEdit: true, groupId: item.groupId.trim(), }); }); } else { that.tempCustomerLabelJsonList.forEach(function(xitem) { // console.log(xitem,'xitem') if (xitem.split("_")[1] == item.groupId) { item.labelJsonList.forEach(function(son) { let sonJson = { lableName: son.trim(), selected: null, //選中 isEdit: true, //是否可編輯 true可以,false不可以 groupId: item.groupId.trim(), result: null, orderNum: 0, }; if (son == xitem.split("_")[0]) { that.flag = false; //表示當前迴圈有匹配上 sonJson.selected = true; sonJson.orderNum = 1; //找到標籤 // let deleteData = that.tempCustomerLabelJsonList.splice( // that.tempCustomerLabelJsonList.indexOf(xitem), // 1 // ); //用完刪除 } else { sonJson.selected = false; } if (item.selectedType == 2) { sonJson.result = bData.labelJsonList.some( (x) => x.lableName == sonJson.lableName ); if (!sonJson.result) { bData.labelJsonList.push(sonJson); } if (sonJson.result && sonJson.selected) { let oldData = JSON.parse(JSON.stringify(sonJson)); oldData.selected = false; let deleteData = bData.labelJsonList.splice( bData.labelJsonList.findIndex( (o) => o.lableName == oldData.lableName && oldData.selected == o.selected ), 1, sonJson ); } } else { bData.labelJsonList.push(sonJson); } }); } }); if (that.flag) { //當次迴圈沒有匹配上的資料迴圈 //沒有匹配上就直接新增 item.labelJsonList.forEach(function(itemLable) { bData.labelJsonList.push({ lableName: itemLable.trim(), selected: false, //未選中 isEdit: true, groupId: item.groupId.trim(), }); }); } } that.groupLabelAllList.push(bData); //迴圈一次加一次 }, //切換標籤是否選中 toggleLabel(tag, item) { tag.orderNum = 2; if (item.selectedType == 1) { //單選 tag.selected = !tag.selected; if ( item.labelJsonList.filter((item) => item.selected == true).length > 1 ) { uni.showToast({ title: '您已經選中一個標籤,請先取消再選擇!', icon: 'error' }) tag.selected = !tag.selected; } } else { //多選 tag.selected = !tag.selected; } }, } } </script> <style scoped lang="scss"> .chy-page { // padding: 10px; background: #F4F5F6; } .chy-lable-box { margin: 50px 0 20px 0; padding: 10px; background: #FFFFFF; border-radius: 10px; // border: 1px solid red; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; .chy-item { width: 26%; // border: 1px solid red; text-align: center; padding: 5px; border: 1px solid #00A0E9; box-sizing: border-box; border-radius: 15px; margin: 5px 0; background: #E6F7FF; color: #00A0E9; font-size: 12px; word-wrap: break-word; } } .chy-lable-biglist-box { background: #FFFFFF; .chy-lable-item { background: #FFFFFF; margin: 10px 0 20px 0; .chy-lable-title { font-weight: 400; font-size: 15px; color: #666666; margin: 10px 15px 0 15px; padding-top: 10px; } .chy-lable-list-box { padding: 10px; background: #FFFFFF; // border: 1px solid red; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; .chy-test { width: 26%; } .chy-item { flex: 1; // border: 1px solid red; text-align: center; padding: 5px; // border: 1px solid #F2F2F2; box-sizing: border-box; border-radius: 15px; margin: 5px 0; background: #F2F2F2; color: #333333; font-size: 12px; word-wrap: break-word; display: flex; justify-content: space-between; align-items: center; .chy-item-left { flex: 1; text-align: center; } .chy-item-right { width: 30px; text-align: center; } } .chy-test:last-of-type { //這個才是取同類最後一個 https://www.csdn.net/tags/NtjaYgxsMjAyNTMtYmxvZwO0O0OO0O0O.html margin-right: calc(26% + 22% / 2); } .chy-test:nth-child(3n) { //第3個和3n元素的處理 margin-right: 0; } } // .chy-lable-list-box:last-of-type { // background: red; // } } } </style>