在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ React項目里,做一個拖拽組件功能,沒報錯但是無法顯示出來和拖拽

React項目里,做一個拖拽組件功能,沒報錯但是無法顯示出來和拖拽

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//設(shè)置下是不是在drag狀態(tài)
        };
        this.handleClick = this.handleClick.bind(this);
        //進(jìn)入title的時候把鼠標(biāo)指針換一下
        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;
回答
編輯回答
骨殘心

現(xiàn)成的拖拽組件:react-dnd react-draggable

2018年3月26日 01:44