点击页面其他位置,弹窗消失

功能:

1、点击消息,显示消息弹窗

2、再次点击消息,弹窗消失

3、点击页面弹窗外其他地方,弹窗消失

实现:

消息弹窗封装为一个组件

<div @mouseenter="mouseenter()" @mouseleave="mouseleave()">
  <div v-show="open">
    ......
  </div>
</div>
data(){
  return{
    // 弹窗状态
    open: false,
    // 鼠标是否在弹窗上
    isExist: false,
  }
}
mounted(){
  // this对象在系统中随时会改变,var that = this之后,that不会改变,仍然指向之前的this,这样就不会
  // 找不到原来的对象
  var that = this
  // 获取到的为当前VueComponent对象
  console.log(this)
  
  window.addEventListener('click',function(){
    // window对象
    console.log(this)
    // 当前VueComponent对象
    console.log(that)
    
    if(that.isExist === true){
      // 如果鼠标停靠在弹窗上点击,弹窗不消失
      that.open = true
    }else{
      that.messageAlertNo()
    }
  })
}
mouseenter() {
  this.isExist = true
},
mouseleave() {
  this.isExist = false
},
messageAlert() {
  this.isExist = !this.isExist
  this.open = !this.open
},
messageAlertNo() {
  if (this.open === true) {
    this.open = false
    this.isExist = false
  }
}, 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值