账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    想问一下关于react实现面板拖曳的功能
    30
    0

    想问一个最基本的react实现拖曳面板的实现~
    有一个add的button,鼠标点击之后会弹出新面板,这个面板是通过绝对定位每次都定位在同一位置上的,想实现鼠标点击这个面板之后的拖曳效果,
    我的实现思路大概就是监听onmousedown,onmousemove和onmouseup,然后用一个flag,先点击,获取到点击时的鼠标的坐标,将flag设为true,然后在mousemove的时候将每一次的位置给styleObj,最后mouseup的时候将flag设为false,

    实现拖曳的效果貌似也不太对。。不太平滑,而且有时候方向是反的,可是我计算这个styleObj的left和top属性值应该没错(move的clientX-点击时得到的clientX+原始的left值)。

    var index=0;
    class Panel extends React.Component{
      constructor (props){
        super(props);
        this.state={
          styleObj:{
            top: 100,
            left:100
          },
          mouseXY:{
            //这个获取的是每一次点击panel时的鼠标的坐标
            x: 0,
            y: 0
          },
          isTouch:false
        };
       this.handleMouseDown = this.handleMouseDown.bind(this);
       this.handleMouseMove = this.handleMouseMove.bind(this);
       this.handleMouseUp = this.handleMouseUp
      }
      handleMouseDown(e){
        //获取当前鼠标按下的位置,并把它放到stat里面去,并且要设置一个flag,使得只有触发了点击,才会 
        console.log(e.clientX);
        this.setState(
          {
          isTouch:!this.state.isTouch,
          mouseXY:{
            x:e.clientX,
            y:e.clientY
          }
          },function(){
            //callback,只有在上面改完才执行
            this.printInfo();
          }
        );
      }
      printInfo(){
        console.log(this.state.mouseXY.x); //输出此时的mouseXY的x
        console.log(this.state.isTouch); //输出true
      }
      handleMouseMove(e){
        //先判断是否已点击,然后再需要让这个panel的x,y随滑动的位置发生相应的改变
        if(this.state.isTouch){
          this.setState({
          styleObj:{
             left:this.state.styleObj.left+e.clientX-this.state.mouseXY.x,
             top:this.state.styleObj.top+e.clientY-this.state.mouseXY.y,
          }
         })
        }
     }
      handleMouseUp(e){
        this.setState({
          isTouch:!this.state.isTouch
        })
      }
      render(){
        return (
        <div className="box" style={this.state.styleObj} onMouseDown={this.handleMouseDown} onMouseMove={this.handleMouseMove} onMouseUp={this.handleMouseUp}></div>
        )
      }
    }
    class App extends React.Component{
     constructor(props){
       super(props);
       this.state={listitem:[]};
       this.handleClick = this.handleClick.bind(this);
     }
      handleClick() {
        index++;
        this.setState({
          listitem :this.state.listitem.concat(<Panel key={index}>{index}</Panel>)
        });
     }
     render(){
      return (
       <div>
       <button onClick={this.handleClick}>
           Add
       </button>
       <div>
         {this.state.listitem}   
       </div>   
      </div>
     )
     }
    }
    ReactDOM.render(
      <App />,
      document.getElementById('root')
    );
    

    在第一条评论下面的是我修改之后不会有bug的,想问下这两者的区别是什么~因为貌似给left和top的数值都是一样的啊

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 烟敛寒林 普通会员 1楼

      在React中,我们可以使用第三方库React-Draggable来实现面板拖曳的功能。React-Draggable是一个基于React的拖放库,它提供了多种拖放控件和各种拖放策略,可以用于实现拖拽操作。

      以下是一个简单的例子:

      ```jsx import React, { useState } from 'react'; import Draggable from 'react-draggable';

      const Panel = () => { const [dragged, setDragged] = useState(null);

      const handleDragStart = (event) => { setDragged(event.target); };

      const handleDrag = (event) => { const { clientX, clientY } = event; const dragedElement = document.getElementById('draggable'); if (dragged) { dragedElement.style.left = ${clientX}px; dragedElement.style.top = ${clientY}px; } };

      return (

      Drag me

      ); };

      export default Panel; ```

      在这个例子中,我们首先在handleDragStart函数中设置了一个状态变量dragged,它存储了我们正在拖动的元素。然后在handleDrag函数中,我们获取了拖动事件的clientXclientY坐标,以及我们正在拖动的元素的id。如果dragged已经存在,我们就更新其lefttop样式,以显示我们正在拖动的元素。

      注意,这个例子使用了id来唯一标识我们的元素。在实际使用中,你可能需要使用其他的标识符,比如元素的classstyle属性。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部