index.js 1.01 KB
Newer Older
zhanghaozhe committed
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
import React, { Component } from 'react';
import ReactDom from "react-dom";

import './overlay.scss'

const root = document.body || document.documentElement

const events = ['touchmove', 'mousewheel']

class Overlay extends Component {
    constructor(props) {
        super(props);
        this.overlayBox = null
    }

    preventScroll = e => {
        e.preventDefault()
    }

    componentDidMount() {
        events.forEach(item => {
            this.overlayBox.addEventListener(item, this.preventScroll, {
                passive: false
            })
        })
    }

    componentWillUnmount() {
        events.forEach(item => {
            this.overlayBox.removeEventListener(item, this.preventScroll)
        })
    }

    render() {
        return ReactDom.createPortal(
zhanghaozhe committed
36 37 38 39
            (<div className={'overlay'}
                  ref={el => this.overlayBox = el}
                  style={{top: `${window.scrollY}px`}}
            >{this.props.children}</div>),
zhanghaozhe committed
40 41 42 43 44 45
            root
        )
    }
}

export default Overlay;