vue輸入框組件開發(fā)過程詳解
更新時間:2022年03月01日 13:43:12 作者:GlimmerL
這篇文章主要為大家詳細(xì)介紹了vue輸入框組件開發(fā)過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue輸入框組件開發(fā)過程的具體代碼,供大家參考,具體內(nèi)容如下
input-number.js
function isValueNumber(value) { ? ? return(/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9]*$)|(^-?0{1}$)/).test(value + ''); } Vue.component('input-number',{ ?? ?template: '\ ?? ?<div class=input-number>\ ?? ??? ?<input\ ?? ??? ??? ?type="text"\ ?? ??? ??? ?:value="currentValue"\ ?? ??? ??? ?@change="handleChange"\ ?? ??? ??? ?@focus="keyControl">\ ?? ??? ?<button \ ?? ??? ??? ?@click="handleDown" \ ?? ??? ??? ?:disabled="currentValue<=min">-</button>\ ?? ??? ?<button \ ?? ??? ??? ?@click="handleUp" \ ?? ??? ??? ?:disabled="currentValue>=max">+</button>\ ?? ?</div>', ?? ?data: function (){ ?? ??? ?return { ?? ??? ??? ?currentValue: this.value, ?? ??? ??? ?currentStep: this.$parent.step ?? ??? ?} ?? ?}, ?? ?watch: { ?? ??? ?currentValue: function (val){ ?? ??? ??? ?this.$emit('input',val); ?? ??? ??? ?this.$emit('on-change',val); ?? ??? ?}, ?? ??? ?value: function(val){ ?? ??? ??? ?this.updateValue(val); ?? ??? ?} ?? ?}, ?? ?methods: { ?? ??? ?handleDown: function(){ ?? ??? ??? ?if(this.currentValue<=this.min) return; ?? ??? ??? ?this.currentValue-=this.currentStep; ?? ??? ?}, ?? ??? ?handleUp: function(){ ?? ??? ??? ?if(this.currentValue>=this.max) return; ?? ??? ??? ?this.currentValue+=this.currentStep; ?? ??? ?}, ?? ??? ?updateValue: function(val){ ?? ??? ??? ?if(val>this.max) val=this.max; ?? ??? ??? ?if(val<this.min) val=this.min; ?? ??? ??? ?this.currentValue=val; ?? ??? ?}, ?? ??? ?handleChange: function(event){ ? ? ? ? ? ? var val = event.target.value.trim(); ? ? ? ? ? ? var max = this.max; ? ? ? ? ? ? var min = this.min; ? ? ? ? ? ? if(isValueNumber(val)) { ? ? ? ? ? ? ? ? val = Number(val); ? ? ? ? ? ? ? ? this.currentValue = val; ? ? ? ? ? ? ? ? if(val > max) { ? ? ? ? ? ? ? ? ? ? this.current = max; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if(val < min) { ? ? ? ? ? ? ? ? ? ? this.current = min; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? //如果輸入的不是數(shù)字,將輸入的內(nèi)容重置為之前的currentValue ? ? ? ? ? ? ? ? event.target.value = this.currentValue; ? ? ? ? ? ? } ?? ??? ??? ? ?? ??? ?}, ?? ??? ?keyControl: function(){ ?? ??? ??? ?var _this=this; ?? ??? ??? ?$(window).keydown(function(e){ ?? ??? ??? ??? ?if($('input')){ ?? ??? ??? ??? ??? ?if(e.keyCode==38){ ?? ??? ??? ??? ??? ??? ?_this.handleUp(); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?else if(e.keyCode==40){ ?? ??? ??? ??? ??? ??? ?_this.handleDown(); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ?} ?? ?}, ?? ?mounted: function(){ ?? ??? ?this.updateValue(this.value); ?? ?}, ?? ?props:{ ?? ??? ?max:{ ?? ??? ??? ?type: Number, ?? ??? ??? ?default: Infinity ?? ??? ?}, ?? ??? ?min: { ?? ??? ??? ?type: Number, ?? ??? ??? ?default: -Infinity ?? ??? ?}, ?? ??? ?value: { ?? ??? ??? ?type:Number, ?? ??? ??? ?default: 0 ?? ??? ?}, ?? ??? ?step: { ?? ??? ??? ?type:Number, ?? ??? ??? ?default: 1 ?? ??? ?} ?? ?} })
index.js
var app=new Vue({ ?? ?el: "#app", ?? ?data: { ?? ??? ?value: 5, ?? ??? ?step: 10 ?? ?} })
index.html
<!DOCTYPE html> <html> ?? ?<head> ?? ??? ?<meta charset="utf-8" /> ?? ??? ?<title></title> ?? ??? ?<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> ?? ??? ?<script src="js/vue.js" type="text/javascript" charset="utf-8"></script> ?? ?</head> ?? ?<body> ?? ??? ?<div id="app"> ?? ??? ??? ?<input-number v-model="value" :max="100" :min="0"></input-number> ?? ??? ?</div> ?? ??? ?<script src="js/input-number.js" type="text/javascript" charset="utf-8"></script> ?? ??? ?<script src="js/index.js" type="text/javascript" charset="utf-8"></script> ?? ?</body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
前端vue3項(xiàng)目中百度地圖的使用api以及操作實(shí)例
最近項(xiàng)目要用到百度地圖api,好久沒用到地圖,就百度了一番,但是找了好幾篇文章,發(fā)現(xiàn)都沒辦法成功實(shí)現(xiàn),現(xiàn)將方法記錄如下,下面這篇文章主要給大家介紹了關(guān)于前端vue3項(xiàng)目中百度地圖的使用api以及操作實(shí)例,需要的朋友可以參考下2023-05-05記一次vue-webpack項(xiàng)目優(yōu)化實(shí)踐詳解
這篇文章主要介紹了記一次vue-webpack項(xiàng)目優(yōu)化實(shí)踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02vue 修改 data 數(shù)據(jù)問題并實(shí)時顯示的方法
今天小編就為大家分享一篇vue 修改 data 數(shù)據(jù)問題并實(shí)時顯示的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08利用Vue實(shí)現(xiàn)卡牌翻轉(zhuǎn)的特效
這篇文章主要介紹了如何利用Vue實(shí)現(xiàn)一個春節(jié)抽??撁?,采用了卡牌翻轉(zhuǎn)的形式。文中的實(shí)現(xiàn)方法講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-02-02