移动端自定义输入框的vue组件 ----input
<style scoped lang="less">
.keyboard {
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", STHeiti, "Microsoft Yahei", Tahoma, Simsun, sans-serif;
user-select: none;
font-size: 16px;
width: 100%;
}
.input-box {
display: flex;
align-items: center;
justify-content: space-between;
line-height: 120px;
background-color: #fff;
border-radius: 8px;
font-size: 64px;
.label {
text-align: center;
width: 119px;
position: relative;
&::after {
content: "";
position: absolute;
/* display: inline-block; */
height: 64px;
top: 28px;
right: 0;
width: 2px;
background-color: rgb(221, 221, 221);
}
}
.content {
display: flex;
flex: 1;
.input {
color: #313131;
}
.cursor {
background-color: #4788c5;
width: 2px;
height: 64px;
margin-top: 28px;
margin-left: 2px;
}
.placeholder {
height: 120px;
padding-left: 38px;
}
.currency {}
}
}
</style>
<template>
<div class="keyboard">
<!-- 自定义输入框 -->
<div class="input-box" @touchstart.stop="focus">
<!-- 左侧标签 -->
<p class="label">¥ </p>
<!-- 右侧内容 -->
<div class="content">
<p class="placeholder" v-show="val.length === 0">
{{value}}
</p>
<!-- 光标 -->
<p class="cursor" :style="{visibility: cursor ? 'visible' : 'hidden'}"></p>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'KeyboardInput',
components: {
},
created() {
/*闪烁光标*/
this.blinkCursor();
// document.addEventListener('touchstart', () => {
// this.blur();
// });
},
props: {
value: '',
inter: {
default: 5
},
decimal: {
default: 2
},
// label: {
// default: '消费金额'
// },
placeholder: {
default: '158898'
}
},
data() {
return {
cursor: false,
keyboard: false,
/*value 在组件中的值*/
val: '',
aIllegal: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0..', '.'],
cursorDuration: 600,
bodyHeight: '',
bodyOverflow: ''
}
},
methods: {
/*focus*/
focus() {
/*显示键盘*/
this.showKeyboard();
/*显示光标*/
this.showCursor();
},
blinkCursor() {
clearInterval(this.intervalID);
this.intervalID = setInterval(() => {
this.cursor = !this.cursor;
}, this.cursorDuration);
},
unblinkCursor() {
clearInterval(this.intervalID);
},
/*blur*/
blur() {
/*隐藏光标*/
this.hideCursor();
/*停止光标闪烁*/
this.unblinkCursor();
/*隐藏键盘*/
this.hideKeyboard();
/*
附加 00, 如果用户输入了一个以 . 结尾的值就点完成了,
那么这个时候就在后面加上00
*/
this.toCompletion();
/*通知父组件, 老子值出来了*/
/*
校验用户输入的金额是不是为 0, 如果是的话, 直接重置
*/
this.checkValue();
this.notify();
},
showKeyboard() {
this.keyboard = true;
},
hideKeyboard() {
this.keyboard = false;
},
showCursor() {
this.cursor = true;
},
hideCursor() {
this.cursor = false;
},
checkValue() {
if (parseFloat(this.val) === 0) {
this.val = '';
}
},
/*判读是否需要加0*/
toCompletion() {
let list = this.value.split('.');
if (typeof list[1] === 'undefined') {
if (this.val !== '') {
this.val = this.val + '.';
this.completion(this.decimal);
}
} else {
if (list[1].length < this.decimal) {
this.completion(this.decimal - list[1].length);
}
}
},
completion(len) {
let v = '';
for (let i = 0; i < len; i++) {
v = v + '0';
}
this.val = this.val + v;
},
notify() {
this.$emit('input', this.val);
},
del() {
/*删除值并不会触发值的校验, 所以需要手动再触发一次*/
this.val = this.val.slice(0, -1);
this.notify();
},
/*输入*/
typing(value) {
/*如果是点击删除*/
if (value === '') {
this.del();
}
/*保存旧的值*/
let oldValue = this.val;
/*获取新的值*/
this.val = this.val + value;
/*检验新值, 如果没有通过检测, 恢复值*/
if (!this.passCheck(this.val)) {
this.val = oldValue;
return;
}
/*为了让外界同步输入, 需要发送事件*/
this.notify();
},
passCheck(val) {
/*验证规则*/
let aRules = [
this.illegalInput,
this.illegalValue,
this.accuracy
]
return aRules.every((item) => {
return item(val);
});
},
illegalInput(val) {
if (this.aIllegal.indexOf(val) > -1) {
return false;
}
return true;
},
/*非法值*/
illegalValue(val) {
if (parseFloat(val) != val) {
return false;
}
return true;
},
/*验证精度*/
accuracy(val) {
let v = val.split('.')
if (v[0].length > this.inter) {
return false;
}
if (v[1] && (v[1].length) > this.decimal) {
return false;
}
return true;
}
}
}
</script>
移动端自定义输入框的vue组件 ----input的更多相关文章
- 移动端自定义键盘的vue组件 ----keyboard
<style scoped lang="less"> .keyboard { /* height: 250px; */ width: 100%; position: f ...
- 移动端无限滚动 TScroll.vue组件
// 先看使用TScroll.vue的几个demo 1.https://sorrowx.github.io/TScroll/#/ 2. https://sorrowx.github.io/TScrol ...
- Vue.js 桌面端自定义滚动条组件|vue美化滚动条VScroll
基于vue.js开发的小巧PC端自定义滚动条组件VScroll. 前段时间有给大家分享一个vue桌面端弹框组件,今天再分享最近开发的一个vue pc端自定义滚动条组件. vscroll 一款基于vue ...
- webAPP如何实现移动端拍照上传(Vue组件示例)?
摘要:使用HTML5编写移动Web应用,主要是为了尝试一下“一套代码多处运行”,一个webapp几乎可以不加修改的运行在PC/Android/iOS等上面运行.但是写到现在觉得虽然这种方式弊大于利,不 ...
- 在vue组件的stylus样式总 取消search类型的input按钮中默认样式
在vue组件的stylus样式中 取消search类型的input按钮中默认样式 记录一个坑 环境 Vue组件 使用了Stylus的CSS风格. 问题 input输入框使用了 type="s ...
- vue组件、自定义指令、路由
1.vue组件 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的 ...
- 基于React.js网页版弹窗|react pc端自定义对话框组件RLayer
基于React.js实现PC桌面端自定义弹窗组件RLayer. 前几天有分享一个Vue网页版弹框组件,今天分享一个最新开发的React PC桌面端自定义对话框组件. RLayer 一款基于react. ...
- 使用VUE组件创建SpreadJS自定义单元格(二)
在上篇中,我们介绍了如何通过设置runtimeCompiler为true,在Vue中实现了动态创建电子表格组件.想了解具体内容可看点击查看使用VUE组件创建SpreadJS自定义单元格(一). 但是在 ...
- Vue组件绑定自定义事件
Vue组件使用v-on绑定自定义事件: 可以分为3步理解: 1.在组件模板中按照正常事件机制绑定事件: template: '<button v-on:click="increment ...
随机推荐
- css样式高级技巧-选择器
用<div>元素为网页 在编写样式表时,我们经常要用div元素来包装内容: <div> <p>Here are two paragraphs of content& ...
- postgres优化项及linux上pg操作记录
1.linux切换到pg命令: $ su - postgres $ psql postgres=# 2.查看/退出pg ps -ef |grep postgres postgres=# \q 3.一般 ...
- 文本数据和mysql 里面的数据比较
实现读取TXT文件中的内容然后存到内存,然后将内存中的数据和mysql 数据库里面某张表数据的字段做一个比较,如果比较内存中的数据在mysql 里存在则不做处理,如果不存在则将该数据插入mysql数据 ...
- JS与Jquery的事件委托机制
传送:http://www.ituring.com.cn/article/467 概念: 什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委 ...
- [MtOI2019]幽灵乐团
题目 一个很暴力的辣鸡做法 考虑到两个数的\(\gcd\)是所有质数次幂取\(\min\),两个数的\(\rm lcm\)是所有质数次幂取\(\max\),于是最后的答案一定是\(\prod p_i^ ...
- 【2018ACM/ICPC网络赛】徐州赛区
呃.自闭了自闭了.我才不会说我写D写到昏天黑地呢. I Characters with Hash 题目链接:https://nanti.jisuanke.com/t/31461 题意:给你一个字符串 ...
- ES6 学习 -- 解构赋值
一.数组解构 **数组解构,解构出来的值跟数组下标是一一对应的,如果左边变量多于右边数组,则左边后面部分变量值为undefined,如果右边数组元素个数多于左边解构变量个数,则左边变量全都有值,且一一 ...
- linux下svn 客户端使用方式
输入 yes 开始 checkout服务器上的文件到本地目录 2.将文件 添加文件到某个目录下(是svn的服务器checkout下来的目录中) 3. 提交到服务器 4 .即可在服务器目录看到(wind ...
- lnmp高人笔记
http://www.cnblogs.com/qizekai/p/5878774.html http://www.cnblogs.com/qizekai/p/5879461.html
- MySQL数据库之DQL(数据查询语言)
1.MySQL之DQL查询AS CONCAT LIKE的使用 (1)select 列名1,列名2,...... from 表名 [where 条件] 查询所有字段用*,不带where条件的话,就会把表 ...