vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的思路詳解
我們?cè)谧隽斜聿樵兊臅r(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue自定義組件雙向綁定實(shí)現(xiàn)原理及方法詳解
這篇文章主要介紹了Vue自定義組件雙向綁定實(shí)現(xiàn)原理及方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
vue實(shí)現(xiàn)頁面加載動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁面加載動(dòng)畫效果,vue頁面出現(xiàn)正在加載的初始頁面與實(shí)現(xiàn)動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
element自定義 多文件上傳 觸發(fā)多次on-change問題
這篇文章主要介紹了element自定義 多文件上傳 觸發(fā)多次on-change問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
詳解使用vue-admin-template的優(yōu)化歷程
這篇文章主要介紹了詳解使用vue-admin-template的優(yōu)化歷程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
深入學(xué)習(xí)Vue nextTick的用法及原理
這篇文章主要介紹了深入學(xué)習(xí)Vue nextTick的用法及原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
vue中for循環(huán)更改數(shù)據(jù)的實(shí)例代碼(數(shù)據(jù)變化但頁面數(shù)據(jù)未變)
這篇文章主要介紹了vue中for循環(huán)更改數(shù)據(jù)的實(shí)例代碼(數(shù)據(jù)變化但頁面數(shù)據(jù)未變)的相關(guān)資料,需要的朋友可以參考下2017-09-09
Vue項(xiàng)目中使用iView組件庫設(shè)置樣式不生效的解決方案
這篇文章主要介紹了Vue項(xiàng)目中使用iView組件庫設(shè)置樣式不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

