This article example shares the implementation of Vue slider validation. The code is as follows <template> <div class="drag" ref="dragDiv"> <div class="drag_bg"></div> <div class="drag_text">{{confirmWords}}</div> <div ref="moveDiv" @mousedown="mousedownFn($event)" :class="{'handler_ok_bg':confirmSuccess}" class="handler handler_bg" style="position: absolute;top: 0px;left: 0px;"></div> </div> </template> <script> export default { data () { return { beginClientX: 0, /* Distance from the left end of the screen*/ mouseMoveStata: false, /* Trigger drag status judgment*/ maxwidth: '', /* Maximum drag width, calculated based on the slider width*/ confirmWords: 'Drag the slider to verify', /* Slider text*/ confirmSuccess: false /* Verification success judgment*/ } }, methods: { mousedownFn: function (e) { if (!this.confirmSuccess) { e.preventDefault && e.preventDefault() // Prevent browser default events such as text selection this.mouseMoveStata = true this.beginClientX = e.clientX } }, // mousedoen event successFunction () { this.confirmSuccess = true this.confirmWords = 'Verification passed' 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' }, // Verify success function 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 event 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' } } // mouseup event}, 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) } } </script> <style scoped> .drag { position: relative; background-color: #e8e8e8; width: 30%; height: 34px; line-height: 34px; text-align: center; } .handler { width: 40px; height: 32px; border: 1px solid #ccc; cursor: move; } .handler_bg { background: #fff url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8++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++IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==') no-repeat center; } .drag_bg { background-color: #7ac23c; height: 34px; 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> The effect diagram is as follows The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of how to create an updateable view in MySQL
>>: Using Nginx to implement grayscale release
1. Overview of file permissions and ownership 1. ...
Added anti-crawler policy file: vim /usr/www/serv...
I believe that many people who have used MySQL fo...
What Beautiful HTML Code Looks Like How to write ...
In this article, we will use the libudev library ...
Samba Services: This content is for reference of ...
Recently, I'm learning to use React with Thre...
Three Paradigms 1NF: Fields are inseparable; 2NF:...
HTML stands for Hypertext Markup Language. Nowada...
Table of contents cause reason Introduction to NP...
Table of contents Overview Blob Blob in Action Bl...
This article shares the specific code of JavaScri...
Table of contents Import on demand: Global Import...
Table of contents Concept Introduction Logical ru...
This article shares the specific code of videojs+...