实现效果
组件源码
SlideContainer.js
import React, {useRef, useState} from "react";
export const SlideContainer = (props) => {
const childrenArray = React.Children.toArray(props.children);
const [left, setLeft] = useState(0);
const [leftOriginal, setleftOriginal] = useState(0);
const [run, setRun] = useState(false);
const touchX = useRef(null);
const mouseDown = (event) => {
const mouseMove = (event) => {
const move = x - touchX.current;
if (leftOriginal + move <= -1 * 50 * (window.innerWidth / 100)) {
if (leftOriginal + move >= 0) {
setLeft(leftOriginal + move);
const mouseUp = (event) => {
if (left < -1 * 25 * (window.innerWidth / 100)) {
setLeft(-1 * 50 * (window.innerWidth / 100));
<div style={{width: '100vw', height: '100px', position: 'relative', overflow: 'hidden'}}
onMouseDown={mouseDown} onMouseMove={mouseMove} onMouseUp={mouseUp}
<div style={{width: '150vw', position: 'absolute', display: 'flex', left: left + 'px'}}>
<div style={{width: '100vw'}}>{childrenArray[0]}div>
<div style={{width: '50vw'}}>{childrenArray[1]}div>
使用样例
import {SlideContainer} from "./component/slide-view/slide-view";
<button type='button' style={{width:'25vw'}}>预览button>
<button type='button' style={{width:'25vw'}}>删除button>