index.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import React, { Component } from "react";
  2. import $ from "jquery";
  3. import style from "./index.less";
  4. import ReactDom from "react-dom";
  5. import closeImg from "./img/closeImg.png";
  6. import imgBgActive from "./img/imgBgActive.png";
  7. import plus from "./img/plus.png";
  8. import reduce from "./img/reduce.png";
  9. import nextImg from "./img/nextImg.png";
  10. import prevImg from "./img/prevImg.png";
  11. import rotated from "./img/rotated.png";
  12. import {imgDragMove} from '@utils/drag';
  13. class RotateImg extends Component {
  14. constructor(props){
  15. super(props);
  16. this.state={
  17. de:0,
  18. current:0,
  19. length:0,
  20. imgWidth:'auto',
  21. imgHeight:'auto',
  22. mgLeft:'',
  23. mgTop:'',
  24. left:'50%',
  25. top:'50%'
  26. }
  27. this.handlePrev = this.handlePrev.bind(this)
  28. this.handleNext = this.handleNext.bind(this)
  29. this.handleRotate = this.handleRotate.bind(this)
  30. this.handlePlus = this.handlePlus.bind(this)
  31. this.handleReduce = this.handleReduce.bind(this)
  32. }
  33. componentDidMount(){
  34. imgDragMove('add')
  35. const { idx,imgLis,windowHeight } = this.props;
  36. let currentImg = imgLis[idx];
  37. let scaleHeight = windowHeight*0.8;
  38. let width = currentImg.width,height = currentImg.height;
  39. if(width>height){
  40. if(width > scaleHeight){
  41. width=scaleHeight
  42. height=height*scaleHeight/width
  43. }
  44. }else{
  45. if(height > scaleHeight){
  46. height = scaleHeight
  47. width = width*scaleHeight/height
  48. }
  49. }
  50. this.setState({
  51. current:idx,
  52. length:imgLis.length,
  53. imgWidth:width,
  54. imgHeight:height,
  55. mgLeft:-width/2,
  56. mgTop:-height/2
  57. })
  58. }
  59. getStyle(){
  60. const { imgDetail,idx,imgLis } = this.props;
  61. let currentImg = imgLis[this.state.current];
  62. let width = currentImg.width,height = currentImg.height;
  63. let isLorR = width > height;
  64. return {
  65. width:width,
  66. height:height,
  67. marginLeft:-width/2,
  68. marginTop:-height/2
  69. }
  70. }
  71. handleNext(){
  72. this.props.setMove(false)
  73. const { imgLis,isMove,windowHeight } = this.props;
  74. let scaleHeight = windowHeight*0.8;
  75. let tmpIdx = this.state.current;
  76. let tmpLen = this.state.length,width,height;
  77. if(tmpIdx == tmpLen-1){
  78. width = imgLis[0].width
  79. height = imgLis[0].height
  80. }else{
  81. width = imgLis[tmpIdx+1].width
  82. height = imgLis[tmpIdx+1].height
  83. }
  84. if(width>height){
  85. if(width > scaleHeight){
  86. width=scaleHeight
  87. height=height*scaleHeight/width
  88. }
  89. }else{
  90. if(height > scaleHeight){
  91. height = scaleHeight
  92. width = width*scaleHeight/height
  93. }
  94. }
  95. let imgDom = this.refs.rotateImg.getDOMNode();
  96. $(imgDom).css({
  97. left:'50%',
  98. top:'50%'
  99. })
  100. this.setState({
  101. current:tmpIdx == tmpLen-1?0:(tmpIdx+1),
  102. imgWidth:width,
  103. imgHeight:height,
  104. mgLeft:-width/2,
  105. mgTop:-height/2,
  106. de:0
  107. })
  108. }
  109. handlePrev(){
  110. const { imgLis,windowHeight } = this.props;
  111. let scaleHeight = windowHeight*0.8;
  112. let tmpIdx = this.state.current;
  113. let tmpLen = this.state.length,width,height;
  114. if(tmpIdx == 0){
  115. width = imgLis[tmpLen-1].width
  116. height = imgLis[tmpLen-1].height
  117. }else{
  118. width = imgLis[tmpIdx-1].width
  119. height = imgLis[tmpIdx-1].height
  120. }
  121. if(width>height){
  122. if(width > scaleHeight){
  123. width=scaleHeight
  124. height=height*scaleHeight/width
  125. }
  126. }else{
  127. if(height > scaleHeight){
  128. height = scaleHeight
  129. width = width*scaleHeight/height
  130. }
  131. }
  132. let imgDom = this.refs.rotateImg.getDOMNode();
  133. $(imgDom).css({
  134. left:'50%',
  135. top:'50%'
  136. })
  137. this.setState({
  138. current:tmpIdx == 0?tmpLen-1:tmpIdx-1,
  139. imgWidth:width,
  140. imgHeight:height,
  141. mgLeft:-width/2,
  142. mgTop:-height/2,
  143. de:0
  144. })
  145. this.props.setMove(false)
  146. }
  147. handlePlus(){
  148. const {imgWidth,imgHeight,isMove} = this.state;
  149. if(this.props.isMove){
  150. this.setState({
  151. imgWidth:1.2*imgWidth,
  152. imgHeight:1.2*imgHeight
  153. })
  154. return;
  155. }
  156. this.setState({
  157. imgWidth:1.2*imgWidth,
  158. imgHeight:1.2*imgHeight,
  159. mgLeft:-imgWidth*1.2/2,
  160. mgTop:-imgHeight*1.2/2
  161. })
  162. }
  163. handleReduce(){
  164. const {imgWidth,imgHeight} = this.state;
  165. if(this.props.isMove){
  166. this.setState({
  167. imgWidth:imgWidth*0.8,
  168. imgHeight:imgHeight*0.8
  169. })
  170. return;
  171. }
  172. this.setState({
  173. imgWidth:imgWidth*0.8,
  174. imgHeight:imgHeight*0.8,
  175. mgLeft:-imgWidth*0.8/2,
  176. mgTop:-imgHeight*0.8/2
  177. })
  178. }
  179. handleRotate(flg){
  180. let deg = this.state.de;
  181. if(flg){
  182. deg = deg-90
  183. }else{
  184. deg = deg-0+90
  185. }
  186. this.setState({
  187. de:deg
  188. })
  189. }
  190. render() {
  191. const { imgLis,handleClose,windowHeight } = this.props;
  192. const { current,imgWidth,imgHeight,mgLeft,mgTop,de,left,top } = this.state;
  193. const domNode = document.getElementById('root');
  194. return ReactDom.createPortal(<div className={style.preImgWrap}>
  195. <div className={style.modal} onClick={handleClose}></div>
  196. <div className={style.mainWrap}>
  197. <div className={style.imgWrap}
  198. id="previewWrapper"
  199. style={{
  200. width:0.8*windowHeight+'px',
  201. height:0.8*windowHeight+'px',
  202. marginLeft:-0.8*windowHeight/2+'px',
  203. marginTop:-0.8*windowHeight/2+'px'
  204. }}>
  205. <img className={style.rotateImg}
  206. id="drugImg"
  207. ref="rotateImg"
  208. src={imgLis[current].originalImage}
  209. style={{
  210. width:imgWidth+'px',
  211. height:imgHeight+'px',
  212. marginLeft:mgLeft+'px',
  213. marginTop:mgTop+'px',
  214. transform:"rotate(" + de + "deg)",
  215. left:left,
  216. top:top,
  217. }}
  218. alt="预览图片"/>
  219. <div className={style.activeBar}>
  220. <img className={style.plus} src={plus} onClick={this.handlePlus} alt="图片放大"/>
  221. <img className={style.reduce} src={reduce} onClick={this.handleReduce} alt="图片缩小"/>
  222. <img className={style.rotate} src={rotated} onClick={this.handleRotate} alt="图片旋转"/>
  223. </div>
  224. <img src={closeImg} onClick={handleClose} className={style.close} alt="close"/>
  225. </div>
  226. <img src={nextImg} className={style.next} onClick={this.handleNext} alt="下一张"/>
  227. <img src={prevImg} className={style.prev} onClick={this.handlePrev} alt="上一张"/>
  228. </div>
  229. </div>,domNode)
  230. }
  231. }
  232. export default RotateImg;