react 实现浮动可吸附悬浮窗,悬浮球,悬浮按钮,支持拖动拖拽功能(suspend-button)

25 篇文章 0 订阅

前言:

最近在做移动端,有个需求是 实现一个浮动球可拖拽,能吸附(吸附到 左右两则,距离哪进就吸附到哪边)。
效果图

实现过程:

1. 使用 suspend-button (但是此组件不支持 ts 和pc端)

npm install suspend-button -S
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import SuspendButton from 'suspend-button'

class App extends Component {
  render() {
    return (
      <SuspendButton></SuspendButton>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('container')
)
属性名类型说明
imgString图片地址
styleobj样式

suspend-button npm地址
这个pc端需要 开启审查,切换到移动端才能看到效果
demo 地址
2. 使用 suspend-button-luckytree (这个支持了ts和 pc、移动都能用)
使用方式和 属性同上

npm i suspend-button-luckytree -S

在这里插入图片描述
suspend-button-luckytree npm地址
demo

**3.**如果你的定制化需求很大,可以 clone下来代码自己 改造 ,主要代码就是 拖拽吸附 ,他不支持自己传入 一个 ui。只支持一个 img 。所以你可以clone 下来 自己改造一下 。
比如:我自己 需要传入 一个 自定义的dom ,而且 需要传入初始化的位置。
自定义dom 可以利用 props.children,自定义 初始化位置可以 传入style。
代码片段截图

完整代码如下:
index.jsx

import React, { Component } from "react"
import "./index.scss"

class suspendButton extends Component {
  constructor(props) {
    super(props)
    this.state = {
      oLeft: "",
      oTop: ""
    }
    this.$vm = null // 悬浮按钮
    this.moving = false // 移动状态

    this.oW = null // 悬钮距离
    this.oH = null

    this.htmlWidth = null // 页面宽度
    this.htmlHeight = null

    this.bWidth = null // 悬钮宽度
    this.bHeight = null

    this.click = false // 是否是点击
  }

  // 移动触发
  onTouchStart(e) {
    e = e.touches[0]
    this.click = true

    this.oW = e.clientX - this.$vm.getBoundingClientRect().left
    this.oH = e.clientY - this.$vm.getBoundingClientRect().top

    this.htmlWidth = document.documentElement.clientWidth
    this.htmlHeight = document.documentElement.clientHeight

    this.bWidth = this.$vm.offsetWidth
    this.bHeight = this.$vm.offsetHeight

    let oLeft = e.clientX - this.oW
    let oTop = e.clientY - this.oH
    //不加这个点击时会出现 往右移动的情况
     this.$vm.bottom=null
    this.$vm.right=null
    
    this.setState({
      oLeft,
      oTop
    })

    this.moving = true
  }

  // 移动结束
  onTouchEnd(e) {
    this.moving = false

    this.$vm.className = this.$vm.className + " t-suspend-button-animate"

    // 左侧距离
    let oLeft = this.state.oLeft
    if (oLeft < (this.htmlWidth - this.bWidth) / 2) {
      oLeft = 0
    } else {
      oLeft = this.htmlWidth - this.bWidth
    }

    if (this.click) {
      if (this.props && this.props.onClick) {
        this.props.onClick()
      }
    }
    // }
    // if(oTop < 0) {
    //   oTop = 0
    // } else if (oTop > this.htmlHeight - this.bHeight) {
    //   oTop = this.htmlHeight - this.bHeight
    // }

    this.setState({
      oLeft
      // oTop
    })
  }

  componentDidMount() {
    this.$vm.addEventListener(
      "touchmove",
      e => {
        if (e.cancelable) {
          e.preventDefault()
        }
      },
      {
        passive: false
      }
    )
  }

  // 开始移动
  onTouchMove(e) {
    this.$vm.className = "t-suspend-button"
    this.$vm.bottom=null
    this.$vm.right=null
    this.moving && this.onMove(e)
  }

  // 移动中
  onMove(e) {
    e = e.touches[0]
    this.click = false

    // 左侧距离
    let oLeft = e.clientX - this.oW
    let oTop = e.clientY - this.oH
    if (oLeft < 0) {
      oLeft = 0
    } else if (oLeft > this.htmlWidth - this.bWidth) {
      oLeft = this.htmlWidth - this.bWidth
    }
    if (oTop < 0) {
      oTop = 0
    } else if (oTop > this.htmlHeight - this.bHeight) {
      oTop = this.htmlHeight - this.bHeight
    }

    this.setState({
      oLeft,
      oTop
    })
  }

  render() {
    const { img, style } = this.props
    return (
      <span
        className="t-suspend-button"
        ref={$vm => (this.$vm = $vm)}
        onTouchStart={e => this.onTouchStart(e)}
        onTouchMove={e => this.onTouchMove(e)}
        onTouchEnd={e => this.onTouchEnd(e)}
        style={{
          position:"fixed",
          right:"12px",
          bottom:"100px",
          left: `${this.state.oLeft}px`,
          top: `${this.state.oTop}px`,
          ...style
        }}
      >
        {img ? <img src={img} alt="" /> : this.props.children}
      </span>
    )
  }
}

export default suspendButton


index.scss (css,less都行),我克隆下来 他这个默认的样式不生效

.t-suspend-button {
  //position: fixed;
  //top: 400px;
  //right: 0;
  //width: 4rem;
  //height: 4rem;
  //border-radius: 2rem;
  //box-shadow: 0px 0px 5px rgba(#000000, .4);
}

// .t-suspend-button img {
//   width: 100%;
//   height: 100%;
// }

.t-suspend-button-animate {
  transition-duration: .4s;
}

使用方法:
1.在需要的地方引入组件:
不过具体路径需要根据你自己所放的地方

import SuspendButton from '../components/suspendButton'; 

2.在组件里使用:

<SuspendButton >
        xxx
 </SuspendButton>

属性

属性名类型默认值说明
imgstring图片地址
styleobj{ position:“fixed”,right:“12px”, bottom:“100px”}样式
childrenreactNode自定义的样式和元素

备注:我为什么style 加了默认值呢,是因为我clone下来的代码,放到我的项目里后发现 样式样式不生效。
4. 其实 react-draggable 这种拖拽类的 插件也可以实现,只不过没必要用这种。如果只是像实现简单的拖拽。上面的插件就够用。

5. gtihub上找其实有很多,类似的,甚至悬浮菜单,clone 下来自己修改一下样式
在这里插入图片描述
github suspend-button

总结:

我改造的很简单,如果你需要更复杂的效果也可以自己clone下来自己改造。比如:

  1. 动画可以自己传入
  2. 点击后 弹出 一个菜单 菜单里能操作

如果需要支持 pc,移动,ts 则可以选其他版本。甚至可以字及编写一个组件,因为核心代码就是拖拽 这个不限于 react,只要 支持 css语法 基本都能用。vue里把主要代码拿出来,改成vue的语法应该也可以。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React-dnd 拖拽排序是一种常见的前端交互方式,可以通过以下代码实现:1. 首先需要安装 react-dnd 和 react-dnd-html5-backend 两个库:``` npm install --save react-dnd react-dnd-html5-backend ```2. 在组件中引入 DragDropContext、Droppable 和 Draggable 组件:``` import { DragDropContext, Droppable, Draggable } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; ```3. 定义一个数组作为拖拽列表的数据源:``` const items = [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, { id: 4, name: 'Item 4' }, { id: 5, name: 'Item 5' }, ]; ```4. 在组件中使用 DragDropContext 组件包裹整个列表,并在其中使用 Droppable 组件包裹每个拖拽项:``` <DragDropContext backend={HTML5Backend}> <Droppable droppableId="items"> {(provided) => ( <ul {...provided.droppableProps} ref={provided.innerRef}> {items.map((item, index) => ( <Draggable key={item.id} draggableId={item.id.toString()} index={index}> {(provided) => ( <li {...provided.draggableProps} {...provided.dragHandleProps} ref={provided.innerRef} > {item.name} </li> )} </Draggable> ))} {provided.placeholder} </ul> )} </Droppable> </DragDropContext> ```5. 在 Draggable 组件中使用 provided.draggableProps 和 provided.dragHandleProps 属性来实现拖拽功能,同时使用 provided.innerRef 属性来获取拖拽元素的引用。6. 在 Droppable 组件中使用 provided.droppableProps 和 provided.innerRef 属性来实现拖拽排序功能。7. 最后,需要在 DragDropContext 组件中定义 onDragEnd 回调函数来处理拖拽结束后的逻辑:``` function onDragEnd(result) { if (!result.destination) { return; } const newItems = Array.from(items); const [reorderedItem] = newItems.splice(result.source.index, 1); newItems.splice(result.destination.index, , reorderedItem); setItems(newItems); }<DragDropContext backend={HTML5Backend} onDragEnd={onDragEnd}> ... </DragDropContext> ```8. 在 onDragEnd 回调函数中,首先判断是否有目标位置,如果没有则直接返回。然后使用 Array.from 方法复制一份原始数据源,从中取出被拖拽的元素并删除,再将其插入到目标位置中,最后使用 setItems 函数更新数据源。以上就是 react-dnd 拖拽排序的代码实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崽崽的谷雨

漫漫前端路,摸爬滚打

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值