vue项目手写微信样式拖动返回首页按钮

微信样式拖动框:

<template>
  <div :class="['drag-button', state === 1?'border-right trans':state === 2?'border-left trans':'']" @click.stop.prevent="click" @touchstart="touchstart($event)" @touchmove.stop.prevent="touchmove($event)" @touchend="touchend($event)" ref="self">
    <div class="drag-content">
      <img src="@/assets/img/icon/icon-drag.png">
    </div>
  </div>
</template>

<script>
export default {
  name: 'BaseDragBtn',
  props: {
    sticky: {
      type: Boolean,
      default: !0
    }
  },
  data () {
    return {
      state: 2
    }
  },
  mounted () {
    this.self = this.$refs['self']
    this.windowWidth = document.body.clientWidth
    this.windowHeight = document.body.clientHeight
    this.halfSelfWidth = this.self.offsetWidth / 2
    this.halfSelfHeight = this.self.offsetHeight / 2
    this.self.style.left = this.windowWidth - this.halfSelfWidth * 2 + 'px'
    this.self.style.top = (this.windowHeight - 140) + 'px'
    console.log(this.self.offsetWidth)
  },
  methods: {
    click () {
      if (this.$route.path !== '/') {
        this.$router.push({ path: '/' })
      }
    },
    touchstart () {
    },
    touchmove (e) {
      if (this.state !== 0) {
        this.state = 0
      }
      const targetX = e.targetTouches[0].clientX
      const targetY = e.targetTouches[0].clientY
      if (this.halfSelfWidth < targetX && targetX < this.windowWidth - this.halfSelfWidth) {
        this.self.style.left = (targetX - this.halfSelfWidth) + 'px'
      }
      if (this.halfSelfHeight < targetY && targetY < this.windowHeight - this.halfSelfHeight) {
        this.self.style.top = (targetY - this.halfSelfHeight) + 'px'
      }
    },
    touchend () {
      if (this.sticky) {
        if (parseInt(this.self.style.left) + this.halfSelfWidth <= this.windowWidth / 2) {
          this.self.style.left = '0px'
          this.state = 1
        } else {
          this.self.style.left = this.windowWidth - this.halfSelfWidth * 2 + 'px'
          this.state = 2
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .drag-button {
    width: 58px;
    height: 58px;
    position: fixed;
    z-index: 100;
    font-size: 12px;
    color: #ffffff;
    background-color: rgba(255, 255, 255, .85);
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #d3d3d3;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0px 1px 10px #c8b4b4;
    .drag-content {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        display: inline-block;
        width: 38px;
        height: 38px;
        border-radius: 50%;
        user-select: none;
        pointer-events: none;
      }
    }
    .drag-content:active {
      background-color: rgba(0, 0, 0, .2)
    }
  }
  .border-right {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    border-top-right-radius: 30px;
    border-bottom-right-radius: 30px;
    border-left: none;
  }
  .border-left {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
    border-top-left-radius: 30px;
    border-bottom-left-radius: 30px;
    border-right: none;
  }
  .trans {
    transition: left .3s;
  }
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值