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

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

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    React项目里,做一个拖拽组件功能,没报错但是无法显示出来和拖拽
    56
    0
    import React from 'react';
    import './assets/styles';
    import TextDrag from '../../../../components/TextDrag';
    
    class LoginPage extends React.Component{
        constructor(props){
            super(props);
            this.state={
                top:(window.innerHeight - 220) /2,
                left:(window.innerWidth - 320) /2
            };
        }
    
        render(){
            const top= this.state.top;
            const left = this.state.left;
            return (
                <div className = 'logpad' style={{'top': top+'px', 'left':left+'px'}} >
                    <TextDrag
                        display = {this.props.display}
                        top={50}
                        left={50}
                    />
                </div>);
        }
    }
    
    
    export default LoginPage;
    import React from 'react'
    import './assets/styles'
    
    class TextDrag extends React.Component {
    
        constructor(props){
            super(props);
            this.state={
                display: 'block',
                cursor:'pointer',
                clientx: null,
                clienty: null,
                isDragging: false//设置下是不是在drag状态
            };
            this.handleClick = this.handleClick.bind(this);
            //进入title的时候把鼠标指针换一下
            this.handleMouseEnter = this.handleMouseEnter.bind(this);
            this.handleMouseLeave = this.handleMouseLeave.bind(this);
            //拖拽
            this.handleMouseDown = this.handleMouseDown.bind(this);
            this.handleMouseMove = this.handleMouseMove.bind(this);
            this.handleMouseUp = this.handleMouseUp.bind(this);
        }
        handleMouseEnter(e){
            this.setState({cursor:'move'});
        }
        handleMouseLeave(e){
            this.setState({cursor:'pointer',isDragging:false});
        }
        handleClick(){
            var newS = false;
            this.setState({display: newS});
        }
        handleMouseDown(e){
            this.setState({
                relativex:e.pageX - this.props.left,
                relativey:e.pageY - this.props.top,
                isDragging:true});
        }
        handleMouseMove(e){
            if(this.state.isDragging === true){
                const maxX = window.innerWidth - 320;
                const maxY = window.innerHeight - 220;
                var moveX= e.pageX - this.state.relativex;
                var moveY = e.pageY - this.state.relativey;
                //用来限制,不让登录框超过边界
                moveX = Math.min(Math.max(0,moveX),maxX);
                moveY = Math.min(Math.max(0,moveY),maxY);
            }else{
                return false;
            }
        }
        handleMouseUp(e){
            e.preventDefault();
            this.setState({
                isDragging:false,
                clientx:null,
                clienty:null
            });
    
        }
        render(){
            const cursor = this.state.cursor;
            return(
                <div className = 'title' style={{'cursor':cursor}} onMouseEnter ={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} onMouseDown={this.handleMouseDown} onMouseMove = {this.handleMouseMove} onMouseUp={this.handleMouseUp}>
                    <h4>登录</h4>
                    <div className='delete' onClick = {this.handleClick}>X</div>
                </div>);
        }
    }
    
    export default TextDrag;
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 建州客 普通会员 1楼

      在React中实现拖拽组件功能,你可以使用第三方库如react-dnd或react-draggable。以下是使用react-draggable的示例:

      1. 首先,安装react-draggable: bash npm install react-draggable
      2. 在你的组件中引入react-draggable: jsx import React, { DragDropContext, ReactDraggable } from 'react-draggable';
      3. 使用DragDropContext和Draggable创建拖拽组件: ```jsx const DragDropContext = DragDropContext({ drag: (event) => { // 当开始拖拽时执行的代码 }, drop: (event) => { // 当释放拖拽时执行的代码 }, });

      const Draggable = ReactDraggable({ onDragStart: () => { // 当拖动开始时执行的代码 }, onDragEnd: () => { // 当拖动结束时执行的代码 }, }); 4. 将你的组件添加到DragDropContext中:jsx

      { this.dropTarget = ref; }} /> 5. 实现拖拽组件功能:jsx const DropTarget = React.useContext(DragDropContext); 6. 在你的组件中创建一个DropTarget元素,并添加一个拖拽事件监听器:jsx const DropTarget = React.useContext(DragDropContext); const DropTargetRef = React.useRef(null);

      function DragDropContextProvider() { return (

      { this.dropTarget = ref; }} /> ); }

      function App() { return (

      ); }

      export default App; ``` 在这个示例中,当你拖动一个元素时,元素将从父元素中移除并添加到DropTarget中。当你释放拖拽时,元素将重新添加到父元素中。

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