首页 > 其他分享 >【前端开发】一个滑动滑块校验登录的组件思路(用vue写的)

【前端开发】一个滑动滑块校验登录的组件思路(用vue写的)

时间:2023-03-03 15:55:24浏览次数:54  
标签:style vue 滑块 disX slide code 前端开发 important

<template>
  <el-dialog
    :visible.sync="dialogVisible"
    custom-class="slideVerifyDialog"
    :close-on-click-modal="false"
    title="身份验证"
    width="360px"
  >
    <section class="slider-section">
      <div class="img-box">
        <img :src="bigImgSrc" class="bg-img" ref="bgImg" />
        <img :src="sliderImgSrc" class="slider-img" ref="sliderImg" />
        <i class="refresh-btn el-icon-refresh" @click="init" />
      </div>
      <div class="slider-box">
        <span class="slider-text">向右滑动滑块填充拼图</span>
        <div class="slider-icon" @mousedown="rangeMove">></div>
      </div>
    </section>
  </el-dialog>
</template>
<script lang="ts">
import { Vue, Component, Prop, Ref } from 'vue-property-decorator'
import { getSlideImg, checkSlide } from '@/modules/login/api/login'

@Component({
  name: 'SlideVerifyDialog'
})
export default class SlideVerifyDialog extends Vue {
  @Prop() visible!: boolean
  @Ref() bgImg: any
  @Ref() sliderImg: any
  bigImgSrc = ''
  sliderImgSrc = ''
  disX = 0 // 滑块距离背景图左侧的位置
  slide: any = {
    bigHeight: 0,
    bigImageBase64: '',
    bigWidth: 0,
    code: '',
    posX: 0,
    posY: 0,
    smallHeight: 0,
    smallImageBase64: '',
    smallWidth: 0
  }

  get dialogVisible() {
    return this.visible
  }

  set dialogVisible(val: boolean) {
    this.$emit('update:visible', val)
  }

  mounted() {
    this.init()
  }

