vue自定义可输入的选择框组件
vue自定义可输入的选择框组件
props:
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| selectDataList | 下拉框中的内容 | Array | 空数组([]) |
| value | 输入框中的内容 | String | 空字符串("") |
| transfer | 是否将下拉框加入到body的直接子节点中,在 Tabs、带有 fixed 的 Table 列内使用时,建议添加此属性,它将不受父级样式影响,从而达到更好的效果 | Boolean | false |
| placeholder | 输入框中的提示信息 | String | 请选择 |
事件 events:
| 时间名 | 说明 | 返回值 |
|---|---|---|
| inputChange | 在输入框中改变值时才会触发,js改变输入框绑定的值不会触发 | 输入框中当前值 |
| selectChange | 选择下拉框内容发生改变时触发 | 当前选择的值 |
InputAndSelect.vue 文件
<style lang="scss" scoped>
.icon-unfold-transform {
-webkit-transform: rotate(180deg);
transform: rotate(180deg);
}
.select-dropdown {
position: absolute;
z-index: 1060;
will-change: top, left;
visibility: visible;
top: 28px;
left: 0px;
width: inherit;
max-height: 210px;
padding: 5px 0;
margin: 5px 0;
background-color: #fff;
box-sizing: border-box;
border-radius: 2px;
box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
overflow: auto;
.select-not-found {
text-align: center;
color: #999;
}
.select-dropdown-list {
.select-item {
height: 30px;
line-height: 16px;
display: flex;
align-items: baseline;
margin: 0;
padding: 7px 12px;
clear: both;
color: #333;
font-size: 12px;
white-space: nowrap;
list-style: none;
cursor: pointer;
transition: all .2s ease-in-out;
}
}
}
.select-input:hover{
border-color: #3597f5;
}
.select-input {
width: inherit;
height: 28px;
position: relative;
text-align: left;
box-sizing: border-box;
outline: 0;
background-color: #fff;
border-radius: 2px;
border: 1px solid #d7dde4;
-webkit-transition: all .2s ease-in-out;
transition: all .2s ease-in-out;
> input {
width: 100%;
vertical-align: top;
display: inline-block;
height: 28px;
line-height: 28px;
padding: 0 24px 0 8px;
font-size: 12px;
outline: 0;
border: none;
-webkit-box-sizing: border-box;
box-sizing: border-box;
color: #333;
background-color: transparent;
position: relative;
}
.select-arrow {
position: absolute;
right: 0;
top: -2px;
height: 28px;
line-height: 28px;
padding: 0 10px;
cursor: pointer;
> i {
font-size: 12px;
color: #9ea7b4;
transition: all .2s ease-in-out;
display: block;
}
}
}
</style>
<template>
<div style="width:inherit;">
<div class="parent" style="position: relative;width:inherit;">
<div class="select-input">
<input class="inputDom" :placeholder="placeholder" v-model="value" type="text" autocomplete="off"
@input="inputChange"
>
<div @click="changeIconDirection($event)" class="select-arrow">
<i class="iconfont icon-unfold"
:class="iconUnfoldClass"></i>
</div>
</div>
<div class="select-dropdown" :style="{display: iconUnfoldClass['icon-unfold-transform']?'block':'none'}">
<ul class="select-not-found" :style="{display: !isExistedSelectData?'block':'none'}">
<li>无匹配数据</li>
</ul>
<ul class="select-dropdown-list" :style="{display: isExistedSelectData?'block':'none'}">
<li @click="selectChange(item)" class="select-item" v-for="(item,index) in selectDataList"
:key="index">
{{item.label}}
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
export default {
name: "InputAndSelect",
props: {
selectDataList: {
type: Array,
default: [],
// 验证必须有label和value
validator: function (value) {
return value.every(item => {
return item.hasOwnProperty("label") && item.hasOwnProperty("value")
})
}
},
// value 与 this.$emit('input', this.value) 配合实现input 输入框 v-model双向绑定功能
value: {
type: String,
default: "",
},
// 是否将下拉框加入到body的直接子节点中
// 在 Tabs、带有 fixed 的 Table 列内使用时,建议添加此属性,它将不受父级样式影响,从而达到更好的效果
transfer: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: "请选择",
}
},
data() {
return {
iconUnfoldClass: {
'icon-unfold-transform': false
},
//记录点击切换按钮时对应的事件
ev: null
}
},
methods: {
//在输入框中改变值时才会触发,js改变输入框绑定的值不会触发
inputChange() {
console.log("inputValue改变了====", this.value)
this.$emit('input', this.value)
this.$emit('inputChange', this.value)
},
//选择内容发生改变时触发
selectChange(item) {
this.value = item.value
this.iconUnfoldClass["icon-unfold-transform"] = false
this.$emit('input', this.value)
this.$emit('selectChange', item)
},
changeIconDirection(e) {
//点击切换图标按钮时,先保存当前点击的事件,在后面监听的click事件中,判断如果不是当前点击的事件,则收起下拉框
this.ev = e
this.iconUnfoldClass["icon-unfold-transform"] = !this.iconUnfoldClass["icon-unfold-transform"]
if (this.iconUnfoldClass["icon-unfold-transform"]) {
this.checkTransfer()
let inputDom = document.querySelector(".inputDom")
inputDom.focus()
}
},
checkTransfer() {
if (this.transfer) {
// 组件监听页面resize只能用addEventListener,否则不会生效
window.addEventListener('resize', this.checkTransfer, false)
// 监听scroll事件的事件传递必须使用捕获阶段,让外部元素事件先触发
// document.addEventListener('scroll', this.checkTransfer, true)
//window.innerHeight document.documentElement.clientHeight 都是获取可是去高度
//window.innerHeight获取的高度包含横向滚动条,而document.documentElement.clientHeight不包含横向滚动条
let bodyHeight = document.documentElement.clientHeight // body 可视区域高度
let matchHeight = this.matchDom.clientHeight // 匹配DOM的高度
let rect = this.matchParent.getBoundingClientRect() // 取出匹配父级DOM的矩形对象
// getBoundingClientRect.bottom为元素下边与页面上边的距离,所以元素下边与页面下边距离 = 页面高度 - getBoundingClientRect.bottom
let bottom = bodyHeight - rect.bottom
this.matchDom.style.left = rect.left + 'px' // 匹配DOM的left与父级一致
let dropdownWidth = rect.width + 'px' //下拉框宽度与父级dom一致
this.matchDom.style.width = dropdownWidth
if (bottom >= matchHeight) { // 父级距离页面下边的高度大于等于匹配DOM的高度,则往下展示
this.matchDom.style.bottom = 'auto'
this.matchDom.style.top = (rect.top + rect.height) + 'px' // 匹配DOM的top = 父级矩形对象top + 父级的高度
} else { // 父级距离页面下边的高度小于匹配DOM的高度,则往上展示
this.matchDom.style.top = 'auto'
this.matchDom.style.bottom = (bottom + rect.height) + 'px' // 匹配DOM的bottom = 父级矩形对象bottom + 父级的高度
}
}
},
// 点击切换图标按钮以外的其他地方触发,关闭下拉框
clickOther(e) {
// 判断如果不是当前点击的事件,则收起下拉框
if (this.ev == e) return
if (this.iconUnfoldClass["icon-unfold-transform"]) {
this.iconUnfoldClass["icon-unfold-transform"] = false
}
}
},
computed: {
isExistedSelectData() {
return this.selectDataList.length > 0 ? true : false
},
matchDom() { // 匹配框,需要相对于body
return this.$el.getElementsByClassName('select-dropdown')[0]
},
matchParent() { // 匹配框父级
return this.$el.getElementsByClassName('parent')[0]
}
},
created() {
if (this.transfer) {
// 将匹配的下拉框从原有位置移到body节点中
this.$nextTick(() => {
const body = document.querySelector('body')
// 将匹配DOM添加到body中
if (body.append) { // 在IE11中 document.appendChild会报错: javascript runtime error:HierarchyRequestError
body.append(this.matchDom)
} else {
body.appendChild(this.matchDom)
}
})
}
},
mounted() {
window.addEventListener("click", this.clickOther);
},
}
</script>
vue自定义可输入的选择框组件的更多相关文章
- 原生js实现一个自定义下拉单选选择框
浏览器自带的原生下拉框不太美观,而且各个浏览器表现也不一致,UI一般给的下拉框也是和原生的下拉框差别比较大的,这就需要自己写一个基本功能的下拉菜单/下拉选择框了.最近,把项目中用到的下拉框组件重新封装 ...
- CSS自定义select下拉选择框(不用其他标签模拟)
今天群里有人问到怎么自定义select下拉选择框的样式,于是群里就展开了激烈的讨论,刚开始一直就是考虑怎样使用纯CSS实现,把浏览器默认的样式覆盖掉,但最后均因兼容问题处理不好而失败告终,最后的解决方 ...
- 微信小程序之自定义select下拉选项框组件
知识点:组件,animation,获取当前点击元素的索引与内容 微信小程序中没有select下拉选项框,所以只有自定义.自定义的话,可以选择模板的方式,也可以选择组件的方式来创建. 这次我选择了组件, ...
- Nuxt/Vue自定义导航栏Topbar+标签栏Tabbar组件
基于Vue.js实现自定义Topbar+Tabbar组件|仿咸鱼底部凸起导航 最近一直在倒腾Nuxt项目,由于Nuxt.js是基于Vue.js的服务端渲染框架,只要是会vue,基本能很快上手了. 一般 ...
- [RN] React Native 自定义 底部 弹出 选择框 实现
React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...
- WeChat-SmallProgram:自定义select下拉选项框组件
1):创建组件所需的文件 2):自定义组件 CSS 及 JS 组件的wxml: <view class='com-selectBox'> <view class='com-sCont ...
- 关于隐式创建vue实例实现简化弹出框组件显示步骤
我们在使用vue写alert组件的时候,经常是定义了一个alert.vue,然后引入alert.vue,然后配置参数等等,非常繁琐,那有没有一种方式可以像window.alert("内容&q ...
- Flex AIR自定义Mobile的弹出框组件
做Flex Mobile开发的人应该知道,Flex为手机应用并没有提供弹出框组件,需要自定义. 通过查找文档.资料,我做出一个效果还算不错的弹出框组件,可以适用于手机设备上,不多讲,直接贴源码,相信对 ...
- 使用vue自定义简单的消息提示框
<style scoped> /** 弹窗动画*/ a { text-decoration: none } .drop-enter-active { /* 动画进入过程:0.5s */ t ...
随机推荐
- IDEA 2020.1.2安装破解激活 idea 2020.1.3下载 安装 一键破解
IDEA 2020.1.2 idea 2020.1.3下载 安装 破解 本项目只做个人学习研究之用,不得用于商业用途!若资金允许,请点击链接购买正版,谢谢合作!学生凭学生证可免费申请正版授权!创业公司 ...
- Linux常用命令(学习笔记)
命令编写以遇到的生产问题的前后为顺序进行记录 虚拟机的镜像是centos6.5版本,在这个版本下,我个人整理记录了一些在linux上常用的命令以及一些项目部署需要的jdk.tomcat.mysql等的 ...
- Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$
Linux切换用户时报错/.bash_profile: Permission denied,命令行(终端提示符)出现-bash-4.2$ 利用su - 切换用户时,发现有一个用户切时出现如下情况 [r ...
- web自动化 -- HTMLreport(二)测试报告输出内容居左对齐
一.需求痛点 1.报告输出内容是居中对齐,很难看 2.我们希望输出内容是居左对齐 3.痛点截图 二.解决办法 1.原因分析 HTMLreport的内容对齐方式,自然是修改HTMLreport的内容了 ...
- 【Nginx】并发量太高,Nginx扛不住?这次我错怪Nginx了!!
写在前面 最近,在服务器上搭建了一套压测环境,不为别的,就为压测下Nginx的性能,到底有没有传说中的那么牛逼!具体环境为:11台虚拟机,全部安装CentOS 6.8 64位操作系统,1台安装部署Ng ...
- php提取xml配置参数
demo1.php <?php class AddressManager{ private $addresses = array("ip地址1","ip地址2&qu ...
- java图片压缩工具类(指定压缩大小)
1:先导入依赖 <!--thumbnailator图片处理--> <dependency> <groupId>net.coobird</groupId> ...
- PHP acosh() 函数
实例 返回不同数的反双曲余弦: <?phpecho(acosh(7) . "<br>");echo(acosh(56) . "<br>&qu ...
- 这届 Showgirl行不行?AI告诉你谁是ChinaJoy上最漂亮的小姐姐
摘要: CJ开幕,顶着三伏天的酷暑高温,暴走一整天,就为了拍点漂亮小姐姐给大家看看. 一年一度的游戏视觉盛宴又来了! 作为一个游戏动漫控的肥宅,去CJ现场是必须的.除了看看游戏和动漫,各大游戏展台漂亮 ...
- Pr剪辑
目录 Pr剪辑教程 入门基础 创建序列类别 处理非正常序列 导出文件 导出设置 导入各类别素材 简单使用: 剪辑素材常用方法 剃刀工具 选择工具 波纹编辑工具 打入点和出点 剪辑速度 整个素材视频速度 ...