Two methods of clicking blank area to close pop up window in vue

  • 2021-10-15 09:52:49
  • OfStack

1. Approach 1

On the front page, one name is main in the outer container, that is, ref= "main". When bankSwitch is true, a pop-up window appears


<div class="selectedBorder" ref="main">
	<div class="bankItem" v-if="bankSwitch == true">
		 Hello, I'm the content part of the pop-up window 
 	</div>
</div>

The events triggered are as follows:

Home page, first create a click event in the global: bodyCloseMenus

Event action: When the main container is clicked (this. refs. main & & ! this. refs. main. contains (e. target), and when the pop-up window appears (self. bankSwitch = = true), click the blank area to close the pop-up window (self. bankSwitch = false)

Finally, remove the click event before logging off the page


 mounted() {
  document.addEventListener("click", this.bodyCloseMenus);
 },
 methods:{
 bodyCloseMenus(e) {
   let self = this;
   if (this.$refs.main && !this.$refs.main.contains(e.target)) {
    if (self.bankSwitch == true){
     self.bankSwitch = false;
    }
   }
  },
beforeDestroy() {
  document.removeEventListener("click", this.bodyCloseMenus);
 },

2. Approach 2

On the front page, a bubble prevention event is defined in the outer container, namely @ click. stop. When bankSwitch is true, a pop-up window appears


<div class="selectedBorder" @click.stop>
	<div class="bankItem" v-if="bankSwitch == true">
		 Hello, I'm the content part of the pop-up window 
 	</div>
</div>

The events triggered are as follows:

Home page, first create a click event in the global: bodyCloseMenus

Event action: When the pop-up window appears (self. bankSwitch = = true), click the blank area to close the pop-up window (self. bankSwitch = false)

Finally, remove the click event before logging off the page


 mounted() {
  document.addEventListener("click", this.bodyCloseMenus);
 },
 methods:{
 bodyCloseMenus(e) {
   let self = this;
    if (self.bankSwitch == true){
     self.bankSwitch = false;
    }
  },
beforeDestroy() {
  document.removeEventListener("click", this.bodyCloseMenus);
 },

The above is the details of the two methods of clicking on the blank area to close the pop-up window in vue. Please pay attention to other related articles on this site for more information about clicking on the blank area to close the pop-up window in vue!


Related articles: