首页 > 其他 > 详细

分享一个自定义vue组件——带搜索的模拟下拉框

时间:2019-11-28 20:00:18      阅读:804      评论:0      收藏:0      [点我收藏+]

先上效果图:

技术分享图片

 

 

组件特点:

  • 模拟下拉框
  • 可输入文字搜索选项,keyup或input事件触发搜索(并优化了原生keyup和input事件的问题)
  • 数据源异步加载
  • 滚动加载选项数据
  • 同一页面可重复使用该组件

 

技术工具说明:

  • 基础框架 vue.js
  • jquery.js辅助
  • 样式 element-ui.js
  • 注意:非单页面,非前后端分离开发

 

写这篇博客我主要想做两件事,首先逐步讲解如何实现这个组件,然后会放上完整代码,附上使用说明。


进入正文:

创建demo.html

  1. 引用jquery,vue,element-ui(cdn)
  2. 创建一个id="app"的根元素
  3. 在body元素尾部添加<script></script>标签,创建vue实例(这部分代码也可以单独写一个js文件)
<!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>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
    <script src="https://cdn.bootcss.com/element-ui/2.10.1/index.js"></script>
</head>
<body>
    <div id="app">
        
    </div>

    <script>
        new Vue({
            el: #app
        })
    </script>
</body>
</html>

 

新建文件夹component,并在里面新建cw-input-select.js和cw-input-select.css文件

结构如下:

| demo.html

| component

  | cw-input-select.js

  | cw-input-select.css

 

在demo.html header标签里引入cw-input-select.js和cw-input-select.css

    <!-- 引入cw-input-select组件 -->
    <link rel="stylesheet" href="component/cw-input-select.css">
    <script src="component/cw-input-select.js"></script>

 

编写cw-input-select.js

1. 编写一个Vue全局组件基本架子

Vue.component(‘cw-input-select‘, {
    template: ‘<div></div>‘,
    data: function () {
        return {}
    },
    created () {},
    methods: {},
})

 

 2. 编写静态(无动态数据无交互)的下拉框组件代码,并用静态数组模拟选项(后面替换动态数据源)

component/cw-input-select.js

Vue.component(‘cw-input-select‘, {
    template: `<div class="cw-input-select_wrap">
    <div class="cw-input-select">
        <div class="cw-input-select_box">
            <span>请选择</span>
        </div>
        <div class="cw-input-select_pop">
            <input type="text" class="cw-input-select_ipt" placeholder="搜索" />
            <ul class="cw-input-select_options">
                <li>
                    <span>不限</span>
                </li>
                <li v-for="option in optionsList">
                    {{option}}
                </li>
            </ul>
            <span class="cw-input-select_arrow"></span>
        </div>
    </div>
</div>`,
    data: function () {
        return {
            optionsList: [‘选项1‘, ‘选项2‘, ‘选项3‘]
        }
    },
    created () {},
    methods: {},
})

 3. 编写 component/cw-input-select.css

技术分享图片
/* 基础样式reset */
input {
    box-sizing: border-box;
    outline: 0;
}
ul {
    margin: 0;
    padding: 0;
}
ul, li {
    list-style: none;
}

/* 组件整体容器 */
.cw-input-select_wrap {
    position: relative;
    width: 198px;
    height: 28px;
    font-size: 14px;
}

/* 组件内容 */
.cw-input-select {
    width: 198px;
    position: absolute;
}

/* 基本下拉框 */
.cw-input-select_box {
    height: 28px;
    border: 1px solid #b7b7b7;
    border-radius: 4px;
    background-color: white;
    position: relative;
    cursor: pointer;
}

/* 基本下拉框里面右边的线体上下箭头(可旋转) */
.cw-input-select_box::after {
    content: ‘‘;
    display: block;
    position: absolute;
    right: 10px;
    top: 8px;
    border-top: 1px solid #C0C4CC;
    border-right: 1px solid #C0C4CC;
    border-radius: 1px;
    width: 8px;
    height: 8px;
    background: transparent;
    transition: transform .3s, -webkit-transform .3s;
    transform: rotate(135deg);
    z-index: 10;
}

/* 基本下拉框 文字 */
.cw-input-select_box > span {
    display: inline-block;
    line-height: 28px;
    padding: 0 30px 0 15px;
    font-size: 12px;
    color: #606266;
    /* 文字超出用省略号 */
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 100%;
    overflow: hidden;
}

/* 选项列表盒子 */
.cw-input-select_pop {
    position: relative;
    background-color: white;
    border: 1px solid #E4E7ED;
    border-radius: 4px;
    max-height: 274px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    margin-top: 12px;
    padding: 5px;
    box-sizing: border-box;
    z-index: 9;
}

/* 选项列表盒子上方的三角形箭头 */
.cw-input-select_arrow {
    position: absolute;
    display: block;
    width: 0;
    height: 0;
    border-color: transparent;
    border-style: solid;
    border-width: 6px;
    filter: drop-shadow(0 2px 12px rgba(0, 0, 0, .03));
    top: -6px;
    left: 35px;
    margin-right: 3px;
    border-top-width: 0;
    border-bottom-color: #fff;
    z-index: 99;
}

/* 选项列表盒子里面的输入框 */
.cw-input-select .cw-input-select_pop .cw-input-select_ipt {
    position: absolute;
    top: 5px;
    z-index: 99;
    height: 24px;
    line-height: 20px;
    width: 94%;
    border: 1px solid #DCDFE6;
    padding: 1px 5px;
    font-size: 12px;
}

/* 选项列表内容 */
.cw-input-select_options {
    display: block;
    margin-top: 26px;
    max-height: 234px;
}

/* 选项单元 */
.cw-input-select_options li {
    padding: 8px 15px;
    background-color: white;
    cursor: pointer;
}

/* 选项单元hover */
.cw-input-select_options li:hover {
    background-color: #F5F7FA;
}
View Code

css注释我尽量写详细,因为很难把css分开讲解。

 

至此,效果如下:

技术分享图片

 

-

 

(未完待续)

 

  

分享一个自定义vue组件——带搜索的模拟下拉框

原文:https://www.cnblogs.com/cathy1024/p/11952986.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!