rc-banner-anim
Version:
banner-anim animation component for react
213 lines (171 loc) • 6.86 kB
JavaScript
;
exports.__esModule = true;
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _Tween = require('rc-tween-one/lib/Tween');
var _Tween2 = _interopRequireDefault(_Tween);
var _styleUtils = require('style-utils');
var _utils = require('./utils');
var _anim = require('./anim');
var _anim2 = _interopRequireDefault(_anim);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var BgElement = function (_React$Component) {
(0, _inherits3['default'])(BgElement, _React$Component);
function BgElement(props) {
(0, _classCallCheck3['default'])(this, BgElement);
var _this = (0, _possibleConstructorReturn3['default'])(this, _React$Component.call(this, props));
_this.onScroll = function () {
var scrollTop = (0, _utils.currentScrollTop)();
var domRect = _this.dom.parentNode.getBoundingClientRect();
var offsetTop = domRect.top + scrollTop;
var height = Math.max(domRect.height, (0, _utils.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 () {
if (!_this.props.show) {
return;
}
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] = (0, _styleUtils.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 = (0, _utils.toArrayChildren)(_this.props.children).some(function (item) {
return item.type === 'video';
});
if (_this.isVideo) {
// 如果是 video,删除 grid 系列,位置发生变化,重加载了 video;
delete _anim2['default'].grid;
delete _anim2['default'].gridBar;
}
if (_this.props.scrollParallax) {
_this.scrollParallaxDuration = _this.props.scrollParallax.duration || 450;
}
_this.video = null;
_this.videoLoad = false;
return _this;
}
BgElement.prototype.componentDidMount = function componentDidMount() {
this.dom = _reactDom2['default'].findDOMNode(this);
if (!this.videoLoad) {
if (this.video && this.props.videoResize) {
this.video.onloadeddata = this.videoLoadedData;
}
}
if (this.props.scrollParallax) {
this.tween = new _Tween2['default'](this.dom, [(0, _extends3['default'])({
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);
}
}
};
BgElement.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var _this2 = this;
if (nextProps.show) {
// 取 dom 在 render 之后;
setTimeout(function () {
if (_this2.video && _this2.props.videoResize && _this2.videoLoad) {
_this2.onResize();
}
if (_this2.props.scrollParallax) {
_this2.onScroll();
}
});
}
};
BgElement.prototype.componentWillUnmount = 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);
}
};
BgElement.prototype.render = function render() {
var _this3 = this;
var props = (0, _extends3['default'])({}, this.props);
['videoResize', 'scrollParallax', 'scrollParallaxDuration', 'show', 'component'].forEach(function (key) {
return delete props[key];
});
if (this.isVideo && this.props.videoResize) {
var children = (0, _utils.toArrayChildren)(props.children).map(function (item, i) {
return _react2['default'].cloneElement(item, (0, _extends3['default'])({}, item.props, { key: item.key || 'bg-video-' + i, ref: function ref(c) {
_this3.video = c;
if (typeof item.ref === 'function') {
item.ref(c);
}
}
}));
});
props.children = children.length === 1 ? children[0] : children;
}
return _react2['default'].createElement(this.props.component, props);
};
return BgElement;
}(_react2['default'].Component);
exports['default'] = BgElement;
BgElement.propTypes = {
className: _propTypes2['default'].string,
style: _propTypes2['default'].object,
children: _propTypes2['default'].any,
component: _propTypes2['default'].any,
videoResize: _propTypes2['default'].bool,
scrollParallax: _propTypes2['default'].object,
show: _propTypes2['default'].bool
};
BgElement.defaultProps = {
component: 'div',
videoResize: true
};
BgElement.isBannerAnimBgElement = true;
module.exports = exports['default'];