Skip to content

小程序 ​

本节目标:理解“小程序”的核心思路,并能把它用于实际开发或面试表达。 判断是否是滑动,根据touchend时的坐标相对于开始时的坐标变化,并判断滑动方向

 swiperDirection = Math.abs(startX - endX) > Math.abs(startY - endY)
            ? (startX - endX > 0 ? 'left' : 'right')
            : (startY - endY > 0 ? 'top' : 'bottom')
import React, { Component } from 'react';
import styles from './index.less';
export default class index extends Component {
    state = {
        startX: 0,
        startY: 0,
        left: 0,
        imgNum: 0,//

图片数量

        currImgIndex: 0,
    }
    componentDidMount() {
        const { imgs } = this.props;
        this.swiper.addEventListener('touchstart', this.handleTouchStart, false);
        this.swiper.addEventListener('touchend', this.handleTouchEnd, false);
        this.setState({ imgNum: imgs.length });
    }
    handleTouchStart = (evt) => {
        // evt.preventDefault();
        if (evt.touches.length > 1) return;
        this.setState({
            startX: evt.touches[0].pageX,
            startY: evt.touches[0].pageY,
        })
    }
    handleTouchEnd = (evt) => {
        // evt.preventDefault();
        if (evt.changedTouches.length > 1) return;
        let currImgIndex = parseInt(evt.target.id) || 0;
        const { startX, startY, imgNum, left: rawLeft } = this.state;
        let left = 0;
        const endX = evt.changedTouches[0].pageX;
        const endY = evt.changedTouches[0].pageY;
        console.log('touchend', imgNum, startX, endX, startY, endY, rawLeft);
        const swiperDirection = Math.abs(startX - endX) > Math.abs(startY - endY)
            ? (startX - endX > 0 ? 'left' : 'right')
            : (startY - endY > 0 ? 'top' : 'bottom')
        if (swiperDirection === 'left') {
            //

最后一张左拉的情况

            if (Math.abs(rawLeft) >= 100 * (imgNum - 1)) {
                left = rawLeft;
                return;
            }
            left = rawLeft - 100;
            currImgIndex++;
        }
        if (swiperDirection === 'right') {
            //

第一张往右拉的情况

            if (rawLeft === 0) {
                left = 0;
                return;
            }
            left = rawLeft + 100;
            currImgIndex--;
        }
        this.setState({ left, currImgIndex });
    }
    render() {
        const { imgs, handleClick } = this.props;
        const { left, currImgIndex } = this.state;
        return (
            <div className={styles.imgsViewer} onClick={handleClick}>
                <div
                    style={{
                        position: 'absolute',
                        top: '50%',
                        left: `${left}vw`,
                        transition: 'all 0.5s ease-out 0s',
                    }}
                    ref={node => this.swiper = node}
                    className={styles.imgsWrap}
                >
                    {imgs.map((imgUrl, index) => {
                        return (
                            <div key={index} className={styles.imgWrap}>
                                <img id={index} src={imgUrl} />
                            </div>
                        )
                    })}
                </div>
                <div
                    style={{
                        position: 'fixed',
                        bottom: '20px',
                        left: '50vw',
                        transform: 'translate(-50%)',
                        padding: 5,
                        zIndex: 99,
                    }}
                >
                    {imgs.map((_, index) => (
                        <div
                            key={index}
                            style={{
                                display: 'inline-block',
                                width: 10,
                                height: 10,
                                background: '#ddd',
                                borderRadius: '50%',
                                marginLeft: 10,
                                boxShadow: currImgIndex === index ? '0 0 4px 2px #fff' : ''
                            }}></div>
                    ))}
                </div>
            </div >
        )
    }
}
.imgsViewer {
    width          : 100%;
    height         : 100vh;
    background     : #000;
    overflow       : hidden;
    position       : relative;
    display        : flex;
    justify-content: center;
    align-items    : center;
    .imgsWrap {
        transform: translateY(-50%);
        overflow : scroll;
        display  : flex;
    }
    .imgWrap {
        width  : 100vw;
        display: inline-block;
        >img {
            display   : block;
            max-width : 100%;
            max-height: 100%;
        }
    }
}