使用konva和vue-konva庫(kù)實(shí)現(xiàn)拖拽滑塊驗(yàn)證功能
1. 在vue項(xiàng)目中安裝konva和vue-konva庫(kù)
npm install konva vue-konva --save-dev
2. 引入vue-konva庫(kù)
import VueKonva from ‘vue-konva';
Vue.use(VueKonva)
3. 創(chuàng)建單獨(dú)的滑塊驗(yàn)證組件 Captcha.vue,在相應(yīng)的頁(yè)面中引入使用即可
<template>
<v-stage :config="Config.stage">
<v-layer ref="layer">
<!-- 背景組 -->
<v-group :config="Config.group">
<v-rect :config="Config.rect"></v-rect>
<v-text :config="Config.text"></v-text>
</v-group>
<!-- 遮罩層組 -->
<v-group :config="Config.group">
<v-rect :config="Config.coverRect" ref="coverRect"></v-rect>
<v-text :config="Config.coverText" v-if="success" ref="coverText"></v-text>
</v-group>
<!-- 滑塊組 -->
<v-group :config="Config.moveGroup" ref="moveGroup" @mouseover="moveGroupMouseOver" @mouseout="moveGroupMouseOut" @mousedown="moveGroupMouseDown" @mouseup="moveGroupStop">
<v-rect :config="Config.moveRect" ref="moveRect"></v-rect>
<!-- 驗(yàn)證成功組 -->
<v-group :config="Config.group" v-if="success">
<v-circle :config="Config.succCircle" ref="succCircle"></v-circle>
<v-line :config="Config.succLine"></v-line>
</v-group>
<v-group :config="Config.moveGroup_l" v-else>
<v-line :config="Config.moveLine1"></v-line>
<v-line :config="Config.moveLine2"></v-line>
</v-group>
</v-group>
</v-layer>
</v-stage>
</template>
<script>
/*
* captchaConfig // 屬性 {width:330, height: 36} 組件的寬高
* eventCaptcha // 驗(yàn)證成功的回調(diào)
*/
let _$mouseDown = false; // 鼠標(biāo)是否在滑塊組中按下,因?yàn)楹蚳tml沒(méi)有綁定,所以沒(méi)有放在data中,并以_$開頭
export default {
props: {
captchaConfig: {
type: Object,
default: () => ({
width: 330, // 寬度
height: 36, // 高度
}),
},
},
data() {
const { width, height } = this.captchaConfig;
let Config = {
stage: {
width: width,
height: height,
},
group: {
x: 0,
y: 0,
},
rect: {
width: width,
height: height,
fill: '#e8e8e8',
},
text: {
x: 0,
y: 0,
width: width,
height: height,
text: '請(qǐng)按住滑塊,拖動(dòng)到最右邊',
fontSize: 14,
fontFamily: '微軟雅黑',
align: 'center',
lineHeight: parseFloat(height / 14),
},
//滑塊組
moveGroup: {
draggable: true,
},
moveRect: {
x: 0.5,
y: 0.5,
width: height - 1,
height: height - 1,
fill: '#fff',
stroke: '#8d92a1',
strokeWidth: 1,
},
moveGroup_l: {
x: height / 3,
y: height / 3,
},
moveLine1: {
x: 0,
y: 0,
points: [0, 0, height / 6, height / 6, 0, height / 3],
stroke: '#8d92a1',
strokeWidth: 1,
lineCap: 'round',
lineJoin: 'round',
},
moveLine2: {
x: height / 6,
y: 0,
points: [0, 0, height / 6, height / 6, 0, height / 3],
stroke: '#8d92a1',
strokeWidth: 1,
lineCap: 'round',
lineJoin: 'round',
},
//創(chuàng)建遮罩層組
coverRect: {
width: height / 2,
height: height,
fill: '#8d92a1',
opacity: 0.8,
},
coverText: {
x: 0,
y: 0,
width: width - height,
height: height,
align: 'center',
text: '驗(yàn)證成功',
fontSize: 14,
fontFamily: '微軟雅黑',
fontStyle: 'bold',
fill: '#fff',
lineHeight: parseFloat(height / 14),
},
//驗(yàn)證成功組
succCircle: {
x: height / 2,
y: height / 2,
radius: height / 4,
fill: '#8d92a1',
},
succLine: {
points: [height / 2 - height / 4 / 2, height / 2, height / 2 - height / 4 / 8, height / 2 + height / 4 / 2, height / 2 + height / 4 / 2, height / 2 - height / 4 / 2],
stroke: '#fff',
strokeWidth: 1,
lineCap: 'round',
lineJoin: 'round',
},
};
return {
Config,
success: 0, // 標(biāo)記是否驗(yàn)證成功 0 失敗 1 成功
};
},
mounted() {
// 給document綁定鼠標(biāo)抬起事件
document.addEventListener('mouseup', this.moveGroupStop);
// 在組件注銷的時(shí)候取消綁定
this.$once('hook:beforeDestroy', () => {
document.removeEventListener('mouseup', this.moveGroupStop);
});
// 給滑塊組綁定拖拽監(jiān)聽
this.$refs.moveGroup.getNode().dragBoundFunc((pos) => {
const { width, height } = this.captchaConfig;
let moveGroup = this.$refs.moveGroup.getNode();
let moveRect = this.$refs.moveRect.getNode();
let coverRect = this.$refs.coverRect.getNode();
let moveX = moveGroup.getAttrs().x ? moveGroup.getAttrs().x : 0;
coverRect.width(moveX + height / 2);
if (pos.x >= width - height) {
if (this.success == 0) {
this.success = 1;
this.$emit('eventCaptcha');
}
coverRect.opacity(1);
}
if (this.success == 0) {
if (pos.x < 0) {
return {
x: 0,
y: moveGroup.getAbsolutePosition().y,
};
} else if (pos.x > width - height) {
return {
x: width - height,
y: moveGroup.getAbsolutePosition().y,
};
} else {
return {
x: pos.x,
y: moveGroup.getAbsolutePosition().y,
};
}
} else {
return {
x: width - height,
y: moveGroup.getAbsolutePosition().y,
};
}
});
},
methods: {
// 鼠標(biāo)進(jìn)入滑塊組
moveGroupMouseOver() {
document.body.style.cursor = 'pointer';
},
// 鼠標(biāo)移出滑塊組
moveGroupMouseOut() {
document.body.style.cursor = 'default';
},
// 鼠標(biāo)按下
moveGroupMouseDown() {
_$mouseDown = true; // 只有在滑塊組點(diǎn)擊鼠標(biāo)才被視作要點(diǎn)擊滑動(dòng)驗(yàn)證
},
// 鼠標(biāo)抬起
moveGroupStop(e) {
if (!_$mouseDown) return;
_$mouseDown = false;
document.body.style.cursor = 'default'; // 鼠標(biāo)恢復(fù)指針狀態(tài)
if (this.success == 0) {
this.$refs.moveGroup.getNode().to({
x: 0,
duration: 0.3,
});
this.$refs.coverRect.getNode().to({
width: this.captchaConfig.height / 2,
duration: 0.3,
});
}
},
},
};
</script>
4. 最終效果



