Home  >  Article  >  Web Front-end  >  How to implement the drag verification code function using jQuery and vue

How to implement the drag verification code function using jQuery and vue

亚连
亚连Original
2018-06-08 18:10:172168browse

This article mainly shares with you the super simple green drag verification code function and code examples implemented by jQuery and vue respectively. Friends who need it can learn from it.

jquery's green drag verification function

I saw a question on the Internet: How is the verification method like dragging a slider to match graphics implemented? .

I suddenly thought of implementing a simple green drag verification code function. After searching on the Internet, I found a code for this function implemented with jquery.

Experience address: http://yanshi.sucaihuo.com/jquery/8/897/demo/

Its CSS code:

#drag{ 
  position: relative;
  background-color: #e8e8e8;
  width: 300px;
  height: 34px;
  line-height: 34px;
  text-align: center;
}
#drag .handler{
  position: absolute;
  top: 0px;
  left: 0px;
  width: 40px;
  height: 32px;
  border: 1px solid #ccc;
  cursor: move;
}
.handler_bg{
  background: #fff url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3Vy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2U5NzRlN2Q4YTEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==") no-repeat center;
}
#drag .drag_bg{
  background-color: #7ac23c;
  height: 34px;
  width: 0px;
}
#drag .drag_text{
  position: absolute;
  top: 0px;
  width: 300px;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select:none;
  -ms-user-select:none; 
}

HTML code structure:

<center style="margin-top: 100px">
  <p id="drag"></p>
</center>

JS calling method:

