vue中元素高度auto的收起展开动画

本文介绍如何使用CSS3的transition属性来创建元素的展开与收起动画效果。通过设置元素的固定高度并利用Vue.js的DOM操作,实现平滑过渡的动画效果。文章详细解释了在动画前手动设置元素高度的重要性,以及如何通过监听DOM更新来获取并应用正确的高度。

<!-- 
**** 说明:
****   使用css3的transition动画来做
****	但是这个属性只能用于固定高度的元素,所有在进行动画之前就要手动的给这个元素设置一个高度值,这样就能正常使用动画了
****
****
-->
<template>
    <div>
        <button @click="fun_animate">
            <span v-if="!is_show">展开</span>
            <span v-if="is_show">收起</span>
        </button>
        <div class="child_right_slider transition_dom" ref="box">
            <p>content</p>
            <p>content</p>
            <p>content</p>
            <p>content</p>
        </div>
    </div>
</template>
<script>
var _this = null;
export default {
    data() {
        return {
            is_show: true,
            height: ""
        };
    },
    methods: {
        fun_animate: function() {
            if (_this.is_show) {
                this.$refs.box.style.height = "0";
            } else {
                this.$refs.box.style.height = _this.height;
            }
            _this.is_show = !_this.is_show;
        },
        fun_get_list: function() {
            // 1:网络请求获取数据后会把元素自动撑开,我们要做的就是让这个元素做展开收起动画
            // 做法: 1:获取这个元素的高度
            //        2:吧获取的高度赋值给这个元素,这样这个元素就是高度固定的了。nextTick用于dom刷新后执行
            _this.$nextTick(function() {
                var height = _this.$refs.box.offsetHeight;
                console.log(height);
                this.$refs.box.style.height = height + "px";
                _this.height = height + "px";
            });
        }
    },
    mounted() {
        _this = this;
        _this.fun_get_list();
    }
};
</script>

<style lang="scss">
.transition_dom {
    transition: all 0.3s linear 0s;
}
.child_right_slider {
    display: inline-block;
    width: 300px;
    background-color: black;
    color: white;
}
</style>

 

Vue 中结合 Element UI 的 `el-form` 实现自适应布局的展开收起功能,可以通过以下方法实现: ### 1. 动态控制表单项的显示与隐藏 使用 `v-if` 或 `v-show` 控制某些表单项的显示状态。通过一个变量控制展开收起的状态切换,例如 `isExpanded`。 ```html <el-form ref="form" :model="form" label-width="auto"> <el-form-item label="基础字段"> <el-input v-model="form.baseField"></el-input> </el-form-item> <transition name="slide"> <div v-show="isExpanded"> <el-form-item label="扩展字段 1"> <el-input v-model="form.extendedField1"></el-input> </el-form-item> <el-form-item label="扩展字段 2"> <el-select v-model="form.extendedField2" placeholder="请选择"> <el-option label="选项1" value="1"></el-option> <el-option label="选项2" value="2"></el-option> </el-select> </el-form-item> </div> </transition> <el-button @click="toggleExpand">{{ isExpanded ? '收起' : '展开' }}</el-button> </el-form> ``` ```javascript export default { data() { return { form: { baseField: '', extendedField1: '', extendedField2: '' }, isExpanded: false }; }, methods: { toggleExpand() { this.isExpanded = !this.isExpanded; } } }; ``` ### 2. 使用 `label-width="auto"` 实现标签自适应宽度 在 `el-form` 组件中设置 `label-width="auto"`,以确保标签宽度根据内容自动调整,避免因标签过长导致布局错乱 [^1]。 ```html <el-form ref="form" :model="form" label-width="auto"> <!-- 表单内容 --> </el-form> ``` ### 3. 自适应布局与响应式设计 为了确保表单在不同屏幕尺寸下都能良好显示,可以结合 CSS 媒体查询或使用 `el-row` 和 `el-col` 实现栅格化布局。 ```html <el-row :gutter="20"> <el-col :span="6"> <el-form-item label="字段A"> <el-input v-model="form.fieldA"></el-input> </el-form-item> </el-col> <el-col :span="6"> <el-form-item label="字段B"> <el-input v-model="form.fieldB"></el-input> </el-form-item> </el-col> </el-row> ``` ### 4. 动画过渡效果 使用 `<transition>` 组件为展开收起操作添加动画效果,提升用户体验。 ```html <transition name="slide"> <div v-show="isExpanded"> <!-- 扩展表单项 --> </div> </transition> ``` ```css .slide-enter-active, .slide-leave-active { transition: max-height 0.3s ease-out; overflow: hidden; } .slide-enter, .slide-leave-to { max-height: 0; } ``` ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值