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,
width: 500,
height: 281.25,
}
componentDidMount() {
}
componentWillUnmount() {
}
updateRndElemLastSizePos = () => {
this.rndElemLastSizePos = {
x: this.state.x,
y: this.state.y,
width: 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={{
width: 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:;
//left:;
//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:;
.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;
}
}
}
}
react项目中实现元素的拖动和缩放实例的更多相关文章
- 如何在非 React 项目中使用 Redux
本文作者:胡子大哈 原文链接:https://scriptoj.com/topic/178/如何在非-react-项目中使用-redux 转载请注明出处,保留原文链接和作者信息. 目录 1.前言 2. ...
- react项目中实现搜索关键字呈现高亮状态(一)
最近有个需求,在一个react项目中,实现搜索关键字呈现高亮状态.这个在普通的html文件中还好操作些,在react项目中有点懵逼了,因为react项目中很少操作dom,有点无从下手.但最后还是实现了 ...
- 如何优雅地在React项目中使用Redux
前言 或许你当前的项目还没有到应用Redux的程度,但提前了解一下也没有坏处,本文不会安利大家使用Redux 概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与 ...
- React项目中实现右键自定义菜单
最近在react项目中需要实现一个,右键自定义菜单功能.找了找发现纯react项目里没有什么工具可以实现这样的功能,所以在网上搜了搜相关资料.下面我会附上完整的组件代码. (注:以下代码非本人原创,具 ...
- React项目中使用Mobx状态管理(二)
并上一节使用的是普通的数据状态管理,不过官方推荐使用装饰器模式,而在默认的react项目中是不支持装饰器的,需要手动启用. 官方参考 一.添加配置 官方提供了四种方法, 方法一.使用TypeScrip ...
- 在react项目中使用ECharts
这里我们要在自己搭建的react项目中使用ECharts,我们可以在ECharts官网上看到有一种方式是在 webpack 中使用 ECharts,我们需要的就是这种方法. 我们在使用ECharts之 ...
- 优雅的在React项目中使用Redux
概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与React没有任何关系,其他UI框架也可以使用Redux react-redux React插件,作用:方便在 ...
- 深入浅出TypeScript(5)- 在React项目中使用TypeScript
前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...
- redux在react项目中的应用
今天想跟大家分享一下redux在react项目中的简单使用 1 1.redux使用相关的安装 yarn add redux yarn add react-redux(连接react和redux) 2. ...
随机推荐
- python利用requests库模拟post请求时json的使用
我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...
- idea没配置Tomcat容器报错及解决方法
servlet报错,提示没有一个容器,需要一个容器来运行,说明没有tomcat容器. 看看idea的配置有没有配置tomcat? 果然没有配置tomcat容器(正常画红框的地方会出现配置的tomcat ...
- Xamarin.Forms 自定义 TapGestureRecognizer 附加属性
While creating Xamarin.Forms applications, definitely you are going to need TapGestureRecognizer oft ...
- MongoDB基本信息
一.MongoDB简介 来源:在2007年,由纽约一个叫10gen的创业团队开发,公司现在叫做MongoDB Inc,最初被开发为PAAS(平台即服务). 数据库类型:基于分布式文件存储的数据库.由C ...
- Shadow DOM及自定义标签
参考链接:点我 一.什么是Shadow DOM Shadow DOM,直接翻译的话就是 影子 DOM,可以理解为潜藏在 DOM 结构中并且我们无法直接控制操纵的 DOM 结构.类似于下面这种结构 Sh ...
- 求你显示pdf
123 <iframe src='http://km.shengaitcm.com/ADC/_layouts/15/WopiFrame.aspx?sourcedoc=%2FADC%2FDocLi ...
- CentOS 7 安装Redis4.0
redis是一种内存型的NoSQL数据库,优点是快,常用来做缓存用 redis存储数据的方法是以key-value的形式 value类型支持字符串,列表,哈希等多种类型 环境 : CnetOS7 ...
- Hibernate的工作原理及使用的原因
一.工作原理: 1.读取并解析配置 2.读取并解析映射信息,创建Session Factory 3.打开Session 4.创建事务Transation 5.持久化操作 6.提交事务 7.关闭Sess ...
- Python 第八阶段 学习记录之---算法
算法(Algorithm): 一个计算过程, 解决问题的方法 1.递归的两个特点 - 调用自身 - 结束条件 时间复杂度 - 时间复杂度是用来估计算法运行时间的一个式子(单位) - 一般来说,时间复杂 ...
- liunx驱动----构造和运行模块
以hello world模块为例 #include <linux/init.h> #include <linux/module.h> //在执行 insmod hlello 的 ...