vue弹窗组件

基于VUE的input双向绑定机制

// 弹窗
<template>
  <div>
    <div v-show="visible" :value="value" class="mod">
      <div class="mod_body">
        <div v-if="useClose" class="close" @click="cancel">X</div>
        <div class="mod_title" v-if="title">{{title}}</div>
        <div class="slot">
          <slot/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: Boolean,
      default: false
    },
    // 是否出现关闭
    useClose: {
      type: Boolean,
      default: true
    },
    //标题
    title: {
      type: String
    }
  },
  data() {
    return {
      visible: false,
      scrollTop: 0
    };
  },
  watch: {
    value(val) {
      this.visible = val;
      const body = window.document.body;
      if (val) {
        this.scrollTop =
          document.body.scrollTop || document.documentElement.scrollTop;
        body.style.position = "fixed";
        body.style.width = "100%";
        body.style.top = -this.scrollTop + "px";
      } else {
        body.style.position = "relative";
        body.style.top = 0;
        document.body.scrollTop = this.scrollTop;
      }
    },
    visible(val) {
      this.$emit("input", val);
    }
  },
  mounted() {
    if (this.value) {
      this.visible = true;
    }
  },
  methods: {
    cancel() {
      this.visible = false;
    }
  }
};
</script>

<style scoped lang="scss" rel="stylesheet/scss" >
// @import '~@/assets/css/style.less';
.mod {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  //   height: 100vh;
  background-color: rgba(0, 0, 0, 0.73);
  z-index: 999;
  .mod_body {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateY(-60%);
    margin-left: -35%;
    border-radius: 6px;
    width: 70%;
    background-color: #fff;
    padding-bottom: 20px;
    .close {
      position: absolute;
      top: 0;
      right: 0;
      width: 38px;
      height: 38px;
      //   background-image: url('~@/assets/image/red-pack/close.png');
      background-size: 100% 100%;
      background-repeat: no-repeat;
    }
    .slot {
      padding: 0 20px;
      height: 72%;
      overflow-x: hidden;
      overflow-y: auto;
      -webkit-overflow-scrolling: touch;
    }
  }
  .mod_title {
    font-size: 16px;
    color: #4a4a4a;
    text-align: center;
    line-height: 23px;
    padding: 38px 25px 0 25px;
  }
}
</style>

 

使用

<template>    
  <alert-mod v-model="showModal" :title="'xxx_title'">xxx_body</alert-mod>
</template>

import alertMod from "@/alertMod.vue";
export default {
  components: { alertMod },
  data() {
    return {
      showModal: true
    };
  },
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值