实现ElementUI Dialog宽度响应式变化

转载原文链接:https://www.cnblogs.com/aquilahkj/p/11309257.html

在ElementUI的Dialog中,需要实现其宽度随浏览器宽度变化而变化,并设定默认值,当浏览器宽度大于该值时,Dialog保持该宽度,小于该值时,使用100%宽度。
代码使用 window.onresize 事件触发变化,具体Demo代码如下:

<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button type="primary" icon="el-icon-edit" @click="dialogFormVisible=true">
        打开
      </el-button>
    </div>
    <el-dialog title="Test" :visible.sync="dialogFormVisible" :width="dialogWidth">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="110px">
        <el-row :gutter="10">
          <el-col>
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="temp.title"
                placeholder="请输入培训标题"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">
          取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'DialogWidthDemo',
  components: {
  },
  data() {
    return {
      temp: {
        title: ''
      },
      dialogWidth: 0,
      dialogFormVisible: false
    }
  },
  created() {
    this.setDialogWidth()
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.setDialogWidth()
      })()
    }
  },
  methods: {
    setDialogWidth() {
      console.log(document.body.clientWidth)
      var val = document.body.clientWidth
      const def = 800 // 默认宽度
      if (val < def) {
        this.dialogWidth = '100%'
      } else {
        this.dialogWidth = def + 'px'
      }
    }
  }
}
</script>

<style scoped>

</style>

我的效果如下:
浏览器全屏:
在这里插入图片描述

手机端:
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给 el-dialog 实现响应式,可以使用 CSS 的媒体查询和 flex 布局。具体实现步骤如下: 1. 在 el-dialog 的外层包裹一个 div 容器,设置该容器为 flex 布局,并将子元素(el-dialog)的宽度设置为 100%。 2. 使用 CSS 媒体查询来设置 el-dialog 在不同屏幕尺寸下的样式,如: ``` @media screen and (max-width: 768px) { .dialog-container { flex-direction: column; } .dialog-container .el-dialog { width: 100%; } } ``` 在上述代码中,当屏幕宽度小于 768px 时,将容器的 flex-direction 设置为 column(纵向排列),并将 el-dialog宽度设置为 100%。 3. 根据需要设置 el-dialog 的最大宽度和最大高度,以保证在不同屏幕尺寸下都能正常显示。 下面是一个示例代码: ```html <template> <div class="dialog-container"> <el-dialog title="对话框标题" :visible.sync="dialogVisible" :max-width="maxWidth" :max-height="maxHeight" > <p>对话框内容</p> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, maxWidth: '800px', maxHeight: '600px' } } } </script> <style> .dialog-container { display: flex; justify-content: center; align-items: center; } @media screen and (max-width: 768px) { .dialog-container { flex-direction: column; } .dialog-container .el-dialog { width: 100%; } } </style> ``` 在上述示例中,我们将 el-dialog 包裹在一个名为 dialog-container 的 div 容器中,使用 flex 布局将其居中显示。在不同屏幕尺寸下,容器的 flex-direction 会发生变化,从而实现el-dialog响应式布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值