UNPKG

yunjiwong-slider

Version:
170 lines (156 loc) 5.7 kB
import _extends from 'babel-runtime/helpers/extends'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import React from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import { stylesToCss } from 'style-utils'; import { currentScrollTop, toArrayChildren, windowHeight } from './utils'; import animType from './anim'; import Tween from 'rc-tween-one/es/Tween'; var BgElement = function (_React$Component) { _inherits(BgElement, _React$Component); function BgElement() { _classCallCheck(this, BgElement); var _this = _possibleConstructorReturn(this, (BgElement.__proto__ || Object.getPrototypeOf(BgElement)).apply(this, arguments)); _this.onScroll = function () { var scrollTop = currentScrollTop(); var domRect = _this.dom.parentNode.getBoundingClientRect(); var offsetTop = domRect.top + scrollTop; var height = Math.max(domRect.height, windowHeight()); var elementShowHeight = scrollTop - offsetTop + height; var scale = elementShowHeight / (height + domRect.height); scale = scale || 0; scale = scale >= 1 ? 1 : scale; _this.tween.frame(scale * _this.scrollParallaxDuration); }; _this.onResize = function () { var domRect = _this.dom.getBoundingClientRect(); var videoDomRect = _this.video.getBoundingClientRect(); _this.videoLoad = true; var scale = void 0; var videoRect = { display: 'block', position: 'relative', top: 0, left: 0 }; if (domRect.width / domRect.height > videoDomRect.width / videoDomRect.height) { scale = domRect.width / videoDomRect.width; videoRect.width = domRect.width; videoRect.height = videoDomRect.height * scale; videoRect.top = -(videoRect.height - domRect.height) / 2; } else { scale = domRect.height / videoDomRect.height; videoRect.height = domRect.height; videoRect.width = videoDomRect.width * scale; videoRect.left = -(videoRect.width - domRect.width) / 2; } Object.keys(videoRect).forEach(function (key) { _this.video.style[key] = stylesToCss(key, videoRect[key]); }); }; _this.videoLoadedData = function () { _this.onResize(); if (window.addEventListener) { window.addEventListener('resize', _this.onResize); } else { window.attachEvent('onresize', _this.onResize); } }; _this.isVideo = toArrayChildren(_this.props.children).filter(function (item) { return item.type === 'video'; }); if (_this.isVideo.length) { delete animType.grid; delete animType.gridBar; } if (_this.props.scrollParallax) { _this.scrollParallaxDuration = _this.props.scrollParallax.duration || 450; } _this.video = null; _this.videoLoad = false; return _this; } _createClass(BgElement, [{ key: 'componentDidMount', value: function componentDidMount() { this.dom = ReactDOM.findDOMNode(this); if (!this.videoLoad) { this.video = ReactDOM.findDOMNode(this.refs.video); if (this.video && this.props.videoResize) { this.video.onloadeddata = this.videoLoadedData; } } if (this.props.scrollParallax) { this.tween = new Tween(this.dom, [_extends({ ease: 'linear' }, this.props.scrollParallax)], { attr: 'style' }); this.tween.frame(0); this.onScroll(); if (window.addEventListener) { window.addEventListener('scroll', this.onScroll); } else { window.attachEvent('onscroll', this.onScroll); } } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (nextProps.show) { if (this.video && this.props.videoResize && this.videoLoad) { this.videoLoadedData(); } if (this.props.scrollParallax) { this.onScroll(); } } else { this.componentWillUnmount(); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (window.addEventListener) { window.removeEventListener('resize', this.onResize); window.removeEventListener('scroll', this.onScroll); } else { window.detachEvent('onresize', this.onResize); window.detachEvent('onscroll', this.onScroll); } } }, { key: 'render', value: function render() { var props = _extends({}, this.props); ['videoResize', 'scrollParallax', 'scrollParallaxDuration', 'show', 'component'].forEach(function (key) { return delete props[key]; }); if (this.isVideo && this.props.videoResize) { props.children = toArrayChildren(props.children).map(function (item) { var ref = item.type === 'video' ? 'video' : null; return React.cloneElement(item, _extends({}, item.props, { ref: ref })); }); } return React.createElement(this.props.component, props); } }]); return BgElement; }(React.Component); export default BgElement; BgElement.propTypes = { className: PropTypes.string, style: PropTypes.object, children: PropTypes.any, component: PropTypes.any, videoResize: PropTypes.bool, scrollParallax: PropTypes.object, show: PropTypes.bool }; BgElement.defaultProps = { component: 'div', videoResize: true }; BgElement.isBannerAnimBgElement = true;