JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<div id="window">
<div class="item demo-1 f-ac">
<input v-for="(code, i) in codes.length" key="i"
type="tel"
ref="codes"
maxlength="1"
autocorrect="off"
autocomplete="off"
autocapitalize="off"
spellcheck="false"
v-model="codes[i]"
@focus="onFocus(i)"
@blur="onBlur(i)"
@keyup="onKeyUp(i)"
@keydown.delete="onDelete(i)"
@keydown.left.right="onJump(i)"
@paste="onPaste(i)">
</div>
<p>Codes: {{ codes.join('') }}</p>
</div>
.item{
width: 300px;
margin-bottom: 32px;
}
.f-ac {
display: flex;
align-items: center;
}
.demo-1{
input[type='tel']{
display: block;
width: 14%;
margin-right: 3.2%;
outline: none;
padding: 10px 0;
font-size: 20px;
font-weight: bold;
border: 2px solid #c7c9d3;
border-radius: 6px;
text-align: center;
transition: all ease .6s;
&:last-child{
margin-right: 0;
}
&:focus{
border-color: #333;
}
}
}
new Vue({
el: '#window',
data:{
codes: new Array(6).fill('')
},
methods:{
onFocus (index) {
const el = this.$refs.codes[index]
if (el.value) {
el.placeholder = el.value
el.prevValue = el.value
this.$set(this.codes, index, '')
}
},
onBlur (index) {
const el = this.$refs.codes[index]
if (el.prevValue && el.value === '') {
this.$set(this.codes, index, el.prevValue)
el.placeholder = ''
el.prevValue = null
}
},
onDelete (index) {
const el = this.$refs.codes[index]
// 删除内容处理
el.prevValue = ''
el.placeholder = ''
el.value = ''
if (index > 0) {
this.$refs.codes[index - 1].focus()
}
},
// 跳转到当前 Index 的下一格
onJump (index) {
const codesLen = this.codes.length
let nextIndex = null
if ( event.key === 'ArrowLeft' || event.key === 'Left') {
nextIndex = index > 0 ? index - 1 : codesLen - 1
} else if ( event.key === 'ArrowRight' || event.key === 'Right' ){
nextIndex = index < codesLen - 1 ? index + 1 : 0
}
this.$refs.codes[nextIndex].focus()
},
onKeyUp (index) {
const el = this.$refs.codes[index]
let val = el.value
// 快速输入问题解决, 将接收到输入事件,但此时 value 没有值。
const char = event.key
if (char.length === 1) val = char
// filter
val = val.replace(/[^0-9]/ig, "");
if (!val) {
el.focus()
} else if (index < this.codes.length - 1){
this.$refs.codes[index + 1].focus()
} else {
// 在输入到最后一个内容时,建议让其失去焦点,以免过多的输入导致最后一位内容被覆盖
el.blur()
}
// 更新 Vue 内容
if (val !== this.codes[index]) this.$set(this.codes, index, val)
},
onPaste (index) {
const el = this.$refs.codes[index]
const paste = (event.clipboardData || window.clipboardData).getData('text')
// 过滤出符合条件的数字
// const match = paste.match(/(?<!\d)\d{6}(?!\d)/i) // 不被兼容
const match = ` ${paste}`.match(/[^\d](?<code>\d{6})(?!\d)/i)
const codes = match ? match.groups.code.toString() : ''
if(codes.length === this.codes.length && +codes == codes){
el.blur()
this.codes = [...codes]
}
}
}
})
Also see: Tab Triggers