天天看点

自适应弹框宽度

<div class="dialog_wrapper" @click.self="close()" v-show="visible">
    <div class="dialog-setting" v-show="visible" :style="{width:dialogWidth}">
    <!-- 弹框的内容(用的是element框架的弹框)--->
    </div>
 </div>
           
<script>
export default {
  props: {
    visible: Boolean,
  },
  watch: {
    visible(value) {
      if (value) {
        this.dialogWidth = innerWidth();
        window.onresize = () => {
          return (() => {
            this.dialogWidth = innerWidth();
          })();
        };
      } else {
        window.onresize = null;
      }
    }
  },
  data() {
    return {
      dialogWidth: "100%"
    };
  },
  methods: {
    close() {
      this.updateVisible(false);
    },
    updateVisible(val) {
      this.$emit("update:visible", val);
    }
  }
};
function innerWidth() {
  if (window.innerWidth >= 960) {
    return "960px";
  } else if (window.innerWidth <= 450) {
    return "450px";
  } else {
    return "100%";
  }
}
</script>