vue怎样实现dialog弹框放大缩小不出页面代码怎么写
可以使用Vue组件的方式来实现弹框的放大缩小,具体的实现可以参考以下代码:
<template>
<div class="dialog-wrapper" ref="dialogWrapper" @click="closeDialog">
<div class="dialog" ref="dialog" @click.stop>
<div class="dialog-header">
<span>{{ title }}</span>
<span class="dialog-close" @click="closeDialog">x</span>
</div>
<div class="dialog-content">
<slot></slot>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Dialog",
props: {
title: {
type: String,
default: "Dialog",
},
},
data() {
return {
isFullScreen: false,
};
},
methods: {
closeDialog() {
this.$emit("close");
},
toggleFullScreen() {
this.isFullScreen = !this.isFullScreen;
if (this.isFullScreen) {
this.$refs.dialog.classList.add("full-screen");
} else {
this.$refs.dialog.classList.remove("full-screen");
}
},
},
};
</script>
<style scoped>
.dialog-wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
}
.dialog {
background-color: #fff;
border-radius: 8px;
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 500px;
max-height: 80vh;
overflow: auto;
transition: all 0.3s ease-in-out;
}
.dialog-header {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 18px;
font-weight: bold;
margin-bottom: 10px;
}
.dialog-close {
cursor: pointer;
}
.full-screen {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100vw;
height: 100vh;
max-height: none;
border-radius: 0;
}
</style>
在上述代码中,我们定义了一个名为Dialog的Vue组件,该组件使用了插槽来动态渲染弹框的内容。组件中包含一个dialog-wrapper和一个dialog元素,dialog-wrapper用于实现弹框居中,并且在点击空白处时关闭弹框,dialog元素则是弹框的具体内容。
在data中,我们定义了一个isFullScreen变量,用于记录弹框是否处于全屏状态。在toggleFullScreen方法中,我们通过切换isFullScreen变量的值来实现弹框的放大缩小,同时也通过classList来添加或删除full-screen类,来实现弹框的样式变化。
最后,我们在Dialog组件中使用了@click.stop来阻止事件冒泡,以避免点击弹框内容时关闭弹框。在样式中,我们使用了position:fixed来实现弹框的固定定位,使用了max-height和overflow:auto来实现弹框内容超出时的滚动条
原文地址: http://www.cveoy.top/t/topic/g1kR 著作权归作者所有。请勿转载和采集!