import React, { Component } from "react"; import $ from "jquery"; import style from "./index.less"; import ReactDom from "react-dom"; import closeImg from "./img/closeImg.png"; import imgBgActive from "./img/imgBgActive.png"; import plus from "./img/plus.png"; import reduce from "./img/reduce.png"; import nextImg from "./img/nextImg.png"; import prevImg from "./img/prevImg.png"; import rotated from "./img/rotated.png"; import {imgDragMove} from '@utils/drag'; class RotateImg extends Component { constructor(props){ super(props); this.state={ de:0, current:0, length:0, imgWidth:'auto', imgHeight:'auto', mgLeft:'', mgTop:'', left:'50%', top:'50%' } this.handlePrev = this.handlePrev.bind(this) this.handleNext = this.handleNext.bind(this) this.handleRotate = this.handleRotate.bind(this) this.handlePlus = this.handlePlus.bind(this) this.handleReduce = this.handleReduce.bind(this) } componentDidMount(){ imgDragMove('add') const { idx,imgLis,windowHeight } = this.props; let currentImg = imgLis[idx]; let scaleHeight = windowHeight*0.8; let width = currentImg.width,height = currentImg.height; if(width>height){ if(width > scaleHeight){ width=scaleHeight height=height*scaleHeight/width } }else{ if(height > scaleHeight){ height = scaleHeight width = width*scaleHeight/height } } this.setState({ current:idx, length:imgLis.length, imgWidth:width, imgHeight:height, mgLeft:-width/2, mgTop:-height/2 }) } getStyle(){ const { imgDetail,idx,imgLis } = this.props; let currentImg = imgLis[this.state.current]; let width = currentImg.width,height = currentImg.height; let isLorR = width > height; return { width:width, height:height, marginLeft:-width/2, marginTop:-height/2 } } handleNext(){ this.props.setMove(false) const { imgLis,isMove,windowHeight } = this.props; let scaleHeight = windowHeight*0.8; let tmpIdx = this.state.current; let tmpLen = this.state.length,width,height; if(tmpIdx == tmpLen-1){ width = imgLis[0].width height = imgLis[0].height }else{ width = imgLis[tmpIdx+1].width height = imgLis[tmpIdx+1].height } if(width>height){ if(width > scaleHeight){ width=scaleHeight height=height*scaleHeight/width } }else{ if(height > scaleHeight){ height = scaleHeight width = width*scaleHeight/height } } let imgDom = this.refs.rotateImg.getDOMNode(); $(imgDom).css({ left:'50%', top:'50%' }) this.setState({ current:tmpIdx == tmpLen-1?0:(tmpIdx+1), imgWidth:width, imgHeight:height, mgLeft:-width/2, mgTop:-height/2, de:0 }) } handlePrev(){ const { imgLis,windowHeight } = this.props; let scaleHeight = windowHeight*0.8; let tmpIdx = this.state.current; let tmpLen = this.state.length,width,height; if(tmpIdx == 0){ width = imgLis[tmpLen-1].width height = imgLis[tmpLen-1].height }else{ width = imgLis[tmpIdx-1].width height = imgLis[tmpIdx-1].height } if(width>height){ if(width > scaleHeight){ width=scaleHeight height=height*scaleHeight/width } }else{ if(height > scaleHeight){ height = scaleHeight width = width*scaleHeight/height } } let imgDom = this.refs.rotateImg.getDOMNode(); $(imgDom).css({ left:'50%', top:'50%' }) this.setState({ current:tmpIdx == 0?tmpLen-1:tmpIdx-1, imgWidth:width, imgHeight:height, mgLeft:-width/2, mgTop:-height/2, de:0 }) this.props.setMove(false) } handlePlus(){ const {imgWidth,imgHeight,isMove} = this.state; if(this.props.isMove){ this.setState({ imgWidth:1.2*imgWidth, imgHeight:1.2*imgHeight }) return; } this.setState({ imgWidth:1.2*imgWidth, imgHeight:1.2*imgHeight, mgLeft:-imgWidth*1.2/2, mgTop:-imgHeight*1.2/2 }) } handleReduce(){ const {imgWidth,imgHeight} = this.state; if(this.props.isMove){ this.setState({ imgWidth:imgWidth*0.8, imgHeight:imgHeight*0.8 }) return; } this.setState({ imgWidth:imgWidth*0.8, imgHeight:imgHeight*0.8, mgLeft:-imgWidth*0.8/2, mgTop:-imgHeight*0.8/2 }) } handleRotate(flg){ let deg = this.state.de; if(flg){ deg = deg-90 }else{ deg = deg-0+90 } this.setState({ de:deg }) } render() { const { imgLis,handleClose,windowHeight } = this.props; const { current,imgWidth,imgHeight,mgLeft,mgTop,de,left,top } = this.state; const domNode = document.getElementById('root'); return ReactDom.createPortal(
预览图片
图片放大 图片缩小 图片旋转
close
下一张 上一张
,domNode) } } export default RotateImg;