Home > Article > Web Front-end > How to achieve folding of search items if they exceed n lines in vue3
Realize the layout of components.
Bind listening events and destroy listening events
Height judgment and display and hiding of icons
Outer box (limit height), folded icon or text (used to show and hide redundant rows), slot (digging a hole to occupy the search row).
You need to bind a resize event. The resize event is triggered when the window size changes. When the resize event is triggered, we need to recalculate the height of the box query item to determine whether it needs to be folded or displayed. The mounted life cycle triggers the calculation of component instance height. And calculate the query item height. The resize event must be destroyed in the life cycle before the component is destroyed. Does not affect other components.
First, the icon box binding state is used to display and hide. Secondly, the outer box needs to set a height critical point, that is, the height at which it will not fold, and if it exceeds this height, it will fold. You need to calculate the height of the box. For example, if you need 4 rows without folding, you need to calculate the height of the four rows and add the height of the icon. If it is greater than the height, the icon is displayed, if it is less than the height, the icon is hidden.
<template> <div class="fold_box"> <div class="fold_box_over" : :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>
<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> 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>
The above is the detailed content of How to achieve folding of search items if they exceed n lines in vue3. For more information, please follow other related articles on the PHP Chinese website!