ShowImage.js
6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { Component } from 'react';
import { Image } from 'antd-v4';
import './ShowImage.less';
import FullScreen from '../../../assets/quanping.svg';
export default class ShowImg extends Component {
constructor(props) {
super(props);
this.state = {
f: this.props.firstIndex,
R: 0,
S: 1,
i: 0,
SS: 1,
data: this.props.data,
fullScreen: false,
// eslint-disable-next-line react/no-unused-state
showimgs: this.props.showimgs,
maxNum: this.props.data !== undefined ? this.props.data.length - 1 : 0,
imgVisible: false,
};
}
componentDidMount() {
}
componentWillReceiveProps(props) {
this.setState({ f: props.firstIndex });
}
// 向左预览
leftshow = () => {
const { maxNum } = this.state;
if (this.state.f === 0) {
this.setState({
f: maxNum, R: 0, S: 1, i: 0, SS: 1,
});
} else {
this.setState({
f: this.state.f - 1, R: 0, S: 1, i: 0, SS: 1,
});
}
// eslint-disable-next-line react/no-string-refs
this.refs.imgstyle.setAttribute('src', this.props.data[this.state.f]);
}
// 向右预览
rightshow = () => {
const { maxNum } = this.state;
if (this.state.f === maxNum) {
this.setState({
f: 0, R: 0, S: 1, i: 0, SS: 1,
});
} else {
this.setState({
f: this.state.f + 1, R: 0, S: 1, i: 0, SS: 1,
});
}
// eslint-disable-next-line react/no-string-refs
this.refs.imgstyle.setAttribute('src', this.props.data[this.state.f]);
}
// 顺时针旋转
rotateright = () => {
this.setState({ R: this.state.R + 90 });
// eslint-disable-next-line react/no-string-refs
this.refs.imgstyle.style.transform = `rotate(${this.state.R}deg) scale(${this.state.SS},${this.state.SS})`;
}
// 逆时针旋转
rotateleft = () => {
this.setState({ R: this.state.R - 90 });
// eslint-disable-next-line react/no-string-refs
this.refs.imgstyle.style.transform = `rotate(${this.state.R}deg) scale(${this.state.SS},${this.state.SS})`;
}
// 放大
showbig = () => {
if (this.state.i >= 0) {
this.setState({ S: this.state.S + 1, i: this.state.i + 1, SS: 1 * (this.state.S + 0.1) });
} else {
this.setState({ S: this.state.S - 1, i: this.state.i + 1, SS: 1 / (this.state.S - 1) });
}
// eslint-disable-next-line react/no-string-refs
this.refs.imgstyle.style.transform = `rotate(${this.state.R}deg) scale(${this.state.SS},${this.state.SS})`;
}
// 缩小
showmin = () => {
if (this.state.i <= 0) {
this.setState({ S: this.state.S + 1, i: this.state.i - 1, SS: 1 / (this.state.S + 1) });
} else {
this.setState({ S: this.state.S - 1, i: this.state.i - 1, SS: 1 * (this.state.S - 1) });
}
// eslint-disable-next-line react/no-string-refs
this.refs.imgstyle.style.transform = `rotate(${this.state.R}deg) scale(${this.state.SS},${this.state.SS})`;
}
// 全屏
fullScreen = () => {
this.setState({
imgVisible: true,
});
// this.setState({ fullScreen: !this.state.fullScreen });
}
render() {
const {
toggleshow, showimgs, imgOffsetWidth, imgOffsetHeight, bFullScreen,
} = this.props;
const {
data, f, R, SS, fullScreen,
} = this.state;
const bigWidth = imgOffsetWidth !== undefined && imgOffsetWidth !== null ? imgOffsetWidth : '950';
const bigHeight = imgOffsetHeight !== undefined && imgOffsetHeight !== null ? imgOffsetHeight : '600';
return (
<div>
{
showimgs ?
<div>
<div className="dilong toggleshow" onClick={toggleshow} />
<div
className="bigimg toggleshow"
style={{ width: fullScreen || bFullScreen ? '100vw' : `${bigWidth}px`, height: fullScreen || bFullScreen ? '100vh' : `${bigHeight}px`, overflow: 'hidden' }}
>
<div className="imgheader" style={{ right: bFullScreen ? '0px' : 'none' }}>
<div className="left_top" onClick={this.leftshow}><i className="icon icon-left" style={{ fontSize: '30px' }} /></div>
<div className="right_top" onClick={this.rightshow}><i className="icon icon-right" style={{ fontSize: '30px' }} /></div>
<div className="rotateright_top" onClick={this.rotateright}><i className="icon icon-rturn" style={{ color: '#999', fontSize: '22px' }} /></div>
<div className="rotateleft_top" onClick={this.rotateleft}><i className="icon icon-lturn" style={{ color: '#999', fontSize: '22px' }} /></div>
<div className="full_screen" onClick={this.fullScreen}><img src={FullScreen} alt="" style={{ width: 22, height: 22 }} /></div>
<div className="showbig_top" onClick={this.showbig}><i className="icon icon-imgbig" style={{ color: '#999', fontSize: '22px' }} /></div>
<div className="showmin_top" onClick={this.showmin}><i className="icon icon-imgmin" style={{ color: '#999', fontSize: '22px' }} /></div>
<div className="close_top" onClick={toggleshow} ><i className="icon icon-close" style={{ color: '#999', fontSize: '22px' }} /></div>
<div className="headtitle">预览图片</div>
</div>
<div className="previewBox">
{/* eslint-disable-next-line react/no-string-refs */}
<img src={data[f]} className="imgstyle" style={{ transform: `rotate(${R}deg) scale(${SS},${SS})` }} ref="imgstyle" alt="无图片" onDoubleClick={this.fullScreen} />
<Image
src={data[f]}
style={{ display: 'none' }}
preview={{
visible: this.state.imgVisible,
scaleStep: 0.5,
src: data[f],
onVisibleChange: (value) => {
this.setState({
imgVisible: value,
});
},
}}
/>
</div>
</div>
</div>
: null
}
</div>
);
}
}