前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue实现弹框组件

vue实现弹框组件

作者头像
治电小白菜
发布2020-08-25 15:59:02
1.7K0
发布2020-08-25 15:59:02
举报
文章被收录于专栏:技术综合

效果

问题总结

添加到body

代码语言:javascript
复制
this.$nextTick(() => {
  const body = document.querySelector('body')
  if (body.append) {
    body.append(this.$el)
  } else {
    body.appendChild(this.$el)
  }
})

禁止页面滚动

代码语言:javascript
复制
if (this.show) {
  document.body.style.height = '100vh'
  document.body.style['overflow-y'] = 'hidden'
} else {
  document.body.style.height = 'unset'
  document.body.style['overflow-y'] = 'auto'
}

切换页面或其他情况, 需要清除添加到body里的弹框

代码语言:javascript
复制
 destroyed () {
    // 如果被销毁, 则清除body里的dom
    if (this.appendBody && this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el)
    }
  }

组件源码

代码语言:javascript
复制
<template>
  <transition name="fade">
    <div class="b-popup" v-show="show">
      <transition name="b-fade">
        <div
          class="b-popup-mask"
          @click="close"
          v-show="hasMask && show">
        </div>
      </transition>
      <transition name="b-slide-up">
        <div
          v-show="show"
          class="b-popup-box b-slide-up"
          :class="contentPosition"
          :style="{background: popupBg, width: contentWidth, borderRadius: contentBorderRadius}">
          <div v-if="!noHeader" class="b-popup-header">
            <div class="iconfont icon-close" :class="closePosition" @click="close"></div>
            <div class="title">{{title}}</div>
          </div>
          <div class="b-popup-body">
            <slot name="body"></slot>
          </div>
        </div>
      </transition>
    </div>
  </transition>
</template>
<script>
export default {
  name: 'BPopup',
  props: {
    open: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '标题'
    },
    closePosition: {
      type: String,
      default: 'right'
    },
    noHeader: {
      type: Boolean,
      default: false
    },
    appendBody: {
      type: Boolean,
      default: true
    },
    popupBg: {
      type: String,
      default: '#fff'
    },
    hasMask: {
      type: Boolean,
      default: true
    },
    contentPosition: {
      type: String,
      default: 'bottom' // bottom, center, top
    },
    contentWidth: {
      type: String,
      default: '100%'
    },
    contentBorderRadius: {
      type: String,
      default: 'unset'
    }
  },
  model: {
    prop: 'open',
    event: 'update'
  },
  mounted () {
    if (this.appendBody) {
      this.$nextTick(() => {
        const body = document.querySelector('body')
        if (body.append) {
          body.append(this.$el)
        } else {
          body.appendChild(this.$el)
        }
      })
    }
    this.show = this.open
  },
  destroyed () {
    // 如果被销毁, 则清除body里的dom
    if (this.appendBody && this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el)
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    close () {
      this.show = false
      this.$emit('update', false)
    }
  },
  watch: {
    open () {
      this.show = this.open
    },
    show () {
      if (this.show) {
        document.body.style.height = '100vh'
        document.body.style['overflow-y'] = 'hidden'
      } else {
        document.body.style.height = 'unset'
        document.body.style['overflow-y'] = 'auto'
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.b-popup {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  z-index: 2;
  .b-popup-mask {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    pointer-events: auto;
    background-color: rgba(37,38,45,.7);
  }
  .b-popup-box {
    position: absolute;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
    overflow: auto;
    text-align: center;
    font-size: 57px;
    // background: #fff;
    z-index: 2;
    &.center {
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    &.top {
      top: 0;
    }
    .b-popup-header {
      padding: 70px 0;
      position: relative;
      border-bottom: 2px solid #e0e0e0;
    }
    .icon-close {
      position: absolute;
      font-size: 70px;
      color: #b8b8b8;
      &.right {
        right: 40px;
      }
      &.left {
        left: 40px;
      }
    }
  }
  .b-fade {
    &-enter, &-leave-to {
      opacity: 0;
    }
    &-enter-active, &-leave-active {
      transition: opacity 250ms;
    }
  }
  .b-slide-up {
    &-enter, &-leave-to {
      transform: translate3d(0, 100%, 0);
    }
    &-leave-active, &-enter-active {
      transition: transform 300ms cubic-bezier(0.165, 0.84, 0.44, 1);
    }
  }
}

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 效果
  • 问题总结
    • 添加到body
      • 禁止页面滚动
        • 切换页面或其他情况, 需要清除添加到body里的弹框
        • 组件源码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档