vue-cli 自定义指令directive 添加验证滑块示例


Posted in Javascript onOctober 19, 2017

vue项目注册登录页面遇到了一个需要滑块的功能,网上看了很多插件发现都不太好用,于是自己写了一个插件供大家参考;

用的是vue的自定义指令direcive,只需要在需要的组件里放入对应的标签嵌套即可;

template:

<template>
<div>
<div class="movebox" >
        <div class="movego"></div>
        <div class="txt" id="txt">拖动滑块验证</div>
        <div class="move moveBefore" v-move></div>
 </div>
<div class="movebox" >
        <div class="movego"></div>
        <div class="txt" id="txt">拖动滑块验证</div>
        <div class="move moveBefore" v-move></div>
 </div>
</div>
</template>

项目中使用的是scss;

css代码:

<style lang="scss" scoped>


.movebox{
  position: relative;
  background-color: #e8e8e8;
  width: 300px;
  height: 34px;
  line-height: 34px;
  text-align: center;
  .txt{
  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;
  }
  .movego{
   background-color: #7ac23c;
   height: 34px;
   width: 0px;
  }
   .move{
    position: absolute;
  top: 0px;
  left: 0px;
  width: 40px;
  height: 34px;
  border: 1px solid #ccc;
  cursor: move;
   }
   .moveBefore{
   background: #fff url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1z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NDBCMjQ2M0ExMDQ1OUYiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTQgKE1hY2ludG9zaCkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDphNWEzMWNhMC1hYmViLTQxNWEtYTEwZS04Y2U5NzRlN2Q4YTEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==") no-repeat center;

    
   }
  }
</style>

jquery已经在全局引入,需要jquery全局引入的,可以看我的上一篇博客,里面有jquery的引入;

script:

<script>


export default {
 components: {
  
 },
 data() {
  return {    
  }
 },
 mounted() {
  //console.log($('选择器'))
 },
 methods: {
    },
 directives: {
  move(el) {
    el.onmousedown = function(e) {
    var X = e.clientX - el.offsetLeft
    document.onmousemove = function(e) {
      var endx = e.clientX - X
    el.className = 'move moveBefore'
    el.style.left = endx + 'px'
    // console.log(el.parentNode.children[0])
    var width = $('.movebox').width() - $('.move').width()
     el.parentNode.children[0].style.width = endx + 'px'
     el.parentNode.children[1].innerHTML = '拖动滑块验证'
    //临界值小于
    if (endx <= 0) {
    el.style.left = 0 + 'px'
     el.parentNode.children[0].style.width = 0 + 'px'
     //$('.movego').width(0)
    }
    //临界值大于
    // console.log(el.style.left)
    if (parseInt(el.style.left) >= width) {
    el.style.left = width + 'px'
     el.parentNode.children[0].style.width = width + 'px'
     el.parentNode.children[1].innerHTML = '验证通过'
    el.className = 'move moveSuccess'
    el.onmousedown = null
    }
    }
  }
  document.onmouseup = function() {
    document.onmousemove = null
  }
  }
 }
}

</script>

 谢了近20分钟,调试好了,效果还是比较可以的:

用的gif制作软件略卡;

vue-cli 自定义指令directive 添加验证滑块示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
AJAX 网页保留浏览器前进后退等功能
Feb 12 Javascript
JavaScript高级程序设计 读书笔记之八 Function类及闭包
Feb 27 Javascript
多种方法实现load加载完成后把图片一次性显示出来
Feb 19 Javascript
jquery单行文字向上滚动效果示例
Mar 06 Javascript
javascript限制用户只能输汉字中文的方法
Nov 20 Javascript
JavaScript事件委托用法分析
Jan 24 Javascript
JavaScript 性能优化小结
Oct 12 Javascript
AngularJS ng-bind-html 指令详解及实例代码
Jul 30 Javascript
react开发中如何使用require.ensure加载es6风格的组件
May 09 Javascript
jquery DataTable实现前后台动态分页
Jun 17 jQuery
利用nvm管理多个版本的node.js与npm详解
Nov 02 Javascript
JavaScript中AOP的实现与应用
May 06 Javascript
Angular2仿照微信UI实现9张图片上传和预览的示例代码
Oct 19 #Javascript
jQuery实现打开网页自动弹出遮罩层或点击弹出遮罩层功能示例
Oct 19 #jQuery
jQuery实现的粘性滚动导航栏效果实例【附源码下载】
Oct 19 #jQuery
浅谈Node Inspector 代理实现
Oct 19 #Javascript
AngularJS表单验证功能
Oct 19 #Javascript
JS实现的按钮点击颜色切换功能示例
Oct 19 #Javascript
Vue如何从1.0迁移到2.0
Oct 19 #Javascript
You might like
建立动态的WML站点(二)
2006/10/09 PHP
php split汉字
2009/06/05 PHP
PHP面向对象概念
2011/11/06 PHP
php实现cookie加密的方法
2015/03/10 PHP
PHP递归调用数组值并用其执行指定函数的方法
2015/04/01 PHP
由Javascript实现的页面日历
2011/11/04 Javascript
jquery ztree实现下拉树形框使用到了json数据
2014/05/14 Javascript
基本DOM节点操作
2017/01/17 Javascript
jquery使用EasyUI Tree异步加载JSON数据(生成树)
2017/02/11 Javascript
js 动态生成html 触发事件传参字符转义的实例
2017/02/14 Javascript
js实现无缝滚动图
2017/02/22 Javascript
nodejs读写json文件的简单方法(必看)
2017/03/09 NodeJs
javascript图片预览和上传(兼容IE)
2017/03/15 Javascript
基于Bootstrap模态对话框只加载一次 remote 数据的解决方法
2017/07/09 Javascript
Angular2关于@angular/cli默认端口号配置的问题
2017/07/15 Javascript
AngularJS 前台分页实现的示例代码
2018/06/07 Javascript
vue实现动态按钮功能
2019/05/13 Javascript
微信小程序 SOTER 生物认证DEMO 指纹识别功能
2019/12/13 Javascript
JavaScript实现单点登录的示例
2020/09/23 Javascript
详解Typescript里的This的使用方法
2021/01/08 Javascript
linux系统使用python获取内存使用信息脚本分享
2014/01/15 Python
利用Python学习RabbitMQ消息队列
2015/11/30 Python
python使用opencv按一定间隔截取视频帧
2018/03/06 Python
对python中的iter()函数与next()函数详解
2018/10/18 Python
Python 函数返回值的示例代码
2019/03/11 Python
Python 实现数据结构-循环队列的操作方法
2019/07/17 Python
python调用支付宝支付接口流程
2019/08/15 Python
Python模块的制作方法实例分析
2019/12/21 Python
css3使网页、图片变成灰色兼容大多数浏览器
2014/07/02 HTML / CSS
CSS 3.0 结合video视频实现的创意开幕效果
2020/06/01 HTML / CSS
html5组织文档结构_动力节点Java学院整理
2017/07/11 HTML / CSS
iphoneX 适配客户端H5页面的方法教程
2017/12/08 HTML / CSS
商务英语大学生职业生涯规划书范文
2014/01/01 职场文书
运动会广播稿20字
2014/02/18 职场文书
村委会贫困证明范本
2014/09/17 职场文书
节约用电通知
2015/04/25 职场文书