用vue写一个前端滑动验证
时间: 2024-05-04 09:17:54 浏览: 136
使用Vue 实现滑动验证码功能
好的,下面是一个简单的前端滑动验证的示例,使用Vue框架和element-ui组件库实现:
首先,我们需要在页面上引入element-ui和vue.js:
```HTML
<!-- 引入 Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- 引入 element-ui CSS -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入 element-ui JS -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
```
然后,在Vue实例中定义一个data属性,用于存储验证状态和滑块位置:
```JavaScript
new Vue({
el: '#app',
data: {
sliderLeft: 0, // 滑块位置
isVerified: false // 验证状态
},
methods: {
verify() {
// 验证逻辑
},
reset() {
// 重置滑块位置和验证状态
}
}
})
```
接着,在页面上添加HTML结构,包括一个滑块和一个验证按钮:
```HTML
<div id="app">
<div class="slider-container">
<div class="slider" :style="{ left: sliderLeft + 'px' }"></div>
</div>
<div class="button-container">
<el-button type="primary" @click="verify">{{ isVerified ? '已验证' : '验证' }}</el-button>
<el-button type="danger" @click="reset">重置</el-button>
</div>
</div>
```
在CSS中定义滑块和按钮的样式:
```CSS
.slider-container {
width: 300px;
height: 50px;
line-height: 50px;
background-color: #ddd;
position: relative;
}
.slider {
width: 50px;
height: 50px;
background-color: #007aff;
position: absolute;
top: 0;
left: 0;
transition: left 0.3s;
}
.button-container {
margin-top: 20px;
}
```
最后,在Vue实例中添加滑块拖动的逻辑:
```JavaScript
new Vue({
el: '#app',
data: {
sliderLeft: 0,
isVerified: false
},
methods: {
verify() {
if (this.sliderLeft >= 250) {
// 验证通过
this.isVerified = true;
} else {
// 验证失败
this.$message.error('请拖动滑块到最右侧');
}
},
reset() {
this.sliderLeft = 0;
this.isVerified = false;
}
},
mounted() {
const slider = document.querySelector('.slider');
let isDragging = false;
let startX = 0;
let currentX = 0;
slider.addEventListener('mousedown', e => {
isDragging = true;
startX = e.clientX;
});
slider.addEventListener('mousemove', e => {
if (isDragging) {
const distance = e.clientX - startX;
currentX = Math.min(Math.max(0, currentX + distance), 250);
this.sliderLeft = currentX;
startX = e.clientX;
}
});
slider.addEventListener('mouseup', e => {
isDragging = false;
if (currentX >= 250) {
this.isVerified = true;
} else {
this.sliderLeft = 0;
currentX = 0;
}
});
}
})
```
完整的代码如下:
```HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>前端滑动验证</title>
<!-- 引入 Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- 引入 element-ui CSS -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入 element-ui JS -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<style>
.slider-container {
width: 300px;
height: 50px;
line-height: 50px;
background-color: #ddd;
position: relative;
}
.slider {
width: 50px;
height: 50px;
background-color: #007aff;
position: absolute;
top: 0;
left: 0;
transition: left 0.3s;
}
.button-container {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="slider-container">
<div class="slider" :style="{ left: sliderLeft + 'px' }"></div>
</div>
<div class="button-container">
<el-button type="primary" @click="verify">{{ isVerified ? '已验证' : '验证' }}</el-button>
<el-button type="danger" @click="reset">重置</el-button>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
sliderLeft: 0,
isVerified: false
},
methods: {
verify() {
if (this.sliderLeft >= 250) {
// 验证通过
this.isVerified = true;
} else {
// 验证失败
this.$message.error('请拖动滑块到最右侧');
}
},
reset() {
this.sliderLeft = 0;
this.isVerified = false;
}
},
mounted() {
const slider = document.querySelector('.slider');
let isDragging = false;
let startX = 0;
let currentX = 0;
slider.addEventListener('mousedown', e => {
isDragging = true;
startX = e.clientX;
});
slider.addEventListener('mousemove', e => {
if (isDragging) {
const distance = e.clientX - startX;
currentX = Math.min(Math.max(0, currentX + distance), 250);
this.sliderLeft = currentX;
startX = e.clientX;
}
});
slider.addEventListener('mouseup', e => {
isDragging = false;
if (currentX >= 250) {
this.isVerified = true;
} else {
this.sliderLeft = 0;
currentX = 0;
}
});
}
})
</script>
</body>
</html>
```
阅读全文