@@ -838,30 +838,39 @@ export default class MarkdownPreview extends React.Component {
838
838
const imgList = item . contentWindow . document . body . querySelectorAll ( 'img' )
839
839
for ( const img of imgList ) {
840
840
img . onclick = ( ) => {
841
- const magnification = document . body . clientWidth / img . width
842
- const top = document . body . clientHeight / 2 - img . height * magnification / 2
843
- const animationSpeed = 300
841
+ const widthMagnification = document . body . clientWidth / img . width
842
+ const heightMagnification = document . body . clientHeight / img . height
843
+ const baseOnWidth = widthMagnification < heightMagnification
844
+ const magnification = baseOnWidth ? widthMagnification : heightMagnification
845
+
846
+ const zoomImgWidth = img . width * magnification
847
+ const zoomImgHeight = img . height * magnification
848
+ const zoomInImgTop = document . body . clientHeight / 2 - zoomImgHeight / 2
849
+ const zoomInImgLeft = document . body . clientWidth / 2 - zoomImgWidth / 2
850
+ const originalImgTop = img . y + rect . top
851
+ const originalImgLeft = img . x + rect . left
844
852
const originalImgRect = {
845
- top : `${ img . y + rect . top } px` ,
846
- left : `${ img . x + rect . left } px` ,
853
+ top : `${ originalImgTop } px` ,
854
+ left : `${ originalImgLeft } px` ,
847
855
width : `${ img . width } px` ,
848
856
height : `${ img . height } px`
849
857
}
850
858
const zoomInImgRect = {
851
- top : `${ top } px` ,
852
- left : 0 ,
853
- width : `100% ` ,
854
- height : `${ img . height * magnification } px`
859
+ top : `${ baseOnWidth ? zoomInImgTop : 0 } px` ,
860
+ left : ` ${ baseOnWidth ? 0 : zoomInImgLeft } px` ,
861
+ width : `${ zoomImgWidth } px ` ,
862
+ height : `${ zoomImgHeight } px`
855
863
}
864
+ const animationSpeed = 300
856
865
857
866
const zoomImg = document . createElement ( 'img' )
858
867
zoomImg . src = img . src
859
868
zoomImg . style = `
860
869
position: absolute;
861
- top: ${ top } px;
862
- left: 0 ;
863
- width: 100% ;
864
- height: ${ img . height * magnification } px;
870
+ top: ${ baseOnWidth ? zoomInImgTop : 0 } px;
871
+ left: ${ baseOnWidth ? 0 : zoomInImgLeft } px ;
872
+ width: ${ zoomImgWidth } ;
873
+ height: ${ zoomImgHeight } px;
865
874
`
866
875
zoomImg . animate ( [
867
876
originalImgRect ,
@@ -882,8 +891,8 @@ export default class MarkdownPreview extends React.Component {
882
891
overlay . onclick = ( ) => {
883
892
zoomImg . style = `
884
893
position: absolute;
885
- top: ${ img . y + rect . top } px;
886
- left: ${ img . x + rect . left } px;
894
+ top: ${ originalImgTop } px;
895
+ left: ${ originalImgLeft } px;
887
896
width: ${ img . width } px;
888
897
height: ${ img . height } px;
889
898
`
0 commit comments