UNPKG

movm

Version:

一个基于antdsegin的ui组件

161 lines (129 loc) 6.24 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import React from "react"; import { LeftOutlined, RightOutlined } from "@ant-design/icons"; import { useEffect, useRef, useState } from "react"; import "./index.css"; var LargerImg = function LargerImg(_ref) { var showItem = _ref.showItem, IsClose = _ref.IsClose, imgUrl = _ref.imgUrl; var close = function close() { IsClose(); }; var _useState = useState([]), _useState2 = _slicedToArray(_useState, 2), imgUrlList = _useState2[0], setImgUrlList = _useState2[1]; var _useState3 = useState(0), _useState4 = _slicedToArray(_useState3, 2), count = _useState4[0], setCount = _useState4[1]; var onLeft = function onLeft(e) { e.stopPropagation(); if (count > 0) { setCount(count - 1); } }; var onRight = function onRight(e) { e.stopPropagation(); if (count < imgUrlList.length - 1) { setCount(count + 1); } }; useEffect(function () { // 通过传来的图片地址在数组里做对比获取索引 并复制给count 以保证点开图片时预览的是当前点击的图片 var index = imgUrlList.indexOf(showItem); setImgUrlList(_toConsumableArray(imgUrl)); setCount(index); // console.log('showItem', ) return function () { setCount(0); }; }, [showItem, imgUrl]); /* 拖动图片 */ var handleDrag = function handleDrag(e) { var _zoomImg$current = zoomImg.current, offsetLeft = _zoomImg$current.offsetLeft, offsetTop = _zoomImg$current.offsetTop, onmousemove = _zoomImg$current.onmousemove, clientWidth = _zoomImg$current.clientWidth, style = _zoomImg$current.style; var x = e.clientX - offsetLeft; var y = e.clientY - offsetTop; document['onmousemove'] = function (e) { style.left = e.clientX - x + 'px'; style.top = e.clientY - y + 'px'; }; document.onmouseup = function () { document['onmousemove'] = null; }; }; /* 鼠标滚动放大 */ var zoomImg = useRef(null); var handleZoom = function handleZoom(e) { var _zoomImg$current2 = zoomImg.current, clientWidth = _zoomImg$current2.clientWidth, style = _zoomImg$current2.style; if (e.nativeEvent.deltaY <= 0 && clientWidth < 2000) { style.width = clientWidth + 50 + 'px'; //图片宽度每次增加50 style.left = style.left - 1 % +'px'; //随便设置 不重要 } else if (e.nativeEvent.deltaY > 0) { if (clientWidth > 400) { style.width = clientWidth - 50 + 'px'; //图片宽度 style.left = style.left + 1 % +'px'; } // else { // style.left = 0 // } } }; return /*#__PURE__*/React.createElement(React.Fragment, null, showItem && /*#__PURE__*/React.createElement("div", { className: "showBigImg", onClick: close }, /*#__PURE__*/React.createElement("img", { ref: zoomImg, className: "zoomImg", onDragStart: function onDragStart(e) { e.preventDefault(); return false; }, onMouseDown: function onMouseDown(e) { e.persist(); handleDrag(e); }, onWheel: function onWheel(e) { handleZoom(e); }, onClick: function onClick(e) { e.stopPropagation(); }, src: imgUrlList[count], alt: "" }), imgUrlList.length > 1 && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", { className: "lengthTxt" }, "\u7B2C ", count + 1, " \u5F20 , \u5171 ", imgUrlList.length, " \u5F20"), /*#__PURE__*/React.createElement("span", { className: "onLeft", style: { cursor: "".concat(count === 0 ? 'no-drop' : 'pointer') }, onClick: function onClick(e) { onLeft(e); } }, /*#__PURE__*/React.createElement(LeftOutlined, null)), /*#__PURE__*/React.createElement("span", { className: "onRight", style: { cursor: "".concat(count === imgUrlList.length - 1 ? 'no-drop' : 'pointer') }, onClick: function onClick(e) { onRight(e); } }, /*#__PURE__*/React.createElement(RightOutlined, null))))); }; export default LargerImg;