Vue implements simple slider verification

Vue implements simple slider verification

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:
  • Vue+elementui realizes dragging the slider puzzle verification
  • Vue implements mobile slider verification
  • Vue implements slider verification when logging in
  • Practice of integrating Vue with Alibaba Cloud for slider verification
  • How to use Tencent slider verification code in Vue3+Vue-cli4 project
  • Use konva and vue-konva libraries to implement drag slider verification function
  • VUE access Tencent verification code function (slider verification) memo
  • Vue implements the drag slider verification function (only css+js without background verification steps)
  • vue-cli custom directive directive add validation slider example
  • How to encapsulate the image slider verification component in Vue

<<:  Detailed explanation of how to create an updateable view in MySQL

>>:  Using Nginx to implement grayscale release

Recommend

Linux firewall status check method example

How to check the status of Linux firewall 1. Basi...

XHTML Getting Started Tutorial: XHTML Tags

Introduction to XHTML tags <br />Perhaps you...

How to run tomcat source code in maven mode

Preface Recently, I was analyzing the startup pro...

How to implement a password strength detector in react

Table of contents Preface use Component Writing D...

Node.js returns different data according to different request paths.

Table of contents 1. Learn to return different da...

Using Nginx to implement grayscale release

Grayscale release refers to a release method that...

Docker installation Nginx tutorial implementation illustration

Let’s install Nginx and try it out. Please note t...

Analysis of MySQL Aborted connection warning log

Preface: Sometimes, the session connected to MySQ...

JS Decorator Pattern and TypeScript Decorators

Table of contents Introduction to the Decorator P...

Don't forget to close the HTML tag

Building web pages that comply with Web standards ...

25 Examples of Using Circular Elements in Web Design

Today, this post lists some great examples of circ...

Tutorial on installing mysql5.7.23 on Ubuntu 18.04

This article shares with you the specific method ...