Skip to content

Commit 49603e0

Browse files
committed
feat(ImgViewer): add setScrollbar effect
set body paddingRight as scrollbarWidth when body is overflowing
1 parent f7e1d15 commit 49603e0

File tree

5 files changed

+88
-14
lines changed

5 files changed

+88
-14
lines changed

.babelrc.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
console.log('babelrc-js');
1+
// console.log('babelrc-js');
22
module.exports = {
33
"presets": [
44
[
@@ -10,15 +10,19 @@ module.exports = {
1010
"@babel/preset-react"
1111
],
1212
"plugins": [
13-
['@babel/proposal-class-properties', { loose: true }],
14-
'@babel/plugin-external-helpers',
13+
['@babel/proposal-class-properties', { loose: true }]
1514
// "external-helpers"
1615
],
1716
"env": {
1817
"development": {
1918
"plugins": [
2019
"react-hot-loader/babel"
2120
]
21+
},
22+
"production": {
23+
"plugins": [
24+
'@babel/plugin-external-helpers',
25+
],
2226
}
2327
}
2428
}

develop/App.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
import React from 'react'
22
import { hot } from 'react-hot-loader'
3+
import { Image } from '../src'
4+
import './style'
5+
const Images = (function() {
6+
const ret = []
7+
for(let i = 1; i <= 20; i++) {
8+
ret.push(require(`../doc/assets/${i}.jpg`))
9+
}
10+
window.Images = ret
11+
return ret
12+
})()
313

414
const App = hot(module)(() => (
515
<div>
6-
development
16+
<div className='container' style={{ display: 'flex', flexWrap: 'wrap' }}>
17+
{Images.map(each => (
18+
<Image
19+
group="multi"
20+
key={each}
21+
src={each}
22+
width={120}
23+
height={120}
24+
style={{ margin: '35px' }}
25+
/>
26+
))}
27+
</div>
728
</div>
829
))
930

develop/style.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
body {
2+
padding: 20px;
3+
margin: 0px;
4+
background: red;
5+
}
6+
7+
.container {
8+
width: 600px;
9+
margin: 0 auto;
10+
}

src/lib/ImgPreview/ImgPreview.jsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import LoadingIcon from '../LoadingIcon'
55
import ErrorIcon from '../ErrorIcon'
66
import Image from '../Image'
77
import { createObserver } from '../Image/observer'
8+
import { setScrollbar, resetScrollbar } from '../../utils'
89
// import Store from './imgViewStore'
910
function find(list, arg) {
1011
return list.findIndex(each => each === arg)
@@ -103,6 +104,20 @@ export default class ImgPpreview extends React.PureComponent {
103104
}
104105
}
105106

107+
addEffect() {
108+
setScrollbar()
109+
window.addEventListener('keyup', this.windowKeyUpHandle)
110+
document.body.addEventListener('click', this.hideHandle)
111+
document.body.style.overflow = 'hidden'
112+
}
113+
114+
removeEffect() {
115+
resetScrollbar()
116+
window.removeEventListener('keyup', this.windowKeyUpHandle)
117+
document.body.removeEventListener('click', this.hideHandle)
118+
document.body.style.overflow = ''
119+
}
120+
106121
/**
107122
* 打开图片浏览器
108123
* 绑定keyup与click事件监听关闭
@@ -112,10 +127,7 @@ export default class ImgPpreview extends React.PureComponent {
112127
this.setState({
113128
open: true
114129
})
115-
const { body } = document
116-
window.addEventListener('keyup', this.windowKeyUpHandle)
117-
body.style.overflow = 'hidden'
118-
body.addEventListener('click', this.hideHandle)
130+
this.addEffect()
119131
}
120132

121133
/**
@@ -126,10 +138,7 @@ export default class ImgPpreview extends React.PureComponent {
126138
this.setState({
127139
open: false
128140
})
129-
const { body } = document
130-
window.removeEventListener('keyup', this.windowKeyUpHandle)
131-
body.style.overflow = ''
132-
body.removeEventListener('click', this.hideHandle)
141+
this.removeEffect()
133142
}
134143

135144
change = current => {
@@ -402,8 +411,8 @@ export default class ImgPpreview extends React.PureComponent {
402411

403412
_renderFooter() {
404413
const {
405-
state, prev, next, rotateFnc
406-
} = this
414+
state, prev, next, rotateFnc
415+
} = this
407416
const { images, current } = state
408417
return (
409418
<>

src/utils/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { documentElement } = document
2+
let scrollBarWidth = 0
3+
4+
export function getScrollbarSize() {
5+
if(scrollBarWidth) {
6+
return scrollBarWidth
7+
}
8+
scrollBarWidth = window.innerWidth - documentElement.clientWidth
9+
return scrollBarWidth
10+
}
11+
export function getDomStyle(dom, name) {
12+
return window.getComputedStyle(dom)[name]
13+
}
14+
15+
function getBodyOriginPaddingRight() {
16+
return parseInt(getDomStyle(document.body, 'paddingRight'), 10)
17+
}
18+
19+
export function setScrollbar() {
20+
const { innerWidth } = window
21+
const { clientWidth, offsetWidth } = documentElement
22+
if(clientWidth < innerWidth && innerWidth > offsetWidth) {
23+
document.body.style.paddingRight = `${getScrollbarSize()
24+
+ getBodyOriginPaddingRight()}px`
25+
}
26+
}
27+
28+
export function resetScrollbar() {
29+
document.body.style.paddingRight = ''
30+
}

0 commit comments

Comments
 (0)