滑块验证

This commit is contained in:
15906671117 2022-04-14 10:04:14 +08:00
parent d0d8d3d240
commit 3e326b2aea

View File

@ -0,0 +1,167 @@
<template>
<div id="sliderVerify" class="slider-item">
<div class="slider-bg layui-bg-green"></div>
<div class="slider-text">{{ prototype.isOk == false ? '请拖动滑块验证' : '验证通过' }}</div>
<div :class="prototype.isOk == false ? 'slider-btn layui-icon layui-icon-next' : 'slider-btn layui-icon layui-icon-ok-circle slider-btn-success'"
@mousedown.stop="down"></div>
</div>
</template>
<script setup lang='ts'>
import { reactive, ref, defineProps } from 'vue'
import { on, off } from "evtd";
import { throttle, handle_select } from "./utils/index";
interface Prop {
disabled?: boolean;
bg?: string,
text?: string,
end?: string,
success?: Function
}
const props = withDefaults(defineProps<Prop>(), {
disabled: false,
});
//
const prototype = reactive({
isOk: false,
bg: props.bg, //
text: props.text,
success: props.end,
ok: props.success
});
//
/* prototype.isMobile = function () {
return (
device.os == "ios" ||
device.os == "android" ||
device.android ||
device.ios ||
(device.weixin && typeof device.weixin === Boolean)
);
}; */
const moveAction = throttle(move);
let distance
let downX
//
const down = function (e) {
e = e || window.event
console.log(e.path)
distance = e.path[1].offsetWidth - e.path[0].offsetWidth
//
downX = e.clientX;
on("selectstart", window, handle_select, { once: true });
on("mouseup", window, stop);
on("mousemove", window, moveAction);
};
//
function move(e) {
e = e || window.event
let moveX = e.clientX;
// -
let offsetX = moveX - downX;
if (offsetX > 0) {
e.path[0].style.left = offsetX + "px";
e.path[1].childNodes[0].style.width = offsetX + "px";
// >=
if (offsetX >= distance) {
prototype.isOk = true
//
off("mousedown", window, down);
off("mousemove", window, moveAction);
//
if (prototype.ok) prototype.ok()
}
}
};
//
const stop = function (e) {
//
if (prototype.isOk === true) {
return;
} else {
e.path[0].style.left = 0;
e.path[1].childNodes[0].style.width = 0;
}
//
off("selectstart", document, handle_select);
off("mouseup", window, stop);
off("mousemove", window, moveAction);
};
</script>
<style scoped >
.slider-item {
height: 38px;
line-height: 38px;
background-color: #d0d0d0;
position: relative;
border: 1px solid white;
}
.slider-bg {
position: absolute;
width: 40px;
height: 100%;
z-index: 100
}
.slider-btn {
width: 40px;
height: 96%;
position: absolute;
border: 1px solid #ccc;
cursor: move;
text-align: center;
background-color: #fff;
user-select: none;
color: #666;
z-index: 120;
}
.slider-btn:hover {
cursor: pointer
}
.slider-btn-success {
font-size: 22px
}
.slider-text {
position: absolute;
text-align: center;
width: 100%;
height: 100%;
user-select: none;
font-size: 14px;
color: #fff;
z-index: 120
}
.slider-error {
animation: glow 800ms ease-out infinite alternate;
}
@keyframes glow {
0% {
border-color: #e6e6e6
}
100% {
border-color: #ff5722
}
}
</style>