vue实现左右伸缩(el-drawer自定义位置展开收缩)
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
·
实现需求
页面内容是左右布局,需求想让左侧内容可收缩,然后展示完全右侧内容。本来想着写原生的css+v-show动态判断即可,后来想到了element组件库有抽屉(el-drawer),顺便想尝试一下是否能自定义抽屉展开的位置,所以有了这篇文章。
实现效果

自定义抽屉(el-drawer)展开位置
<template>
<div>
<el-row style="margin-top: 1%" :gutter="20">
<!-- 左侧 列表 -->
<el-col style="height: 350px;" :span="span" :class="[span != '8' ? 'span1' : 'span1']">
<div style="position: relative; width: 100%; height: 100%">
<el-drawer
class="drawerClass"
style="position: absolute"
:append-to-body="false"
:modal="false"
:show-close="false"
:wrapperClosable="false"
size="100%"
:visible.sync="drawer"
direction="ltr"
>
<el-card class="box-card" style="position: relative">
<div>左侧内容</div>
</el-card>
</el-drawer>
<div
style="
position: absolute;
z-index: 999999999;
cursor: pointer;
top: 30%;
"
:class="[drawer ? 'imgright' : 'imgright1']"
@click="clickImg"
>
<img
v-show="!drawer"
style="height: 40px; width: 25px"
:src="require('@/assets/image/zhankai.png')"
alt=""
/>
<img
v-show="drawer"
style="height: 40px; width: 25px"
:src="require('@/assets/image/shousuo.png')"
alt=""
/>
</div>
</div>
</el-col>
<!-- 右侧 用户列表 -->
<el-col :span="span1" :class="[span1 != '15' ? 'span1' : 'span1']">
<el-card class="box-card">
<div>右侧内容</div>
</el-card>
</el-col>
</el-row>
</div>
</template>
实现原理
给el-drawer父级标签添加position: relative;
el-drawer属性调整:
:append-to-body=“false” 遮罩层是否插入至 body 元素上,
:modal=“false” 是否需要遮罩层
:show-close=“false” 是否显示关闭按钮
:wrapperClosable=“false” 点击遮罩层是否可以关闭 Drawer
js方法,点击的时候抽屉伸缩展开,并且给左侧右侧内容对应的宽度
export default {
data() {
return {
span: 8,
span1: 15,
drawer: true,
};
},
methods: {
clickImg() {
this.drawer = !this.drawer;
if (this.drawer) {
this.span = 8;
this.span1 = 15;
} else {
this.span = 1;
this.span1 = 23;
}
},
},
};
<style lang="scss" scoped>
.span1 {
transition: all 0.2s;
}
.imgright {
right: 0;
background-color: #f5f5f5;
transition: all 0.2s;
}
.imgright1 {
left: 0;
background-color: #fff;
transition: all 0.2s;
}
.drawerClass ::v-deep .el-drawer__container .el-drawer .el-drawer__header {
display: none;
}
</style>
以上只是尝试抽屉是否能自定义位置伸缩展开。当然有更简单的方法写个css动态判断宽度即可
第二种方法
<el-row style="margin-top: 1%">
<div style="display: flex; align-items: center">
<!-- 左侧 列表 -->
<div :class="[drawer ? 'left' : 'left1']">
<el-card class="box-card">
<div>左侧内容</div>
</el-card>
</div>
<!-- 折叠展开图片-->
<div
style="cursor: pointer; width: 5%"
:class="[drawer ? 'imgright' : 'imgright1']"
@click="clickImg"
>
<img
v-show="!drawer"
style="height: 40px; width: 25px"
:src="require('@/assets/image/zhankai.png')"
/>
<img
v-show="drawer"
style="height: 40px; width: 25px"
:src="require('@/assets/image/shousuo.png')"
/>
</div>
<!-- 右侧 用户列表 -->
<div :class="[drawer ? 'right' : 'right1']">
<el-card class="box-card">
<div>右侧内容</div>
</el-card>
</div>
</div>
</el-row>
methods: {
clickImg() {
this.drawer = !this.drawer;
},
},
.left {
width: 35%;
transition: all 0.2s;
}
.left1 {
width: 0%;
transition: all 0.2s;
}
.right {
width: 65%;
transition: all 0.2s;
}
.right1 {
width: 95%;
transition: all 0.2s;
}
.box-card {
height: 350px;
background-color: #ff6e6e;
}
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:3 个月前 )
9e887079
[skip ci] 1 年前
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)