生成一段vue滑动验证码
时间: 2023-07-11 17:40:41 浏览: 160
vue/vux滑动验证码
好的,以下是一个简单的 Vue 滑动验证码组件的代码:
```html
<template>
<div class="slider-verify">
<div class="slider-verify-wrapper" ref="wrapper">
<div class="slider-verify-bg"></div>
<div class="slider-verify-btn" :style="sliderBtnStyle" ref="sliderBtn"></div>
</div>
<div class="slider-verify-tips" v-text="sliderTips"></div>
</div>
</template>
<script>
export default {
name: 'SliderVerify',
data() {
return {
isDragging: false, // 是否正在拖拽滑块
sliderBtnWidth: 42, // 滑块宽度
sliderBtnLeft: 0, // 滑块距离左侧的距离
sliderTips: '请按住滑块,拖动到最右边', // 提示文字
};
},
computed: {
sliderBtnStyle() {
return {
left: `${this.sliderBtnLeft}px`,
width: `${this.sliderBtnWidth}px`,
};
},
},
mounted() {
this.initSliderBtn();
},
methods: {
initSliderBtn() {
const wrapper = this.$refs.wrapper;
const sliderBtn = this.$refs.sliderBtn;
const wrapperWidth = wrapper.offsetWidth;
const sliderBtnWidth = sliderBtn.offsetWidth;
this.sliderBtnLeft = Math.floor(Math.random() * (wrapperWidth - sliderBtnWidth - 2)) + 1;
wrapper.addEventListener('mousedown', this.handleMouseDown);
wrapper.addEventListener('mousemove', this.handleMouseMove);
wrapper.addEventListener('mouseup', this.handleMouseUp);
},
handleMouseDown(e) {
this.isDragging = true;
this.startX = e.clientX;
},
handleMouseMove(e) {
if (this.isDragging) {
const wrapper = this.$refs.wrapper;
const sliderBtnWidth = this.sliderBtnWidth;
const wrapperWidth = wrapper.offsetWidth;
const sliderBtnLeft = this.sliderBtnLeft;
const diffX = e.clientX - this.startX;
let newLeft = sliderBtnLeft + diffX;
if (newLeft < 0) {
newLeft = 0;
} else if (newLeft > wrapperWidth - sliderBtnWidth) {
newLeft = wrapperWidth - sliderBtnWidth
}
this.sliderBtnLeft = newLeft;
this.startX = e.clientX;
}
},
handleMouseUp() {
this.isDragging = false;
const wrapper = this.$refs.wrapper;
const sliderBtnWidth = this.sliderBtnWidth;
const wrapperWidth = wrapper.offsetWidth;
const sliderBtnLeft = this.sliderBtnLeft;
if (sliderBtnLeft + sliderBtnWidth >= wrapperWidth) {
this.sliderTips = '验证通过';
this.sliderBtnLeft = wrapperWidth - sliderBtnWidth - 2;
wrapper.removeEventListener('mousedown', this.handleMouseDown);
wrapper.removeEventListener('mousemove', this.handleMouseMove);
wrapper.removeEventListener('mouseup', this.handleMouseUp);
this.$emit('success');
} else {
this.sliderTips = '请按住滑块,拖动到最右边';
}
},
},
};
</script>
<style scoped>
.slider-verify {
position: relative;
width: 300px;
height: 44px;
background-color: #f2f2f2;
border-radius: 5px;
box-sizing: border-box;
overflow: hidden;
}
.slider-verify-wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
border: 1px solid #dcdcdc;
border-radius: 5px;
background-color: #fff;
}
.slider-verify-bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #f2f2f2;
border-radius: 5px;
}
.slider-verify-btn {
position: absolute;
top: 1px;
left: 0;
height: 42px;
background-color: #fff;
border: 1px solid #dcdcdc;
border-radius: 5px;
cursor: pointer;
transition: all 0.2s ease-in-out;
}
.slider-verify-tips {
position: absolute;
top: 50%;
left: 0;
width: 100%;
text-align: center;
font-size: 14px;
color: #666;
transform: translateY(-50%);
}
</style>
```
使用方法:
```html
<template>
<div>
<slider-verify @success="handleSuccess"></slider-verify>
</div>
</template>
<script>
import SliderVerify from './SliderVerify.vue';
export default {
components: {
SliderVerify,
},
methods: {
handleSuccess() {
alert('验证通过');
},
},
};
</script>
```
阅读全文