vue滑动解锁组件使用方法详解
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了vue滑动解锁组件的使用,供大家参考,具体内容如下
这是一个pc端的滑动解锁组件
效果图:
话不多说,直接上代码
html部分
<template>
<div class="dragVerify">
<div class="spout" ref="spout">
<div
class="slidingBlock"
ref="slidingBlock"
:style="{ left: `${place}%` }"
@mousedown="mousedown($event)"
:class="place < distance ? 'unfinished' : 'complete'"
></div>
<div class="succeedBox" :class="place >= distance ? 'succeedText' : ''" :style="{ width: `${place}%` }"></div>
</div>
</div>
</template>
js部分
export default {
name: 'dragVerify',
data() {
return {
place: 0,
sliding: {
isDown: true,
X: 0 // 初始X值
},
move: {
X: 0 // 移动X值
},
spoutW: 0,
slidingBlockW: 0,
distance: 1 // 要移动的距离
}
},
mounted() {},
methods: {
// 鼠标事件
mousedown(e) {
if (this.place < this.distance) {
this.sliding.isDown = true
// 计算百分比
this.spoutW = this.$refs.spout.offsetWidth
this.slidingBlockW = this.$refs.slidingBlock.offsetWidth
this.distance = 100 - (this.slidingBlockW / this.spoutW) * 100
}
this.sliding.X = e.x
// 添加鼠标的移动事件
document.addEventListener('mousemove', e => {
if (this.sliding.isDown) {
this.move.X = e.x
if (this.place >= this.distance) {
this.place = this.distance
} else if (this.place >= 0 && this.place < this.distance) {
this.place = ((this.move.X - this.sliding.X) / this.spoutW) * 100
}
if (this.place <= 0) {
this.place = 0
document.removeEventListener('mousemove', null, false)
return
}
}
e.preventDefault()
})
// 松开鼠标
document.onmouseup = e => {
if (this.place == this.distance) {
this.$emit('setVerify', true)
} else {
this.sliding.isDown = false
this.place = 0
this.$emit('setVerify', false)
}
}
}
}
}
css部分
.dragVerify {
border: 1px solid #e1e1e1;
height: 40px;
background: #eeeeee;
}
.spout {
line-height: 40px;
height: 40px;
position: relative;
width: 293px;
}
.spout::before {
content: '请按住滑块,拖动到最右边';
width: 233px;
top: 0;
right: 0;
bottom: 0;
color: #919593;
font-size: 16px;
text-align: center;
position: absolute;
}
.succeedText::before {
content: '验证通过';
width: 233px;
top: 0;
right: 0;
bottom: 0;
color: #ffffff;
font-size: 16px;
text-align: center;
position: absolute;
}
.succeedBox {
color: #ffffff;
font-size: 16px;
text-align: center;
line-height: 38px;
height: 38px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #23b14d;
z-index: 8;
}
.slidingBlock {
width: 60px;
height: calc(100% - 0.1rem);
border: 1px solid #e1e1e1;
border-top: none;
border-left: none;
background: #ffffff;
position: absolute;
top: 0;
bottom: 0;
left: 0;
cursor: move;
z-index: 9;
}
.slidingBlock::after {
content: '';
position: absolute;
background-size: 100% 100%;
background-repeat: no-repeat;
width: 20px;
height: 20px;
left: 50%;
top: 50%;
margin-left: -10px;
margin-top: -10px;
}
.unfinished::after {
background-image: url(你的图片);
}
.complete::after {
background-image: url(你的图片);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341