UNPKG

lm-noticebar

Version:

* 作者:kanghongyan * 邮箱:khongyan@gmail.com * 版本:**`1.0.3`**

232 lines (176 loc) 9.51 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require('react')); var _propTypes = _interopRequireDefault(require('prop-types')); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _jsxFileName = 'src\\ScrollH.js'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (_typeof(call) === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + _typeof(superClass)); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var gap = 2; // 留出dis间隔 var AutoPlay = function (_React$PureComponent) { _inherits(AutoPlay, _React$PureComponent); function AutoPlay() { _classCallCheck(this, AutoPlay); var _this = _possibleConstructorReturn(this, (AutoPlay.__proto__ || Object.getPrototypeOf(AutoPlay)).call(this)); _this.startPlay = function (dom) { var contWh = dom.getBoundingClientRect().width; // container长度 var domWh = dom.scrollWidth; // 内容长度 /** * * @param scrollWh 需要滚动的距离 * @param duration 延迟时间 * @private */ var _play = function _play(scrollWh, duration) { _this.timer = setTimeout(function () { var currentTransform = window.getComputedStyle(dom).transform || window.getComputedStyle(dom).webkitTransform; if (currentTransform !== 'none' && +currentTransform.split(',')[4].trim() <= -domWh) { // 滚动完毕: 将内容移动到最右侧、立即_play var posScale = _this._getPositionScale(_this.loopIndex, 1); _this._resetStyle(posScale, function () { _play(domWh + contWh * posScale - gap, 60); }); } else { var time = _this.props.speed * scrollWh; _this.setState({ 'WebkitTransition': '-webkit-transform ' + time + 's linear', 'transition': 'transform ' + time + 's linear', 'WebkitTransform': 'translate3d(-' + domWh + 'px, 0, 0)', 'transform': 'translate3d(-' + domWh + 'px, 0, 0)' }, function () { // 每隔500ms检查一次是否已经滚动完毕 _play(domWh, 500); }); } }, duration); }; // _play(domWh, 1000) _this._initStyle(function (posScale) { _play(domWh + contWh * posScale, 1000); }); }; _this._initStyle = function () { var cd = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {}; var startPosition = _this.props.startPosition; if (!startPosition) { return cd(0); } var posScale = _this._percentToDecimal(startPosition, 0); _this._resetStyle(posScale, cd); }; _this._resetStyle = function (scale) { var cd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {}; var contWh = _this.dom.children[0].getBoundingClientRect().width; // container长度 var _gap = scale !== 0 ? gap : 0; _this.setState({ 'WebkitTransition': 'none', 'transition': 'none', 'WebkitTransform': 'translate3d(' + (contWh * scale - _gap) + 'px, 0, 0)', 'transform': 'translate3d(' + (contWh * scale - _gap) + 'px, 0, 0)' }, function () { cd(scale); }); }; _this._getPositionScale = function (loopIndex) { var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var _this$props$loopPosit = _this.props.loopPosition, loopPosition = _this$props$loopPosit === undefined ? [] : _this$props$loopPosit; if (loopPosition && loopPosition.length) { _this.loopIndex = loopIndex + 1 >= loopPosition.length ? 0 : loopIndex + 1; return loopPosition.length > loopIndex ? _this._percentToDecimal(loopPosition[loopIndex], defaultValue) : defaultValue; } return defaultValue; }; _this._percentToDecimal = function (percent) { var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; try { var str = (percent + '').replace("%", ""); return str / 100; } catch (e) { return defaultValue; } }; _this.state = {}; _this.timer = null; _this.loopIndex = 0; return _this; } _createClass(AutoPlay, [{ key: 'componentDidMount', value: function componentDidMount() { var contWh = this.dom.children[0].getBoundingClientRect().width; var scrollWh = this.dom.children[0].scrollWidth; if (Math.ceil(scrollWh) > Math.ceil(contWh)) { this.startPlay(this.dom.children[0]); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { clearTimeout(this.timer); } }, { key: '_cloneChildren', value: function _cloneChildren(children) { var _this2 = this; var _props = this.props, height = _props.height, style = _props.style; var isObjectChildren = Object.prototype.toString.call(children) === '[object Object]'; var childrenArr = isObjectChildren ? [children] : children; return childrenArr.map(function (item, index) { return _react.default.cloneElement(item, { key: index, style: _extends({ height: height }, style, _this2.state, { whiteSpace: 'nowrap' }) }); }); } }, { key: 'render', value: function render() { var _this3 = this; var children = this.props.children; return _react.default.createElement( 'div', _defineProperty({ className: 'm-autoplay-wrap', style: { overflow: 'hidden' }, ref: function ref(ele) { _this3.dom = ele; }, __source: { fileName: _jsxFileName, lineNumber: 172 }, __self: this }, '__self', this), this._cloneChildren(children) ); } }]); return AutoPlay; }(_react.default.PureComponent); AutoPlay.propTypes = { speed: _propTypes.default.number, style: _propTypes.default.object, height: _propTypes.default.number }; AutoPlay.defaultProps = { speed: 1.0 / 20, // 每秒移动多少像素 style: {}, height: 20 }; var _default = AutoPlay; exports.default = _default; module.exports = exports['default'];