溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Vue.js中使用下拉菜單組件

發(fā)布時間:2021-03-23 16:06:18 來源:億速云 閱讀:185 作者:Leah 欄目:web開發(fā)

這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)怎么在Vue.js中使用下拉菜單組件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>可從外部關(guān)閉的下拉菜單</title>
 <link rel="stylesheet" type="text/css" href="style.css" >
</head>
<body>
 <div id="app" v-cloak>
 <div class="main" v-clickoutside="handleClose">
  <button @click="show = !show">點擊顯示下拉菜單</button>
  <div class="dropdown" v-show="show">
  <p>下拉框的內(nèi)容,點擊外面區(qū)域可以關(guān)閉</p>
  </div>
 </div>
 </div>
 <script src="https://unpkg.com/vue/dist/vue.js"></script>
 <script src="clickoutside.js"></script>
 <script src="index.js"></script>
</body>
</html>

根實例 index.js

var app = new Vue({
 el: '#app',
 data: {
 show: false
 },
 methods: {
 handleClose () {
  this.show = false;
 }
 }
});

下拉框組件 clickoutside.js

Vue.directive('clickoutside',{
 bind: function (el, binding, vnode) {
 function documentHandler(e) {
  if(el.contains(e.target)){
  return false;
  }
  if(binding.expression){
  binding.value(e);
  }
 }
 el.__vueClickOutside__ = documentHandler;
 document.addEventListener('click',documentHandler);
 },
 unbind: function (el, binding) {
 document.removeEventListener('click', el.__vueClickOutside__);
 delete el.__vueClickOutside__;
 }
});

樣式表

[v-cloak]{
 display: none;
}
.main{
 width: 125px;
}
button{
 display: block;
 width: 100%;
 color: #fff;
 background-color: #39f;
 border: 0;
 padding: 6px;
 text-align: center;
 font-size: 12px;
 border-radius: 4px;
 cursor: pointer;
 outline: none;
 position: relative;
}
button:active{
 top:1px;
 left: 1px;
}
.dropdown{
 width:100%;
 height: 150px;
 margin: 5px 0;
 font-size: 12px;
 background-color: #fff;
 border-radius: 4px;
 box-shadow: 0 1px 6px rgba(0,0,0,.2);
}
.dropdown p{
 display: inline-block;
 padding: 6px;
}

上述就是小編為大家分享的怎么在Vue.js中使用下拉菜單組件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI