vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的思路詳解
我們在做列表查詢的時(shí)候,會(huì)有很多查詢項(xiàng),如何實(shí)現(xiàn)超過n行查詢項(xiàng)的時(shí)候自動(dòng)折疊起來呢?
實(shí)現(xiàn)效果

實(shí)現(xiàn)思路
- 實(shí)現(xiàn)組件的布局。
- 綁定監(jiān)聽事件和銷毀監(jiān)聽事件
- 高度判斷和圖標(biāo)的顯示與隱藏
實(shí)現(xiàn)組件的布局
外層盒子(限制高度)、折疊的圖標(biāo)或者文字(用來顯示和隱藏多余的行)、插槽(挖個(gè)坑給搜索行占位)。
事件綁定與事件銷毀
需要綁定一個(gè)resize事件。resize事件是在窗口大小發(fā)生變化時(shí)就會(huì)觸發(fā)。resize事件觸發(fā)我們就要重新計(jì)算盒子查詢項(xiàng)的高度,判斷是否需要折疊或者顯示。mounted生命周期觸發(fā)計(jì)算組件實(shí)例高度。并計(jì)算查詢項(xiàng)高度。resize事件要在組件銷毀前的生命周期中進(jìn)行銷毀。不影響其他組件。
高度判斷和圖標(biāo)的顯示與隱藏
首先圖標(biāo)盒子綁定狀態(tài),用來顯示和隱藏。 其次外層盒子需要設(shè)置一個(gè)高度臨界點(diǎn),即多大高度時(shí)不折疊,超過了這個(gè)高度就折疊。盒子高度需要你計(jì)算,比如,你需要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獲取組件實(shí)例以及獲取ref屬性的使用
- vue3的生命周期onMounted、onBeforeUnmount、setup的使用
- css3 transition和transform的使用
- offsetHeight和offsetWidth的使用
到此這篇關(guān)于vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的文章就介紹到這了,更多相關(guān)vue3搜索項(xiàng)折疊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參
這篇文章主要介紹了vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
vue導(dǎo)航欄部分的動(dòng)態(tài)渲染實(shí)例
今天小編就為大家分享一篇vue導(dǎo)航欄部分的動(dòng)態(tài)渲染實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue.js中window.onresize的超詳細(xì)使用方法
這篇文章主要給大家介紹了關(guān)于vue.js中window.onresize的超詳細(xì)使用方法,window.onresize 是直接給window的onresize屬性綁定事件,只能有一個(gè),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境
這篇文章主要介紹了詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
vue-router中關(guān)于children的使用方法
這篇文章主要介紹了vue-router中關(guān)于children的使用方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
利用Vue Native構(gòu)建移動(dòng)應(yīng)用的全過程記錄
VueNative是一個(gè)使用JavaScript構(gòu)建跨平臺(tái)原生移動(dòng)應(yīng)用程序的框架m這篇文章主要給大家介紹了關(guān)于如何利用Vue Native構(gòu)建移動(dòng)應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08

