vue实现滑块拖拽校验功能的全过程
程序员文章站
2022-08-21 23:07:05
效果图定义骨架,写html和csshtml部分
定义骨架,写html和css
<...
效果图
定义骨架,写html和css
html部分
<template> <div class="drag-wrapper" ref="dragdiv"> <div class="drag_bg"></div> <div class="drag_text f14">{{ confirmwords }}</div> <!-- 移动的模块 --> <div ref="movediv" @mousedown="mousedownfn($event)" :class="{'handler_ok_bg': confirmsuccess}" class="handler handler_bg"></div> </div> </template>
css部分: 由于担心图片源的问题,所以写成了base64的图片
<style scoped> .drag{ position: relative; background-color: #e8e8e8; width: 100%; height: 40px; line-height: 40px; text-align: center; } .handler{ width: 40px; height: 40px; border: 1px solid #ccc; cursor: move; position: absolute;top: 0px;left: 0px; } .handler_bg{ background: #fff url("data:image/png;base64,ivborw0kggoaaaansuheugaaabaaaaaqcayaaaaf8/9haaaagxrfwhrtb2z0d2fyzqbbzg9izsbjbwfnzvjlywr5ccllpaaaa3hpvfh0we1momnvbs5hzg9izs54bxaaaaaaadw/ehbhy2tldcbizwdpbj0i77u/iibpzd0ivzvnme1wq2voauh6cmvtek5uy3pryzlkij8+idx4onhtcg1ldgegeg1sbnm6ed0iywrvymu6bnm6bwv0ys8iihg6eg1wdgs9ikfkb2jlifhnucbdb3jliduuns1jmdixidc5lje1ntc3miwgmjaxnc8wms8xmy0xoto0ndowmcagicagicagij4gphjkzjpsreygeg1sbnm6cmrmpsjodhrwoi8vd3d3lnczlm9yzy8xotk5lzaylziylxjkzi1zew50yxgtbnmjij4gphjkzjpezxnjcmlwdglvbibyzgy6ywjvdxq9iiigeg1sbnm6eg1wtu09imh0dha6ly9ucy5hzg9izs5jb20vegfwlzeumc9tbs8iihhtbg5zonn0umvmpsjodhrwoi8vbnmuywrvymuuy29tl3hhcc8xljavc1r5cguvumvzb3vyy2vszwyjiib4bwxuczp4bxa9imh0dha6ly9ucy5hzg9izs5jb20vegfwlzeumc8iihhtce1nok9yawdpbmfsrg9jdw1lbnrjrd0ieg1wlmrpzdo0zdhlnwy5my05nmi0ltrlnwqtogfjyi03zty4ogyymtu2ztyiihhtce1nokrvy3vtzw50suq9inhtcc5kawq6nteyntvemurgmkvfmtffnei5ndbcmjq2m0exmdq1ouyiihhtce1nokluc3rhbmnlsuq9inhtcc5pawq6nteyntvemungmkvfmtffnei5ndbcmjq2m0exmdq1ouyiihhtcdpdcmvhdg9yvg9vbd0iqwrvymugughvdg9zag9wiendidiwmtqgke1hy2ludg9zackipia8eg1wtu06rgvyaxzlzezyb20gc3rszwy6aw5zdgfuy2vjrd0ieg1wlmlpzdo2mtc5nznmzs02otqxltqyotytytiwni02ndi2ytnkowu5ymuiihn0umvmomrvy3vtzw50suq9inhtcc5kawq6ngq4ztvmotmtotzinc00ztvklthhy2itn2u2odhmmje1nmu2ii8+idwvcmrmokrlc2nyaxb0aw9upia8l3jkzjpsrey+idwvedp4bxbtzxrhpia8p3hwywnrzxqgzw5kpsjyij8+yirg4aaaalfjrefuenpi/p//pwmlgimbqka9a+bonfsiiboxkcinh+ycacdubyoswaiopxwjciacfegbqz1avbsis5otk/8tkmnejwwgqiugtquijwaxuf3yx3xygieiflwhpkyawb+i1xgswxuligf9a7mqkbwtlhbxaflhgpgqeactkmncu6al9d8wii4hovk3itkwjaxwumloogqhmse45viq2kubuasoyc4wf+ouyxz6mqkgwaan9mirurecxgaaaabjru5erkjggg==") no-repeat center; } .handler_ok_bg{ background: #fff url("data:image/png;base64,ivborw0kggoaaaansuheugaaabaaaaaqcayaaaaf8/9haaaagxrfwhrtb2z0d2fyzqbbzg9izsbjbwfnzvjlywr5ccllpaaaa3hpvfh0we1momnvbs5hzg9izs54bxaaaaaaadw/ehbhy2tldcbizwdpbj0i77u/iibpzd0ivzvnme1wq2voauh6cmvtek5uy3pryzlkij8+idx4onhtcg1ldgegeg1sbnm6ed0iywrvymu6bnm6bwv0ys8iihg6eg1wdgs9ikfkb2jlifhnucbdb3jliduuns1jmdixidc5lje1ntc3miwgmjaxnc8wms8xmy0xoto0ndowmcagicagicagij4gphjkzjpsreygeg1sbnm6cmrmpsjodhrwoi8vd3d3lnczlm9yzy8xotk5lzaylziylxjkzi1zew50yxgtbnmjij4gphjkzjpezxnjcmlwdglvbibyzgy6ywjvdxq9iiigeg1sbnm6eg1wtu09imh0dha6ly9ucy5hzg9izs5jb20vegfwlzeumc9tbs8iihhtbg5zonn0umvmpsjodhrwoi8vbnmuywrvymuuy29tl3hhcc8xljavc1r5cguvumvzb3vyy2vszwyjiib4bwxuczp4bxa9imh0dha6ly9ucy5hzg9izs5jb20vegfwlzeumc8iihhtce1nok9yawdpbmfsrg9jdw1lbnrjrd0ieg1wlmrpzdo0zdhlnwy5my05nmi0ltrlnwqtogfjyi03zty4ogyymtu2ztyiihhtce1nokrvy3vtzw50suq9inhtcc5kawq6ndlbrdi3njvgmkq2mtffnei5ndbcmjq2m0exmdq1ouyiihhtce1nokluc3rhbmnlsuq9inhtcc5pawq6ndlbrdi3njrgmkq2mtffnei5ndbcmjq2m0exmdq1ouyiihhtcdpdcmvhdg9yvg9vbd0iqwrvymugughvdg9zag9wiendidiwmtqgke1hy2ludg9zackipia8eg1wtu06rgvyaxzlzezyb20gc3rszwy6aw5zdgfuy2vjrd0ieg1wlmlpzdphnwezmwnhmc1hymviltqxnwetytewzs04y2u5nzrln2q4yteiihn0umvmomrvy3vtzw50suq9inhtcc5kawq6ngq4ztvmotmtotzinc00ztvklthhy2itn2u2odhmmje1nmu2ii8+idwvcmrmokrlc2nyaxb0aw9upia8l3jkzjpsrey+idwvedp4bxbtzxrhpia8p3hwywnrzxqgzw5kpsjyij8+k+shwwaaaszjrefuenpi/p//pwmykd8uzw+kuodyeyglomivghg/em/pthx0efk9i8waoez+idupiimy8in1qjwenogj3aco5gnabmbahlgaxa4gq5igannj0mwavtsx7ikyy7l2unujaf+amamj78aedtbiwgyg5gbifcsxfczoabmcy4a4gojnh0d6dpk4ixnsvihafsdoaelrajrjgjp/awpbhmhejiqnwyrmuznq4vqgdqqxk0ia/0i17wjipmqntnbeagmlqiwiqa2vgww7qppbekgxsajiieusbnnsbdweay9mefgmmgbk00e0izta7ahectdq58mrua6wllggfmompig1qfegwaixgzo8guhiysmwqgsazgwhaezhicizoabkjkqym0caaqdgx279jf50aaaaaabjru5erkjggg==") no-repeat center; } .drag_bg{ background-color: #7ac23c; height: 40px; width: 0px; } .drag_text{ position: absolute; top: 0px; width: 100%;text-align: center; -moz-user-select: none; -webkit-user-select: none; user-select: none; -o-user-select:none; -ms-user-select:none; } </style>
实现滑动拖拽校验
定义参数
data() { return { beginclientx:0, // 距离屏幕左端距离 mousemovestata:false, // 触发拖动状态 判断 maxwidth:'', // 拖动最大宽度,依据滑块宽度算出来的 confirmwords:'拖动滑块验证', // 滑块文字 confirmsuccess:false // 验证成功判断 } }
1. 在mounted里面,根据滑块宽度计算可拖动最大宽度以及监听手指的触摸和离开事件
mounted() { // 根据滑块宽度计算可拖动最大宽度 this.maxwidth = this.$refs.dragdiv.clientwidth - this.$refs.movediv.clientwidth // 监听手指的触摸事件 document.getelementsbytagname('html')[0].addeventlistener('mousemove', this.mousemovefn) // 监听手指离开事件 document.getelementsbytagname('html')[0].addeventlistener('mouseup', this.moseupfn) }
2. 编写手指滑动的事件和手指离开的事件
mousemove事件
首先判断是否触发拖动状态,然后计算拖动的距离和模块距离,实时赋值
//验证成功函数 mousemovefn(e){ if(this.mousemovestata){ let width = e.clientx - this.beginclientx if(width > 0 && width <= this.maxwidth) { document.getelementsbyclassname('handler')[0].style.left = width + 'px' document.getelementsbyclassname('drag_bg')[0].style.width = width + 'px' }else if(width > this.maxwidth) this.successfunction() } },
mouseup事件
把拖动状态改成false,并且把滑块移到对应的手指落下位置上
moseupfn(e) { this.mousemovestate = !1 // 修改状态 const width = e.clientx - this.beginclientx // 计算获取宽度 if(width < this.maxwidth) { // 当宽度小于模块的宽度时,赋值 document.getelementsbyclassname('handler')[0].style.left = 0 + 'px' document.getelementsbyclassname('drag_bg')[0].style.width = 0 + 'px' } }
在上面html部分的handler块里,定义了mousedown事件(mousedownfn($event))
需要阻止文件选中等浏览器默认行为,并把触发拖动状态这个阈值打开,记录手指移动的距离
mousedownfn:function (e) { e.preventdefault && e.preventdefault() // 阻止文字选中等 浏览器默认事件 this.mousemovestata = true // 把触发拖动状态这个阈值打开 this.beginclientx = e.clientx // 记录手指移动的距离 },
至此,功能就完成了。。
完整的js代码如下
<script> export default { data(){ return { beginclientx:0, /*距离屏幕左端距离*/ mousemovestata:false, /*触发拖动状态 判断*/ maxwidth:'', /*拖动最大宽度,依据滑块宽度算出来的*/ confirmwords:'拖动滑块验证', /*滑块文字*/ confirmsuccess:false /*验证成功判断*/ } }, mounted(){ this.maxwidth = this.$refs.dragdiv.clientwidth - this.$refs.movediv.clientwidth document.getelementsbytagname('html')[0].addeventlistener('mousemove',this.mousemovefn) document.getelementsbytagname('html')[0].addeventlistener('mouseup',this.moseupfn) }, methods: { mousedownfn:function (e) { if(!this.confirmsuccess){ e.preventdefault && e.preventdefault() //阻止文字选中等 浏览器默认事件 this.mousemovestata = true this.beginclientx = e.clientx } }, //mousedoen 事件 successfunction(){ this.confirmsuccess = true this.confirmwords = '验证通过' this.$emit('onvalidation', true) if(window.addeventlistener){ document.getelementsbytagname('html')[0].removeeventlistener('mousemove',this.mousemovefn) document.getelementsbytagname('html')[0].removeeventlistener('mouseup',this.moseupfn) }else document.getelementsbytagname('html')[0].removeeventlistener('mouseup',()=>{}) document.getelementsbyclassname('drag_text')[0].style.color = '#fff' document.getelementsbyclassname('handler')[0].style.left = this.maxwidth + 'px' document.getelementsbyclassname('drag_bg')[0].style.width = this.maxwidth + 'px' }, //验证成功函数 mousemovefn(e){ if(this.mousemovestata){ let width = e.clientx - this.beginclientx if(width > 0 && width <= this.maxwidth) { document.getelementsbyclassname('handler')[0].style.left = width + 'px' document.getelementsbyclassname('drag_bg')[0].style.width = width + 'px' }else if(width > this.maxwidth) this.successfunction() } }, //mousemove事件 moseupfn(e){ this.mousemovestata = false var width = e.clientx - this.beginclientx if(width<this.maxwidth){ document.getelementsbyclassname('handler')[0].style.left = 0 + 'px' document.getelementsbyclassname('drag_bg')[0].style.width = 0 + 'px' } } } } </script>
总结
到此这篇关于vue实现滑块拖拽校验功能的文章就介绍到这了,更多相关vue滑块拖拽校验内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: vue实现简易计时器组件
下一篇: ps怎么使用画笔画翠绿的小草?