vue實(shí)現(xiàn)滑動(dòng)驗(yàn)證條
更新時(shí)間:2022年03月02日 14:52:14 作者:Ezerbel
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滑動(dòng)驗(yàn)證條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue實(shí)現(xiàn)滑動(dòng)驗(yàn)證條的具體代碼,供大家參考,具體內(nèi)容如下
效果


代碼
VerifySlider.vue
<template>
? <div
? ? ref="dragDiv"
? ? class="drag"
? >
? ? <div ref="drag_bg" class="drag_bg" />
? ? <div ref="drag_text" class="drag_text">{{ confirmWords }}</div>
? ? <div class="slide-check-tip">請(qǐng)滑動(dòng)驗(yàn)證</div>
? ? <div
? ? ? ref="moveDiv"
? ? ? :class="{'handler_ok_bg':confirmSuccess}"
? ? ? class="handler handler_bg"
? ? ? style="position: absolute;top: 0px;left: 0px;"
? ? ? @mousedown="mousedownFn($event)"
? ? />
? </div>
</template>
<script>
export default {
? data() {
? ? return {
? ? ? beginClientX: 0, /* 距離屏幕左端距離 */
? ? ? mousePressing: false, /* 是否正則按壓滑動(dòng)條 */
? ? ? maxwidth: '', /* 拖動(dòng)最大寬度,依據(jù)滑塊寬度算出來的 */
? ? ? confirmWords: '按住滑塊,拖動(dòng)到最右邊', /* 滑塊文字 */
? ? ? confirmSuccess: false /* 驗(yàn)證成功判斷 */
? ? };
? },
? mounted() {
? ? document.getElementsByTagName('html')[0].addEventListener('mousemove', this.mouseMoveFn);
? ? document.getElementsByTagName('html')[0].addEventListener('mouseup', this.moseUpFn);
? },
? methods: {
? ? mousedownFn: function(e) {
? ? ? console.log(' mousedownFn ');
? ? ? this.initMaxWidth();
? ? ? if (!this.confirmSuccess) {
? ? ? ? e.preventDefault && e.preventDefault(); // 阻止文字選中等 瀏覽器默認(rèn)事件
? ? ? ? this.mousePressing = true;
? ? ? ? this.beginClientX = e.clientX;
? ? ? }
? ? }, // mousedoen 事件
? ? successFunction() {
? ? ? this.confirmSuccess = true;
? ? ? this.confirmWords = '驗(yàn)證通過';
? ? ? if (window.addEventListener) {
? ? ? ? document.getElementsByTagName('html')[0].removeEventListener('mousemove', this.mouseMoveFn);
? ? ? ? document.getElementsByTagName('html')[0].removeEventListener('mouseup', this.moseUpFn);
? ? ? } else {
? ? ? ? document.getElementsByTagName('html')[0].removeEventListener('mouseup', () => { });
? ? ? }
? ? ? this.$refs.drag_text.style.color = '#fff';
? ? ? this.$refs.moveDiv.style.left = this.maxwidth + 'px';
? ? ? this.$refs.drag_bg.style.width = this.maxwidth + 'px';
? ? }, // 驗(yàn)證成功函數(shù)
? ? mouseMoveFn(e) {
? ? ? if (this.mousePressing) {
? ? ? ? console.log('mouseMoveFn');
? ? ? ? const width = e.clientX - this.beginClientX;
? ? ? ? if (width > 0 && width <= this.maxwidth) {
? ? ? ? ? this.$refs.moveDiv.style.left = width + 'px';
? ? ? ? ? this.$refs.drag_bg.style.width = width + 'px';
? ? ? ? } else if (width > this.maxwidth) {
? ? ? ? ? this.successFunction();
? ? ? ? }
? ? ? }
? ? }, // mousemove事件
? ? moseUpFn(e) {
? ? ? console.log('moseUpFn');
? ? ? this.mousePressing = false;
? ? ? var width = e.clientX - this.beginClientX;
? ? ? if (width < this.maxwidth) {
? ? ? ? // document.getElementsByClassName('handler')[0].style.left = 0 + 'px'
? ? ? ? // document.getElementsByClassName('drag_bg')[0].style.width = 0 + 'px'
? ? ? ? this.$refs.moveDiv.style.left = 0 + 'px';
? ? ? ? this.$refs.drag_bg.style.width = 0 + 'px';
? ? ? }
? ? }, // mouseup事件,
? ? initMaxWidth() {
? ? ? this.maxwidth = this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
? ? ? // this.maxwidth = 338
? ? },
? ? // 將滑動(dòng)條重新至于最左側(cè)
? ? reset() {
? ? ? this.$refs.moveDiv.style.left = 0 + 'px';
? ? ? this.$refs.drag_bg.style.width = 0 + 'px';
? ? ? this.$refs.drag_text.style.color = '#333';
? ? ? this.confirmSuccess = false;
? ? ? this.mousePressing = false;
? ? ? this.maxwidth = this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
? ? ? document.getElementsByTagName('html')[0].addEventListener('mousemove', this.mouseMoveFn);
? ? ? document.getElementsByTagName('html')[0].addEventListener('mouseup', this.moseUpFn);
? ? }
? }
};
</script>
<style scoped>
.slide-check-tip{
? position: absolute;
? display: inline-block;
? top: 0px;
? right: -85px;
? /* border: 1px solid red; */
? color: #999;
? font-size: smaller;
}
.drag {
? position: relative;
? background-color: #e8e8e8;
? width: 70%;
? height: 34px;
? line-height: 34px;
? text-align: center;
}
.handler {
? width: 40px;
? height: 32px;
? border: 1px solid #ccc;
? cursor: move;
}
.handler_bg {
? background: #fff
? ? url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bX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TEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==')
? ? no-repeat center;
}
.drag_bg {
? background-color: #7ac23c;
? height: 34px;
? width: 0px;
}
.drag_text {
? position: absolute;
? top: 0px;
? width: 100%;
? color: #333;
? text-align: center;
? -moz-user-select: none;
? -webkit-user-select: none;
? user-select: none;
? -o-user-select: none;
? -ms-user-select: none;
}
</style>以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證功能
- vue自定義開發(fā)滑動(dòng)圖片驗(yàn)證組件
- vue實(shí)現(xiàn)登錄滑動(dòng)拼圖驗(yàn)證
- vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證
- Vue實(shí)現(xiàn)滑動(dòng)驗(yàn)證功能
- Vue實(shí)現(xiàn)滑動(dòng)拼圖驗(yàn)證碼功能
- 使用Vue 實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼功能
- vue 登錄滑動(dòng)驗(yàn)證實(shí)現(xiàn)代碼
- Vue.js圖片滑動(dòng)驗(yàn)證的實(shí)現(xiàn)示例
相關(guān)文章
深入探討Vue計(jì)算屬性與監(jiān)聽器的區(qū)別和用途
在Vue的開發(fā)中,計(jì)算屬性(Computed Properties)和監(jiān)聽器(Watchers)是兩種非常重要的概念,它們都用于響應(yīng)式地處理數(shù)據(jù)變化,本文將帶你深入了解計(jì)算屬性和監(jiān)聽器的區(qū)別,以及在何時(shí)使用它們,感興趣的朋友可以參考下2023-09-09
一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn)
這篇文章主要介紹了一文詳解Vue3中使用ref獲取元素節(jié)點(diǎn),本文介紹在vue3的setup中使用composition?API獲取元素節(jié)點(diǎn)的幾種方法,需要的朋友可以參考一下2022-07-07
vue.js中使用微信掃一掃解決invalid signature問題(完美解決)
這篇文章主要介紹了vue.js中使用微信掃一掃解決invalid signature問題(推薦),本文通過實(shí)例代碼給出解決方法,代碼簡單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue實(shí)現(xiàn)美團(tuán)app的影院推薦選座功能【推薦】
大家都經(jīng)常使用美團(tuán)app買電影票,很多朋友不知道她的功能是怎么實(shí)現(xiàn)的,作為我程序員一枚對(duì)它的算法很好奇,今天小編就把基于Vue實(shí)現(xiàn)美團(tuán)app的影院推薦選座功能分享到腳本之家平臺(tái),感興趣的朋友一起看看吧2018-08-08

