vue做弹出框红包,首次加载页面可以显示,第二次打开隐藏

<template>
	<div class="dialog-wrapper" v-if="ifImg">
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="30%"
        :show-close="false"
        custom-class="elDialog"
        :visible.sync="dialogVisible"
        :before-close="handleClose"
      >
      <!--点击图片时跳转-->
        <img :src="indexImg" @click="linkImg" />
        <div class="el-close">
          <i class="el-icon-error" @click="dialogVisible = false"></i>
        </div>
      </el-dialog>
    </div>
</template>
<script>
 data() {
    return {
      // 判断有没有宣传图
      ifImg: false,
      //图片路径
      indexImg: "",
      //点击红包时跳转路径
      indexro: "",
      // 控制红包的
      dialogVisible: false,
      		}
      }
	created(){
		this.getImage();
	},
	methods: {
	    // 点击宣传图片跳转页面
	    linkImg() {
	      window.open(this.indexro, "_blank"); // 新窗口打开外链接
	    },
	    getImage() {
	    	//判断sessionStorage是否含有userfirst,若为null,则第一次打开该页面,可以向后台发送请求,获取图片
	      if (window.sessionStorage.getItem("userfirst") == null) {
	        this.getJSON("后端接口地址").then((res) => {
	        //返回的为数组,若为空的话,是后端未设置该功能
	          if (res.data.length > 0) {
	          //isEnable根据后台数据判断是否需要展示弹出红包
	            if (res.data[0].isEnable) {
	            //在sessionStorage中存储会话级别的userfirst
	              window.sessionStorage.setItem("userfirst", 0);
	              //显示红包弹框
	              this.dialogVisible = true;
	              //显示el-dialog弹框
	              this.ifImg = true;
	              //给参数赋值
	              this.indexImg = res.data[0].picture;
	              this.indexro = res.data[0].link;
	            }
	          } else {
	            this.ifImg = false;
	          }
	        });
	      }
	    },
	    // 点击红包
	    handleClose() {
	      this.dialogVisible = false;
	    },
	    //发送请求
	    getJSON(url) {
		  return new Promise((resolve, reject) => {
		    var xhr = new XMLHttpRequest();
		    xhr.open("GET", url, true);
		    xhr.setRequestHeader("Content-type", "application/json");
		    xhr.onreadystatechange = function () {
		      if (this.readyState === 4) {
		        if (this.status === 200) {
		          resolve(JSON.parse(this.responseText), this);
		        } else {
		          let resJson = { code: this.status, response: this.response };
		          reject(resJson, this);
		        }
		      }
		    };
		    xhr.send();
		  });
		}
	  }
</script>


<style scoped lang="scss">
/*修改element中el-dialog自带的默认属性*/
.dialog-wrapper {
  margin-top: 0;
  /deep/ .el-dialog__wrapper {
    display: flex;
    align-items: center;
    .el-dialog {
      margin: 0 auto   !important;;
      background-color: rgba(0, 0, 0, 0) !important;
      box-shadow:0 0 0 0;
      .el-dialog__body {
        padding: 0px;
        /*去掉默认背景色*/
      	background-color: rgba(0, 0, 0, 0) !important;
        box-sizing: border-box;
      }
      .el-dialog__header {
        padding: 0px;
      }
    }
  }
}
.elDialog {
  height: 100%;
  img {
    width: 100%;
    height: 100%;
  }
  img:hover{
    cursor:pointer;
  }
  .el-close {
    position: absolute;
    width: 50px;
    height: 50px;
    top: -70px;
    right: -80px;
    font-size: 30px;
    text-align: center;
    line-height: 80px;
    color: rgb(165, 142, 142);
  }
}
</style>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武良神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值