$(&#39;#drag&#39;).drag();

JS implementation code:

(function($){
  $.fn.drag = function(options){
    var x, drag = this, isMove = false, defaults = {
    };
    var options = $.extend(defaults, options);
    //添加背景,文字,滑块
    var html = &#39;<p class="drag_bg"></p>&#39;+
          &#39;<p class="drag_text" onselectstart="return false;" unselectable="on">拖动滑块验证</p>&#39;+
          &#39;<p class="handler handler_bg"></p>&#39;;
    this.append(html);
    
    var handler = drag.find(&#39;.handler&#39;);
    var drag_bg = drag.find(&#39;.drag_bg&#39;);
    var text = drag.find(&#39;.drag_text&#39;);
    var maxWidth = drag.width() - handler.width(); //能滑动的最大间距
    
    //鼠标按下时候的x轴的位置
    handler.mousedown(function(e){
      isMove = true;
      x = e.pageX - parseInt(handler.css(&#39;left&#39;), 10);
    });
    
    //鼠标指针在上下文移动时,移动距离大于0小于最大间距,滑块x轴位置等于鼠标移动距离
    $(document).mousemove(function(e){
      var _x = e.pageX - x;
      if(isMove){
        if(_x > 0 && _x <= maxWidth){
          handler.css({&#39;left&#39;: _x});
          drag_bg.css({&#39;width&#39;: _x});
        }else if(_x > maxWidth){ //鼠标指针移动距离达到最大时清空事件
          dragOk();
        }
      }
    }).mouseup(function(e){
      isMove = false;
      var _x = e.pageX - x;
      if(_x < maxWidth){ //鼠标松开时,如果没有达到最大距离位置,滑块就返回初始位置
        handler.css({&#39;left&#39;: 0});
        drag_bg.css({&#39;width&#39;: 0});
      }
    });
    
    //清空事件
    function dragOk(){
      handler.removeClass(&#39;handler_bg&#39;).addClass(&#39;handler_ok_bg&#39;);
      text.text(&#39;验证通过&#39;);
      drag.css({&#39;color&#39;: &#39;#fff&#39;});
      handler.unbind(&#39;mousedown&#39;);
      $(document).unbind(&#39;mousemove&#39;);
      $(document).unbind(&#39;mouseup&#39;);
    }
  };
})(jQuery);

VUE’s green drag verification function

HTML structure:

<template>
  <p id="drag">
    <p class="drag_bg weui-btn_primary" :style="{width:curW+&#39;px&#39;}"></p>
    <p class="drag_text" onselectstart="return false;" unselectable="on":class="[isDragOk ? &#39;whitecolor&#39;:&#39;&#39;]">{{text}}</p>
    <p class="handler" :style="{left:curW+&#39;px&#39;}" :class="[isDragOk ? &#39;handler_ok_bg&#39;:&#39;handler_bg&#39;]"></p>
  </p>
</template>

CSS code:

<style scoped>
#drag {
  position: relative;
  background-color: #e8e8e8;
  width: 100%;
  height: 36px;
  line-height: 36px;
  text-align: center;
}
#drag .drag_bg {
  height: 36px;
  width:0;
}
#drag .drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
.drag_text.whitecolor{
  color:#fff;
}
#drag .handler {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 40px;
  height: 34px;
  border: 1px solid #ccc;
  cursor: move;
}
.handler_bg {
  background:#fff url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo0ZDhlNWY5My05NmI0LTRlNWQtOGFjYi03ZTY4OGYyMTU2ZTYiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NTEyNTVEMURGMkVFMTFFNEI5NDBCMjQ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EYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==") no-repeat center
}
</style>

JS code:

<script>
export default{
  name:&#39;slider&#39;,
  props:{
    
  },
  data(){
    return{
      curW:0,
      isMove:false, //是否在运动
      isDragOk:false, //是否拖动成功
      maxWidth:0, //拖动的最大宽度
      element:{},
      text:&#39;拖动滑块验证&#39;,
      currentPos:{
        x: 0,
        y: 0
      }
    }
  },
  created(){
    
  },
  mounted () {
    var self = this;
    this.element = document.querySelector(&#39;.handler&#39;);
    this.getMaxWidth();
    window.addEventListener(&#39;resize&#39;,function(){self.getMaxWidth()});
    window.addEventListener(&#39;orientationchange&#39;,function(){self.getMaxWidth()});
    
    
     this.element.addEventListener(&#39;touchstart&#39;,self.touchstartFun,false);
    document.querySelector(&#39;body&#39;).addEventListener(&#39;touchmove&#39;,self.touchmoveFun,false);
    document.querySelector(&#39;body&#39;).addEventListener(&#39;touchend&#39;,self.touchendFun,false);
    
    this.element.addEventListener(&#39;mousedown&#39;,self.touchstartFun,false);
    document.querySelector(&#39;body&#39;).addEventListener(&#39;mousemove&#39;,self.touchmoveFun,false);
    document.querySelector(&#39;body&#39;).addEventListener(&#39;mouseup&#39;,self.touchendFun,false);
    
    (function drawAnimate() {
      if( self.curW <= self.maxWidth){
        window.requestAnimFrame(drawAnimate,1000/60);
        self.curW = self.currentPos.x;
      }else{
        self.curW = self.currentPos.x = self.maxWidth;
      }
    })();
  },
  watch:{
    
  },
  methods:{
    touchstartFun(e){
      if(this.isDragOk){
         e.preventDefault();
         return;
       }
      this.isMove = true;
      this.curW = this.currentPos.x = this.getCurrentPosition(e).x;
    },
    touchmoveFun(e){
      if(this.isMove && this.curW > 0 && this.curW < this.maxWidth){
        this.currentPos.x = this.getCurrentPosition(e).x;
      }
      else if(this.isMove && this.curW >= this.maxWidth){
        this.curW = this.currentPos.x = this.maxWidth;
        this.isDragOk = true;
        this.text = "验证通过";
      }
    },
    touchendFun(e){
      this.isMove = false;
      if(this.curW < this.maxWidth){
        this.curW = this.currentPos.x = 0;
      }
    },
    
    getCurrentPosition(event){
      var xPos, yPos, rect;
      rect = document.getElementById(&#39;drag&#39;).getBoundingClientRect();
      //event = event.originalEvent;
      //判断是touch,还是鼠标事件
      if (event.type.indexOf(&#39;touch&#39;) !== -1) {
        xPos = event.touches[0].clientX - rect.left;
        yPos = event.touches[0].clientY - rect.top;
      }
      
      //鼠标事件
      else {
        xPos = event.clientX - rect.left;
        yPos = event.clientY - rect.top;
      }
      return {
        x: xPos,
        y: yPos
      }
    },
    getMaxWidth(){
      this.maxWidth = document.querySelector("#drag").clientWidth - document.querySelector(".handler").scrollWidth;
    }
  }
}
</script>

Page reference method:

<slider></slider>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to configure the babel configuration file in vue-cli

How to use babel installation and configuration tutorial

Use gm cropping to synthesize pictures under Nodejs

The above is the detailed content of How to implement the drag verification code function using jQuery and vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn