Vue实现滑动登录(Java Web项目)

Vue实现滑动登录(Java Web项目)

1.Vue中Template部分

代码如下:

<div class="login-btn">
   <div class="jc-component__range">
      <div class="jc-range" :class="rangeStatus ? 'success' :'' ">
      <i @mousedown="rangeMove" :class="rangeStatus?successIcon:startIcon"></i>
      <span style="color:white;">{{rangeStatus?successText:startText}}</span>
      </div>
   </div>
 </div>

2.Vue中Script部分

代码如下:

props: {
      // 成功之后的函数
      successFun: {
        type: Function
      },
      //成功图标
      successIcon: {
        type: String,
        default: 'el-icon-success'
      },
      //成功文字
      successText: {
        type: String,
        default: '验证成功'
      },
      //开始的图标
      startIcon: {
        type: String,
        default: 'el-icon-d-arrow-right'
      },
      //开始的文字
      startText: {
        type: String,
        default: '登录'
      },
      //失败之后的函数
      errorFun: {
        type: Function
      },
      //或者用值来进行监听
      status: {
        type: String
      }
    },
data() {
      return {
        rangeStatus: ''
      };
    }
	  // 滑动登录函数
      rangeMove(e) {
        let ele = e.target;
        let startX = e.clientX;
        let eleWidth = ele.offsetWidth;
        let parentWidth = ele.parentElement.offsetWidth;
        let MaxX = parentWidth - eleWidth;
        if (this.rangeStatus) {//不运行
          return false;
        }
        document.onmousemove = (e) => {
          let endX = e.clientX;
          this.disX = endX - startX;
          if (this.disX <= 0) {
            this.disX = 0;
          }
          if (this.disX >= MaxX - eleWidth) {//减去滑块的宽度,体验效果更好
            this.disX = MaxX;
          }
          ele.style.transition = '.1s all';
          ele.style.transform = 'translateX(' + this.disX + 'px)';
          e.preventDefault();
        };
        document.onmouseup = () => {
          if (this.disX !== MaxX) {
            ele.style.transition = '.5s all';
            ele.style.transform = 'translateX(0)';
            //执行失败的函数
            this.errorFun && this.errorFun();
          } else {
            if (this.form.name.trim() != '' && this.form.password.trim() != '') {
              // 编码加密
              let base64 = require('js-base64').Base64;

              let params = new URLSearchParams();
              params.append("userName",this.form.name);
              params.append("password",base64.encode(this.form.password));

              let that = this;

              this.$axios({
                url: '/api/login/login',
                method:'post',
                data: params
              }).then(data => {
                if(data.data.success){
                  that.rangeStatus = true;
                  if (that.status) {
                    that.$parent[that.status] = true;
                  }
                  //执行成功的函数
                  this.successFun && this.successFun();
                  sessionStorage.setItem('user', JSON.stringify(data.data.data));
                  sessionStorage.setItem('SESSION_NAME', data.data.data.realName);
                  this.$message({type: 'success', message: '登录成功'});

                  // 跳转到首页
                  this.$router.push('/homepage');
                }else if(data.data.code == '010'){
                  that.disable.on = true;
                }else if(data.data.code == '003'){
                  ele.style.transition = '.5s all';
                  ele.style.transform = 'translateX(0)';
                  //执行失败的函数
                  this.errorFun && this.errorFun();

                  this.$message({type: 'warning', message: '用户名和密码错误'});
                }
              }).catch(error => {
                this.$message({type:'error',message:'网络连接异常'});
              })
            } else {
              ele.style.transition = '.5s all';
              ele.style.transform = 'translateX(0)';
              //执行失败的函数
              this.errorFun && this.errorFun();

              this.$message({type: 'warning', message: '请输入用户名和密码'});
            }
          }
          document.onmousemove = null;
          document.onmouseup = null;
        };
      },

3.Vue中Style部分

代码如下:

.login-btn {
    position: relative;
    text-align: center;
    top: 20px
}
.jc-range {
    border-radius: 20px;
    background-color: #5E9386;
    position: relative;
    transition: 1s all;
    user-select: none;
    color: #333;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 45px; /*no*/
}

.jc-range i {
    position: absolute;
    left: 0;
    width: 60px; /*no*/
    height: 100%;
    color: #919191;
    background-color: #fff;
    border: 1px solid #bbb;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
}
.jc-range.success {
    background-color: #7AC23C;
    color: #fff;
}
.jc-range.success i {
    color: #7AC23C;
}

总结

每天一个提升小技巧!!!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小辰哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值