  getSide() {
// 这是获取滑块code的 return getSlideImg().then((res: any) => { this.slide = res.data }) } checkSlide() {
// 这里把滑块的code和滑块x轴滑动的距离传给后端 checkSlide({ code: this.slide.code, posX: this.disX }) .then(() => { this.dialogVisible = false this.$emit('submitPic', { code: this.slide.code, posX: this.disX }) }) .catch(() => { this.init() }) } async init() { await this.getSide() this.disX = 0 // 初始化滑块位置 this.bigImgSrc = 'data:image/png;base64,' + this.slide.bigImageBase64 this.sliderImgSrc = 'data:image/png;base64,' + this.slide.smallImageBase64 this.sliderImg.style.width = this.slide.smallWidth + 'px' // 设置滑块图片宽度 this.sliderImg.style.height = this.slide.smallHeight + 'px' // 设置滑块图片高度 this.sliderImg.style.top = `${Number(this.slide.posY) + 1}px` // 设置滑块距离背景图顶部的距离 this.sliderImg.style.left = 0 // 设置滑块距离背景图左侧的距离 } // 移动事件触发 rangeMove(e: any) { const ele = e.target const startX = e.clientX const eleWidth = ele.offsetWidth const parentWidth = ele.parentElement.offsetWidth const MaxX = parentWidth - eleWidth document.onmousemove = (event: any) => { const endX = event.clientX this.disX = endX - startX this.sliderImg.style.left = this.disX + 'px' if (this.disX <= 0) { this.disX = 0 this.sliderImg.style.left = 0 } if (this.disX >= MaxX - eleWidth) { // 减去滑块的宽度,体验效果更好 this.disX = MaxX this.sliderImg.style.left = parentWidth - this.sliderImg.width + 'px' } ele.style.transition = '.1s all' ele.style.transform = 'translateX(' + this.disX + 'px)' event.preventDefault() } document.onmouseup = () => { this.checkSlide() // this.$emit('submitPic', { code: this.slide.code, posX: this.disX }); setTimeout(() => { ele.style.transition = '.1s all' // 初始化滑块位置 ele.style.transform = 'translateX(0)' }, 200) document.onmousemove = null document.onmouseup = null } } } </script> <style lang="scss" scoped> .slideVerifyDialog { margin: 0 !important; position: absolute !important; top: 50% !important; left: 50% !important; transform: translate(-50%, -50%) !important; .el-dialog__body { padding: 16px !important; } } </style> <style scoped lang="scss"> .slider-section { // margin: 20px 0; .img-box { position: relative; .bg-img { width: 100%; } .refresh-btn { position: absolute; cursor: pointer; bottom: 16px; right: 16px; width: 20px; height: 20px; font-size: 20px !important; color: #fff; z-index: 20; } .slider-img { position: absolute; left: 0; top: 0; z-index: 30; } } .slider-box { margin-top: 20px; background: #f7f9fa; color: #666; border: 1px solid #e4e7eb; position: relative; height: 30px; width: 100%; &:hover { box-shadow: 0 0 3px #ccc; } .slider-text { font-size: 14px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .slider-icon { cursor: move; width: 30px; height: 30px; line-height: 30px; background: #c8923a; text-align: center; font-size: 20px !important; color: #fff; box-shadow: 0 0 6px #ccc; } } } </style>

效果

 

标签:style,vue,滑块,disX,slide,code,前端开发,important
From: https://www.cnblogs.com/xiaohuizhang/p/17175873.html

相关文章

  • vue-cli指定版本安装
    安装新的版本前,需要先把之前安装的版本卸载掉。vue卸载:npmuninstallvue-cli-g(3.0以下版本卸载)npmuninstall-g@vue/cli(3.0以上版本卸载)vue安装:npminstall-g@vue/cl......
  • 最全的uniapp脚手架开发,集成eslint,ui库,request请求,ts,支持vue2, vue3
    期望你的收获前端工程化中脚手架的整体设计和结构能够自己独立为企业定制一套前端脚手架背景开发新项目遇到的问题开发时间短要求技术栈完整辅助功能完善针......
  • vue实现app页面切换效果
    pageAninmatevue-router实现webApp切换效果快速集成1.复制PageTransittion.vue到项目目录。2.修改router配置。Router.prototype.goBack=function(){this.isB......
  • vue devTools
    下载打开Chrome浏览器–>选择更多工具–>扩展程序–>打开开发者模式,点击“加载已解压的扩展程序”,选择下载并解压出来的文件夹。如果看不到“加载已解压的扩展程序…”......
  • Django+vue 上传execl文件并解析
    Django+vue上传execl文件并解析VUE<template><el-buttontype="primary"class="but_list_but1"><inputtype="file"name="avatar"id="avatar"style="display......
  • 从0搭建Vue3组件库(三): 组件库的环境配置
    本篇文章将在项目中引入typescript,以及手动搭建一个用于测试组件库组件Vue3项目因为我们是使用Vite+Ts开发的是Vue3组件库,所以我们需要安装typescript、vue3,同......
  • npm vue-router安装报错
    因为2022年2月7日以后,vue-router的默认版本,为4版本,而且vue-router4,只能在vue3中,只有vue-router3中,能用在vue2中如果把vue-router4强制安装到vue2中,则会报上面的错误;解......
  • vue3 门户网站搭建8-字体
    浏览器默认的可选字体比较少,如果没有合适的则需要额外下载并引入。一般使用开源字体即可,商用需要花钱~ 将下载好的ttf格式字体放入项目下文件夹: 样式文件中增加......
  • vue3 门户网站搭建7-eslint
    为了方便阅读和维护,代码规范还是有必要的 1、安装:npmieslint--save-dev 2、配置 .eslintrc.cjs文件,增加rules:rules:{'semi':['warn','always'],......
  • vue3组件透传
    <template><divclass="empty-box"><el-emptydescription="暂无数据"v-bind="$attrs"><templatev-for="(item,key)in$slots"#[key]><slot......