• vue实现移动端悬浮可拖拽按钮


    需求:

    1. 按钮在页面侧边悬浮显示;
    2. 点击按钮可展开多个快捷方式按钮,从下向上展开。
    3. 长按按钮,则允许拖拽来改变按钮位置,按钮为非展开状态;
    4. 按钮移动结束,手指松开,计算距离左右两侧距离并自动移动至侧边显示;
    5. 移动至侧边后,根据具体左右两次位置判断改变展开样式;
    6. 处理移动到非可视区域时特殊情况。

    效果展示:
    在这里插入图片描述

    具体实现:

    <template>
      <div class="shortcut" @touchstart="touchstart($event)" @touchmove="touchMove($event)" @touchend="touchEnd($event)" v-if="isMobile">
        <div class="shortcut__container">
          <transition name="fade">
            <div class="shadow" v-if="showPopover" @click.stop="showPopover = false"></div>
          </transition>
          <transition name="sub-fade">
            <div :class="['shortcut__list', `${type}`]" v-if="showPopover">
              <div class="shortcut__list_item">
                <div class="icon-box"><img src="@/images/common/ic_question.png" alt=""></div>
                投资理财
              </div>
              <div class="shortcut__list_item">
                <div class="icon-box"><img src="@/images/common/ic_question.png" alt=""></div>
                我的资产
              </div>
              <div class="shortcut__list_item">
                <div class="icon-box"><img src="@/images/common/ic_question.png" alt=""></div>
                咨询我们
              </div>
            </div>
          </transition>
          <div class="shortcut__btn" :class="{ anim: showPopover }" @click.stop="handleBtn()">+</div>
        </div>
      </div>
    </template>
    
    <script>
    const TIME = 50
    export default {
      data () {
        return {
          isMobile: /Mobi|Android|iPhone/i.test(navigator.userAgent),
          showPopover: false,
          timeOutEvent: 0,
          longClick: 0,
          // 手指原始位置
          oldMousePos: {},
          // 元素原始位置
          oldNodePos: {},
          type: 'right',
        }
      },
      methods: {
        touchstart (ev) {
          // 定时器控制长按时间,超过{TIME}毫秒开始进行拖拽
          this.timeOutEvent = setTimeout(() => {
            this.longClick = 1
          }, TIME)
          const selectDom = ev.currentTarget
          const { pageX, pageY } = ev.touches[0] // 手指位置
          const { offsetLeft, offsetTop } = selectDom // 元素位置
          // 手指原始位置
          this.oldMousePos = {
            x: pageX,
            y: pageY,
          }
          // 元素原始位置
          this.oldNodePos = {
            x: offsetLeft,
            y: offsetTop,
          }
          this.handleMoving()
          selectDom.style.left = `${offsetLeft}px`
          selectDom.style.top = `${offsetTop}px`
        },
        touchMove (ev) {
          // 未达到{TIME}毫秒就移动则不触发长按,清空定时器
          clearTimeout(this.timeOutEvent)
          if (this.longClick === 1) {
            this.handleMoving()
            this.showPopover = false
    
            const selectDom = ev.currentTarget
            // x轴偏移量
            const lefts = this.oldMousePos.x - this.oldNodePos.x
            // y轴偏移量
            const tops = this.oldMousePos.y - this.oldNodePos.y
            const { pageX, pageY } = ev.touches[0] // 手指位置
            selectDom.style.left = `${pageX - lefts}px`
            selectDom.style.top = `${pageY - tops}px`
          }
        },
        touchEnd (ev) {
          // 清空定时器
          clearTimeout(this.timeOutEvent)
          if (this.longClick === 1) {
            this.longClick = 0
            const selectDom = ev.currentTarget
            const { innerWidth, innerHeight } = window
            const { offsetLeft, offsetTop } = selectDom
            selectDom.style.left = offsetLeft + 50 > innerWidth / 2 ? 'calc(100% - 55px)' : '15px'
            if (offsetTop < 150) {
              selectDom.style.top = '150px'
            } else if (offsetTop + 150 > innerHeight) {
              selectDom.style.top = `${innerHeight - 150}px`
            }
            this.type = offsetLeft + 50 > innerWidth / 2 ? 'right' : 'left'
    
            setTimeout(() => {
              document.body.style.overflow = 'auto'
              document.body.style.userSelect = 'auto'
            }, 1000)
          }
        },
        handleMoving () {
          // 禁止body滚动
          document.body.style.overflow = 'hidden'
          // 禁止body文本选中
          document.body.style.userSelect = 'none'
        },
        handleBtn () {
          this.showPopover = !this.showPopover
        }
      },
    }
    </script>
    
    <style scoped lang="less">
    .icon-box {
      background: #fff;
      width: .8rem;
      height: .8rem;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .shortcut {
      position: fixed;
      z-index: 9999;
      left: calc(100% - 55px);
      top: calc(100% - 150px);
      user-select: none;
    
      &__container {
        position: relative;
      }
    
      &__list {
        position: absolute;
        bottom: .8rem;
        z-index: 8;
        &_item {
          color: #fff;
          display: flex;
          flex-direction: row;
          align-items: center;
          white-space: nowrap;
          margin-bottom: .15rem;
    
          .icon-box {
            margin: 0 .1rem 0 0;
    
            img {
              width: 0.36rem;
              height: 0.36rem;
            }
          }
        }
    
        &.left {
          left: 0;
        }
    
        &.right {
          right: 0;
          .shortcut__list_item {
            flex-direction: row-reverse;
            .icon-box {
              margin: 0 0 0 .1rem;
            }
          }
        }
      }
    
      &__btn {
        background: #fff;
        width: .8rem;
        height: .8rem;
        border-radius: 50%;
        text-align: center;
        line-height: .7rem;
        color: #3356D9;
        font-size: .5rem;
        position: relative;
        z-index: 8;
        border: 1px solid #3356D9;
        transition: all .3s linear;
    
        &.anim {
          transform: rotate(135deg);
        }
      }
    }
    .shadow {
      width: 100%;
      max-width: 1024px;
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 1;
      margin: 0 auto;
    }
    .sub-fade-leave-active,.sub-fade-enter-active {
      transition: max-height 0.3s linear;
    }
    .sub-fade-enter,.sub-fade-leave-to {
      max-height: 0;
      overflow: hidden;
    }
    .sub-fade-enter-to,.sub-fade-leave {
      max-height: 2.56rem;
      overflow: hidden;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
  • 相关阅读:
    永州出入境检验实验室建设那些事
    商品管理模块微服务demo
    103.(cesium之家)cesium蜂巢图(正方形)
    camx 主要接口
    国际结算业务
    JWT安全
    集合和集合框架
    HbuilderX表格加粗没有效果怎么回事
    你想知道的ArrayList知识都在这
    excel表格,下拉选项如何修改?
  • 原文地址:https://blog.csdn.net/qq_36012563/article/details/133307522