美文网首页
vue 手势解锁组件

vue 手势解锁组件

作者: littlesunn | 来源:发表于2022-08-13 00:49 被阅读0次
image.png
<!-- <template>
  <div class='scratchable-container' 
  @touchmove="onMousemove($event)"
   @ontouchend="onMouseup"
   >
    <div class="box" v-for="(item, index) in boxs" :key="index" :ref="'box'+index">
      <div :class="['ponint', {selected: item.selected}]" 
      @touchstart.once="onMouseDown($event, index)" 
      @touchenter.once="onMouseEnter($event, index)"
      ></div>
    </div>
    <svg class="svg-wrap">
      <polyline :points="pointsStr" style="fill:none;stroke:#a8071a;stroke-width:4;stroke-linejoin:round" />
    </svg>
  </div>
</template> -->
<template>
  <div class='scratchable-container' @mousemove="onMousemove($event)" @mouseup="onMouseup">
    <div class="box" v-for="(item, index) in boxs" :key="index" :ref="'box'+index">
      <div :class="['ponint', {selected: item.selected}]" @mousedown.once="onMouseDown($event, index)" @mouseenter.once="onMouseEnter($event, index)">
        <div class="dot" :style="{
            display: item.selected ? 'inline-block' : 'none'
        }"></div>
      </div>
    </div>
    <svg class="svg-wrap">
      <polyline :points="pointsStr" style="fill:none;stroke:#a8071a;stroke-width:4;stroke-linejoin:round" />
    </svg>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      selectIndex: -1,
      points: [],
      line: null,
      contianer: null,
      selected: false,
      boxs: [],
      sortNum: []
    }
  },
  created() {
    for (let index = 0; index < 9; index++) {
      this.boxs.push(
        {
          selected: false,
        }
      )
    }
  },
  mounted() {
    this.contianer = document.querySelector(".scratchable-container");
    this.line = document.querySelector(".scratchable-container>svg>polyline");
  },
  methods: {
    onMouseDown(e, index) {
      this.sortNum.push(index)
      this.selected = true;
      this.boxs[index].selected = true;
      this.points.push(`${e.target.parentNode.offsetLeft + 50}, ${e.target.parentNode.offsetTop + 50}`)
      this.points.push("")
    },
    onMousemove(e) {
      if (!this.selected) {
        return;
      }
      this.points[this.points.length - 1] = `${e.clientX - this.contianer.getBoundingClientRect().x}, ${e.clientY - this.contianer.getBoundingClientRect().y}`
      this.line.setAttribute("points", this.points.join(" "))
    },
    onMouseEnter(e, index) {
      if (!this.selected) {
        return;
      }
      this.sortNum.push(index)
      this.boxs[index].selected = true;
      this.selectIndex = index;
      if (this.points.length > 0) {
        this.points[this.points.length - 1] = `${e.target.parentNode.offsetLeft + 50}, ${e.target.parentNode.offsetTop + 50}`
      } else {
        this.points.push(`${e.target.parentNode.offsetLeft + 50}, ${e.target.parentNode.offsetTop + 50}`)
      }
      this.points.push("")
    },
    onMouseup() {
      this.selected = false;
      this.selectIndex = -1
      this.points.pop();
      console.log("密码是: " + this.sortNum.toString());
    },
  },
  computed: {
    pointsStr() {
      console.log(123123);
      return this.points.join(" ")
    }
  },
}
</script>

<style scoped lang='scss'>
.scratchable-container {
  display: flex;
  width: 300px;
  flex-wrap: wrap;
  position: relative;
  background: #f5f5f5;
  backdrop-filter: filter(10);
  border-radius: 10px;
}
.box {
  width: 100px;
  height: 100px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  &:focus {
    .ponint {
      box-shadow: 0 0 0px 0px #ffccc7;
    }
  }
  .ponint {
    width: 50%;
    height: 50%;
    border-radius: 50%;
    transition: 0.25s;
    box-shadow: 0 0 0px 4px #ccc;
    position: relative;
    &.selected {
      background: #ff7875;
      box-shadow: 0 0 0px 10px #ffccc7;
    }
  }
  .dot {
    width: 24%;
    height: 24%;
    border-radius: 50%;
    background: #820014;
    z-index: 10;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
.svg-wrap {
  pointer-events: none;
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
</style>

相关文章

  • vue 手势解锁组件

  • iOS指纹解锁和手势解锁

    iOS指纹解锁和手势解锁 iOS指纹解锁和手势解锁

  • iOS手势解锁组件GestureLockView

    效果展示 使用方式简单 附 demo下载

  • vue-hash-calendar,移动端日期时间选择插件(使用

    按照惯例,先上效果图 vue-hash-calendar 基于 vue 2.X 开发的日历组件 支持手势滑动操作 ...

  • DrawRect绘图实现手势密码控件

    公司项目中除了之前的指纹解锁外,还有手势解锁,这就扯到了手势解锁的功能实现 其实核心就是利用touchBegin,...

  • 手势解锁

    金融产品手势解锁是常见的东西了,这里把我自己实现的记录一下。 自定义View的流程一般都是onMeasure跟on...

  • 手势解锁

    分析界面,当手指在上面移动时,当移动到一个按钮范围内当中, 它会把按钮给成为选中的状态.并且把第一个选中的按钮当做...

  • 手势解锁

    效果 实现思路 1.继承UIview 包含 所有子按钮的数组 按钮设置不可交互 有选中 和 未选中的图片2.布局...

  • 手势解锁

    首先看下我们要制作功能的效果如图所示: 思路介绍 手势密码一般为9宫格模式,通过手势滑动设置一个多边形(polyg...

  • 手势解锁

    下班回家,随便写写,写了个手势解锁,很多app都有。自己封装了一个,手势解锁视图。代码如下:头文件 源文件: 使用...

网友评论

      本文标题:vue 手势解锁组件

      本文链接:https://www.haomeiwen.com/subject/fducgrtx.html