實例:vue中點擊空白區域關閉某個div圖層


<template>
  <div class="search" ref="searchMain">
    <el-input v-model="value" placeholder="請輸入搜索內容" ref="input"
      @focus="getFocus"
      @input="changeVal"
      @clear='clearCard'
      @blur="onBlurFocus"
      clearable style="width:300px">
    </el-input>
    <el-button type="primary" icon="el-icon-search" @click="onSearch"></el-button>
    
    <!-- 點擊空白區域關閉某個div圖層 -->
    <el-card>
      <el-button @click="togglePanel">點擊</el-button> 
      <div v-show="visible" ref="main">彈出層</div>
    </el-card>
  </div>
</template>

data() {
  return {
    visible: false
  }
} methods:{
// 點擊空白區域關閉某個div圖層 togglePanel () { this.visible ? this.hide() : this.show() }, show () { this.visible = true document.addEventListener('click', this.hidePanel, false) }, hide () { this.visible = false document.removeEventListener('click', this.hidePanel, false) }, hidePanel (e) { if (!this.$refs.searchMain.contains(e.target)) { this.hide() }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM