vue3實現(xiàn)搜索項超過n行就折疊的思路詳解
我們在做列表查詢的時候,會有很多查詢項,如何實現(xiàn)超過n行查詢項的時候自動折疊起來呢?
實現(xiàn)效果
實現(xiàn)思路
- 實現(xiàn)組件的布局。
- 綁定監(jiān)聽事件和銷毀監(jiān)聽事件
- 高度判斷和圖標(biāo)的顯示與隱藏
實現(xiàn)組件的布局
外層盒子(限制高度)、折疊的圖標(biāo)或者文字(用來顯示和隱藏多余的行)、插槽(挖個坑給搜索行占位)。
事件綁定與事件銷毀
需要綁定一個resize事件。resize事件是在窗口大小發(fā)生變化時就會觸發(fā)。resize事件觸發(fā)我們就要重新計算盒子查詢項的高度,判斷是否需要折疊或者顯示。mounted生命周期觸發(fā)計算組件實例高度。并計算查詢項高度。resize事件要在組件銷毀前的生命周期中進(jìn)行銷毀。不影響其他組件。
高度判斷和圖標(biāo)的顯示與隱藏
首先圖標(biāo)盒子綁定狀態(tài),用來顯示和隱藏。 其次外層盒子需要設(shè)置一個高度臨界點,即多大高度時不折疊,超過了這個高度就折疊。盒子高度需要你計算,比如,你需要4行不折疊,需要算出四行的高度并加上圖標(biāo)的高度。如果大于高度則顯示圖標(biāo)、如果小于隱藏圖標(biāo)。
完整代碼
布局
<template> <div class="fold_box"> <div class="fold_box_over" :style="{height: mainHeight > 0 ? mainHeight + 'px' : ''}" :class="{'fold_box_over_max': isOver}" > <div ref="foldBoxMain" class="fold_box_main" > <slot></slot> </div> <div v-show="isOverChange" class="fold_box_over_show" > <div class="fold_box_over_btn" @click="showOver" > <el-icon :class="{'fold_box_over_btn_rotate': !isOver}" :size="14"> <ArrowDown /> </el-icon> </div> </div> </div> </div> </template>
css代碼
<style lang="less" scoped> .fold_box { width: 100%; .fold_box_over { overflow: hidden; position: relative; transition: all 0.4s ease; } .fold_box_over_max { height: 159px !important; } .fold_box_main { width: 100%; } .fold_box_over_show { height: 15px; position: absolute; width: 100%; background-color: #fff; bottom: 0; left: 0; } .fold_box_over_btn { width: 20px; height: 15px; cursor: pointer; text-align: center; line-height: 15px; margin: 0 auto; el-icon svg{ font-weight: bold; transition: all 0.4s ease; } &:hover { color: #00caab; } } .fold_box_over_btn_rotate svg{ transform: rotate(180deg); } } </style>
script
<script> import { reactive, toRefs, ref,onMounted,onBeforeUnmount,getCurrentInstance } from "vue"; export default { setup() { const state = reactive({ boxWidth: 0, mainHeight: 0, isOver: false, isOverChange: false }); const { ctx } = getCurrentInstance() const changeSize = () => { let el = ctx.$el state.boxWidth = el.offsetWidth countMainHeight() } window.addEventListener('resize', changeSize) const countMainHeight = () => { if (ctx.$refs['foldBoxMain']) { let el= ctx.$refs['foldBoxMain'] state.mainHeight = el.offsetHeight + 15 if (state.mainHeight > 129) { state.isOverChange = true state.isOver = true } else { state.isOverChange = false state.isOver = false } } } onMounted(() => { changeSize() }) onBeforeUnmount(()=> { window.removeEventListener('resize', changeSize) }) const showOver = () => { state.isOver = !state.isOver } return { ...toRefs(state), changeSize, countMainHeight, showOver }; }, }; </script>
組件使用
<template> <FoldBox ref="foldBox"> <div class="item" v-for="(item,index) in boxList" :key="index">{{item}}</div> </FoldBox> </template> <script> import { reactive, toRefs, ref } from "vue"; import FoldBox from './FoldBox.vue' export default { components:{ FoldBox }, setup() { const state = reactive({ boxList: [1,2,3,4,5] }); return { ...toRefs(state), }; }, }; </script> <style scoped> .item { height: 30px; margin: 6px; background-color: skyblue; } </style>
總結(jié)
- resize事件的使用
- vue3中g(shù)etCurrentInstance獲取組件實例以及獲取ref屬性的使用
- vue3的生命周期onMounted、onBeforeUnmount、setup的使用
- css3 transition和transform的使用
- offsetHeight和offsetWidth的使用
到此這篇關(guān)于vue3實現(xiàn)搜索項超過n行就折疊的文章就介紹到這了,更多相關(guān)vue3搜索項折疊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element自定義 多文件上傳 觸發(fā)多次on-change問題
這篇文章主要介紹了element自定義 多文件上傳 觸發(fā)多次on-change問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03詳解使用vue-admin-template的優(yōu)化歷程
這篇文章主要介紹了詳解使用vue-admin-template的優(yōu)化歷程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05深入學(xué)習(xí)Vue nextTick的用法及原理
這篇文章主要介紹了深入學(xué)習(xí)Vue nextTick的用法及原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10vue中for循環(huán)更改數(shù)據(jù)的實例代碼(數(shù)據(jù)變化但頁面數(shù)據(jù)未變)
這篇文章主要介紹了vue中for循環(huán)更改數(shù)據(jù)的實例代碼(數(shù)據(jù)變化但頁面數(shù)據(jù)未變)的相關(guān)資料,需要的朋友可以參考下2017-09-09Vue項目中使用iView組件庫設(shè)置樣式不生效的解決方案
這篇文章主要介紹了Vue項目中使用iView組件庫設(shè)置樣式不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09