vue当中实现悬浮球可拖拽

touch.vue

<template>
  <transition>
    <div
      ref="breathing_lamp"
      class="breathing_lamp"
      @click="onclick()"
      @touchstart.stop="handleTouchStart"
      @touchmove.prevent.stop="handleTouchMove($event)"
      @touchend.stop="handleTouchEnd"
      :style="{
   
        left: left + 'px',
        top: top + 'px',
        width: itemWidth + 'px',
        height: itemHeight + 'px',
      }"
      v-text="text"
      v-if="isShow"
    >
      {
   {
    text }}
    </div>
     
  </transition>
</template>
<script>
export default {
   
  props: {
   
    // 球名字默认:“球”
    text: {
   
      type: String,
      default: "ball",
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现靠边悬浮的代码,可以通过以下步骤来实现: 1. 在Vue组件中,定义一个data属性来存储悬浮的位置和状态信息,如下所示: ```js data() { return { ball: { left: 0, // 悬浮左侧距离屏幕左侧的距离 top: 0, // 悬浮顶部距离屏幕顶部的距离 isFixed: false, // 悬浮是否固定在边缘 isLeft: true // 悬浮是否在左侧边缘 } } } ``` 2. 在组件的`mounted`钩子函数中,监听窗口的滚动事件,并根据窗口滚动的距离和悬浮的位置信息,来判断悬浮是否需要固定在边缘,以及固定在哪个边缘,如下所示: ```js mounted() { window.addEventListener('scroll', () => { const ball = this.ball const scrollTop = document.documentElement.scrollTop || document.body.scrollTop if (scrollTop > ball.top && !ball.isFixed) { ball.isFixed = true ball.top = 0 if (ball.left < 100) { ball.isLeft = true ball.left = 0 } else { ball.isLeft = false ball.left = document.documentElement.clientWidth - 100 } } else if (scrollTop <= ball.top && ball.isFixed) { ball.isFixed = false ball.left = ball.isLeft ? 0 : document.documentElement.clientWidth - 100 } }) } ``` 3. 在组件的模板中,根据悬浮的位置和状态信息,来动态设置悬浮的样式和位置,如下所示: ```html <template> <div> <div class="ball" :class="{ fixed: ball.isFixed, left: ball.isLeft }" :style="{ left: ball.left + 'px', top: ball.top + 'px' }"></div> </div> </template> <style scoped> .ball { position: absolute; width: 100px; height: 100px; border-radius: 50%; background-color: #f00; cursor: pointer; transition: all 0.3s ease; } .ball.fixed { position: fixed; top: 0; } .ball.left { left: 0; } .ball:not(.left) { right: 0; } </style> ``` 通过以上步骤,就可以实现一个Vue组件中的靠边悬浮了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值