zoukankan      html  css  js  c++  java
  • react项目中实现元素的拖动和缩放实例

      在react项目中实现此功能可借助 react-rnd 库,文档地址:https://github.com/bokuweb/react-rnd#Screenshot 。下面是实例运用:

      

    import React, { Component } from 'react'
    import PropTypes from 'prop-types'
    import assign from 'object-assign'
    import classNames from 'classnames'
    import _ from 'lodash'
    import { Rnd } from 'react-rnd'
    
    import './index.less'
    
    class ScreenShareCapture extends Component {
      static propTypes = {
        style: PropTypes.object,
      }
    
      static defaultProps = {
        style: {},
      }
    
      state = {
        x: 0,
        y: 0,
         500,
        height: 281.25,
      }
    
      componentDidMount() {
      }
    
      componentWillUnmount() {
      }
    
      updateRndElemLastSizePos = () => {
        this.rndElemLastSizePos = {
          x: this.state.x,
          y: this.state.y,
           this.state.width,
          height: this.state.height,
        }
      }
    
      calculateRndElemSizePos = (dir, ref, sizeDelta, position) => {
        const sizePos = _.clone(this.rndElemLastSizePos)
        const heightWidthRatio = 0.5625
        sizePos.x = position.x
        sizePos.y = position.y
        sizePos.width = ref.offsetWidth
        sizePos.height = ref.offsetWidth * heightWidthRatio
    
        this.setState(sizePos)
        return sizePos
      }
    
    
      handleDragStart = () => {
        this.updateRndElemLastSizePos()
      }
    
      handleDrag = (e, data) => {
        this.setState({
          x: data.x,
          y: data.y,
        })
      }
    
      handleDragStop = (e, data) => {
        this.setState({
          x: data.x,
          y: data.y,
        })
      }
    
      handleResizeStart = () => {
        this.updateRndElemLastSizePos()
      }
    
      handleResize = (e, dir, ref, delta, position) => {
        console.log('beibei handleresize', e, dir, ref, delta, position)
        this.calculateRndElemSizePos(dir, ref, delta, position)
      }
    
      rndElemLastSizePos
    
      rnd
    
      render() {
        const wrapStyles = assign({}, this.props.style)
        return (
          <Rnd
            style={wrapStyles}
            bounds=".teacher-layout-component-wrap"
            dragHandleClassName="screen-share-bottom-content-wrap"
            enableResizing={{
              top: false,
              right: false,
              bottom: false,
              left: false,
              topRight: true,
              bottomRight: true,
              bottomLeft: true,
              topLeft: true,
            }}
            position={{
              x: this.state.x,
              y: this.state.y,
            }}
            size={{
               this.state.width,
              height: this.state.height,
            }}
            lockAspectRatio={16 / 9}
            maxWidth="100%"
            maxHeight="100%"
            minWidth={400}
            minHeight={225}
            onDragStart={this.handleDragStart}
            onDrag={this.handleDrag}
            onDragStop={this.handleDragStop}
            onResizeStart={this.handleResizeStart}
            onResize={this.handleResize}
            onResizeStop={_.noop}
            ref={(r) => { this.rnd = r }}
          >
            <div className="screen-share-capture-component-wrap">
              <div className="screen-share-main-content-wrap">
                <span className="start-screen-share-button">开始屏幕共享</span>
              </div>
              <div className="screen-share-bottom-content-wrap">
                <div className="screen-share-bottom-left">
                  <i
                    className="preview-icon"
                  />
                  <span className="preview-text">预览</span>
                </div>
                <div className="screen-share-bottom-right">
                  <i
                    className={classNames({
                      'device-icon': true,
                      'camera-icon': true,
                      active: false,
                    })}
                  />
                  <i
                    className={classNames({
                      'device-icon': true,
                      'mic-icon': true,
                      active: false,
                    })}
                  />
                  <i
                    className={classNames({
                      'device-icon': true,
                      'speaker-icon': true,
                      active: false,
                    })}
                  />
                  <i
                    className={classNames({
                      'device-icon': true,
                      'wifi-icon': true,
                      active: false,
                    })}
                  />
                  {/*<i*/}
                    {/*className={classNames({*/}
                      {/*'device-icon': true,*/}
                      {/*'restart-icon': true,*/}
                      {/*active: false,*/}
                    {/*})}*/}
                  {/*/>*/}
                  <i
                    className={classNames({
                      'device-icon': true,
                      'exit-icon': true,
                      active: false,
                    })}
                  />
                </div>
              </div>
            </div>
          </Rnd>
        )
      }
    }
    
    export default ScreenShareCapture

      下面是css样式:

      

    .screen-share-capture-component-wrap{
      box-sizing: border-box;
      width: 100%;
      height: 100%;
      position: relative;
      //top: 0;
      //left: 0;
      //width: 100%;
      //height: 100%;
      //display: flex;
      //flex-direction: column;
      //align-items: center;
      //justify-content: center;
      //background-color: red;
    
      .screen-share-main-content-wrap{
        box-sizing: border-box;
        width: 100%;
        height: 100%;
        border-radius: 2px;
        border: solid 2px #ff4343;
        background-color: #ffffff;
        display: flex;
        justify-content: center;
        align-items: center;
    
    
        .start-screen-share-button{
          display: inline-block;
          width: 120px;
          height: 36px;
          line-height: 36px;
          text-align: center;
          border-radius: 2px;
          background-color: #ff4343;
          color: #ffffff;
          font-size: 14px;
          opacity: 0.8;
          cursor: pointer;
        }
      }
    
      .screen-share-bottom-content-wrap{
        width: 100%;
        height: 36px;
        border-radius: 2px;
        background-color: #5d6674;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        position: absolute;
        bottom: -36px;
        left: 0;
    
        .screen-share-bottom-left{
          width: 72px;
          height: 32px;
          border-radius: 2px;
          border: solid 1px #ffffff;
          background-color: #5d6674;
          margin: 3px;
          display: flex;
          flex-direction: row;
          justify-content: space-around;
          align-items: center;
          cursor: pointer;
    
          .preview-icon{
            width: 24px;
            height: 24px;
            outline: none;
            cursor: pointer;
            background: url('~ROOT/shared/assets/image/vn-screen-preview-42-42.png') no-repeat center;
            background-size: 16px 16px;
          }
    
          .preview-text{
            color: #ffffff;
            font-size: 14px;
          }
        }
    
        .screen-share-bottom-right{
          display: flex;
          flex-direction: row;
          align-items: center;
    
          .device-icon {
            width: 24px;
            height: 24px;
            outline: none;
            cursor: pointer;
            margin-right: 11px;
    
            &:nth-last-child(2){
              margin-right: 38px;
            }
    
          }
    
          .camera-icon {
            background: url('~ROOT/shared/assets/image/vn-screen-camera-off-66-66.png') no-repeat center;
            background-size: 22px 22px;
          }
    
          .mic-icon {
            background: url('~ROOT/shared/assets/image/vn-screen-voice-on-54-72.png') no-repeat center;
            background-size: 18px 24px;
          }
    
          .speaker-icon {
            background: url('~ROOT/shared/assets/image/vn-screen-speaker-60-66.png') no-repeat center;
            background-size: 20px 22px;
          }
    
          .wifi-icon {
            background: url('~ROOT/shared/assets/image/vn-screen-wifi-72-57.png') no-repeat center;
            background-size: 24px 19px;
          }
    
          .restart-icon{
            background: url('~ROOT/shared/assets/image/vn-screen-restart-60-60.png') no-repeat center;
            background-size: 19px 19px;
          }
    
          .exit-icon{
            background: url('~ROOT/shared/assets/image/vn-screen-exit-60-60.png') no-repeat center;
            background-size: 19px 19px;
          }
    
        }
    
      }
    }
  • 相关阅读:
    nodejs文本文件的读写
    nodejs目录与文件遍历
    问题建模
    Using JavaScript with WKWebView in iOS 8
    git 合并提交
    iOS中类簇的使用--__NSPlaceholderArray __NSPlaceholderDictionary
    QQ音乐Android客户端Web页面通用性能优化实践
    HDFS+ClickHouse+Spark:从0到1实现一款轻量级大数据分析系统
    图计算黑科技:打开中文词嵌入训练实践新模式
    腾讯“神盾-联邦计算”平台带你翻越数据合作的重重大山
  • 原文地址:https://www.cnblogs.com/chenbeibei520/p/10478549.html
Copyright © 2011-2022 走看看