小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

react中實現(xiàn)可拖動div

 行者花雕 2021-10-30

把拖動div功能用react封裝成class,在頁面直接引入該class即可使用。

title為可拖動區(qū)域。panel為要實現(xiàn)拖動的容器。

優(yōu)化了拖動框超出頁面范圍的情況,也優(yōu)化了拖動太快時鼠標超出可拖動區(qū)域的情況,優(yōu)化了拖動會卡頓的情況。

 

頁面中添加引入方法:

<Draggable panelId="要拖動容器的id" titleId="容器內(nèi)標題的id" contentId="容器內(nèi)除標題外的其他部分id" setPanelPosition={this.setPanelPosition.bind(this)}/>

頁面中添加拖拽回調(diào)函數(shù)

  //推拽回調(diào)函數(shù)
  setPanelPosition(left,top){
    this.setState({pageX: left, pageY: top})
  }

要拖動的div如下:

<div id="要拖動的id" style={{left:this.state.pageX,top:this.state.pageY}}></div>

 

封裝的class代碼:

import React from 'react';
class Draggable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }
  //拖拽
  initDrag(){
    let {panelId,titleId,contentId} = this.props;
    this.panelDom = document.getElementById(panelId);
    this.titleDom = document.getElementById(titleId);
    this.contentDom = document.getElementById(contentId);
    this.backgroundDom = document.body;
    this.bindEvent();
  }

  //region event
  componentDidMount() {
    this.initDrag();
  }
  bindEvent(){
    this.titleDom.onmousedown = this.onMouseDown.bind(this);
    this.titleDom.onmouseup = this.onMouseUp.bind(this);
    this.titleDom.onmousemove = this.onMouseMove.bind(this);

    this.contentDom.onmouseup = this.onContentMouseUp.bind(this);
    this.contentDom.onmousemove = this.onContentMouseMove.bind(this);

    this.backgroundDom.onmouseup = this.onBackgroundMouseUp.bind(this);
    this.backgroundDom.onmousemove = this.onBackgroundMouseMove.bind(this);
    let step = ()=>{
      this.activeAnimation = true;
      window.requestAnimationFrame(step);
    };
    window.requestAnimationFrame(step);
  }

  /**
   * 鼠標按下,設(shè)置modal狀態(tài)為可移動,并注冊鼠標移動事件
   * 計算鼠標按下時,指針所在位置與modal位置以及兩者的差值
   **/
  onMouseDown (e) {
    const position = this.getPosition(e)
    this.setState({moving: true, diffX: position.diffX, diffY: position.diffY})
  }

  // 松開鼠標,設(shè)置modal狀態(tài)為不可移動
  onMouseUp (e) {
    const { moving } = this.state
    moving && this.setState({moving: false});
  }

  // 鼠標移動重新設(shè)置modal的位置
  onMouseMove (e) {
    const {moving, diffX, diffY} = this.state
    if (moving) {
      if(this.activeAnimation){
        // 獲取鼠標位置數(shù)據(jù)
        const position = this.getPosition(e)
        // 計算modal應(yīng)該隨鼠標移動到的坐標
        const x = position.mouseX - diffX
        const y = position.mouseY - diffY
        // 窗口大小,結(jié)構(gòu)限制,需要做調(diào)整,減去側(cè)邊欄寬度
        const { clientWidth, clientHeight } = document.documentElement
        const modal = this.panelDom
        if (modal) {
          // 計算modal坐標的最大值
          const maxHeight = clientHeight - modal.offsetHeight
          const maxWidth = clientWidth - modal.offsetWidth
          // 判斷得出modal的最終位置,不得超出瀏覽器可見窗口
          const left = x > 0 ? (x < maxWidth ? x : maxWidth) : 0
          const top = y > 0 ? (y < maxHeight ? y : maxHeight) : 0
          if(this.props.setPanelPosition){
            this.props.setPanelPosition(left,top);
          }
        }
        this.activeAnimation = false;
      }
    }
  }
  onContentMouseMove(e){
    let obj = {};
    obj.target = this.titleDom;
    obj.pageX = e.pageX;
    obj.screenY = e.screenY;
    this.onMouseMove(obj);
  }
  onContentMouseUp(){
    this.onMouseUp();
  }
  onBackgroundMouseMove(e){
    let obj = {};
    obj.target = this.titleDom;
    obj.pageX = e.pageX;
    obj.screenY = e.screenY;
    this.onMouseMove(obj);
  }
  onBackgroundMouseUp(){
    this.onMouseUp();
  }
  //endregion

  //region request
  // 獲取鼠標點擊title時的坐標、title的坐標以及兩者的位移
  getPosition (e) {
    // 標題DOM元素titleDom
    const titleDom = e.target
    // titleDom的坐標(視窗)
    const X = titleDom.getBoundingClientRect().left
    // 由于Y軸出現(xiàn)滾動條,需要與鼠標保持一致,存儲頁面相對位置
    const Y = this.panelDom.offsetTop

    // 鼠標點擊的坐標(頁面)
    let mouseX = e.pageX
    let mouseY = e.screenY
    // 鼠標點擊位置與modal的位移
    const diffX = mouseX - X
    const diffY = mouseY - Y
    return {X, Y, mouseX, mouseY, diffX, diffY}
  }
  //endregion

  //region render
  //endregion

  //region clear
  //endregion

  render() {
    return (
      <>
      </>
    );
  }
}
export default Draggable;

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多