簡(jiǎn)單的滑塊驗(yàn)證功能實(shí)現(xiàn),可直接在vue頁(yè)面中引入使用。konva庫(kù):https://konvajs.org/
到此這篇關(guān)于使用konva和vue-konva完成前端拖拽滑塊驗(yàn)證功能的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)konva和vue-konva拖拽滑塊驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue+elementui實(shí)現(xiàn)拖住滑塊拼圖驗(yàn)證
- vue實(shí)現(xiàn)移動(dòng)滑塊驗(yàn)證
- vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證
- Vue集成阿里云做滑塊驗(yàn)證的實(shí)踐
- Vue3+Vue-cli4項(xiàng)目中使用騰訊滑塊驗(yàn)證碼的方法
- vue實(shí)現(xiàn)簡(jiǎn)單滑塊驗(yàn)證
- VUE接入騰訊驗(yàn)證碼功能(滑塊驗(yàn)證)備忘
- Vue 實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證功能(只有css+js沒(méi)有后臺(tái)驗(yàn)證步驟)
- vue-cli 自定義指令directive 添加驗(yàn)證滑塊示例
- vue封裝圖片滑塊驗(yàn)證組件的方法
相關(guān)文章
vue中使用AJAX實(shí)現(xiàn)讀取來(lái)自XML文件的信息
這篇文章主要為大家詳細(xì)介紹了vue中如何使用AJAX實(shí)現(xiàn)讀取來(lái)自XML文件的信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下2023-12-12
vue-next-admin項(xiàng)目使用cdn加速詳細(xì)配置
這篇文章主要為大家介紹了vue-next-admin項(xiàng)目使用cdn加速的詳細(xì)配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue實(shí)現(xiàn)導(dǎo)航菜單和編輯文本的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)導(dǎo)航菜單和編輯文本功能的方法,文中示例代碼非常詳細(xì),幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
element?ui?el-calendar日歷組件使用方法總結(jié)
這篇文章主要給大家介紹了關(guān)于element?ui?el-calendar日歷組件使用方法的相關(guān)資料,elementui是一款基于Vue.js的UI框架,其中的日歷組件calendar是elementui中非常常用的組件之一,需要的朋友可以參考下2023-07-07
vue.js通過(guò)路由實(shí)現(xiàn)經(jīng)典的三欄布局實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了vue.js通過(guò)路由實(shí)現(xiàn)經(jīng)典的三欄布局,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
elementUI中input回車觸發(fā)頁(yè)面刷新問(wèn)題與解決方法
這篇文章主要給大家介紹了關(guān)于elementUI中input回車觸發(fā)頁(yè)面刷新問(wèn)題與解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用elementUI具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07
element el-table如何實(shí)現(xiàn)表格動(dòng)態(tài)增加/刪除/編輯表格行(帶校驗(yàn)規(guī)則)
這篇文章主要介紹了element el-table如何實(shí)現(xiàn)表格動(dòng)態(tài)增加/刪除/編輯表格行(帶校驗(yàn)規(guī)則),本篇文章記錄el-table增加一行可編輯的數(shù)據(jù)列,進(jìn)行增刪改,感興趣的朋友跟隨小編一起看看吧2024-07-07

