首页 > web前端 > Vue.js > 如何编写出vue.js菜单组件

如何编写出vue.js菜单组件

coldplay.xixi
发布: 2020-11-30 13:33:44
原创
3348 人浏览过

写出vue.js菜单组件的方法:首先使用【index.html】编写入口页面;然后使用【clickoutside.js】下拉框组件,代码为【Vue.directive('clickoutside'】;最后实现样式表。

如何编写出vue.js菜单组件

【相关文章推荐:vue.js
本教程操作环境:windows7系统、Vue2.9.6版,该方法适用于所有品牌电脑。

写出vue.js菜单组件的方法:

1、入口页面 index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<!DOCTYPE html>

<html>

<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>可从外部关闭的下拉菜单</title>

 <link rel="stylesheet" type="text/css" href="style.css" >

</head>

<body>

 <div id="app" v-cloak>

  <div v-clickoutside="handleClose">

   <button @click="show = !show">点击显示下拉菜单</button>

   <div v-show="show">

    <p>下拉框的内容,点击外面区域可以关闭</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>

登录后复制

2、根实例 index.js

1

2

3

4

5

6

7

8

9

10

11

var app = new Vue({

 el: &#39;#app&#39;,

 data: {

  show: false

 },

 methods: {

  handleClose () {

   this.show = false;

  }

 }

});

登录后复制

3、下拉框组件 clickoutside.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Vue.directive(&#39;clickoutside&#39;,{

 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(&#39;click&#39;,documentHandler);

 },

 unbind: function (el, binding) {

  document.removeEventListener(&#39;click&#39;, el.__vueClickOutside__);

  delete el.__vueClickOutside__;

 }

});

登录后复制

4、样式表

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

[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;

}

登录后复制

相关免费学习推荐:JavaScript(视频)

以上是如何编写出vue.js菜单组件的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板