图片轮播预览

效果

实现

js代码:

import React, { Component } from 'react';
import Carousel from 'nuka-carousel';
import './largePreview.scss';

const Index = props => {
    return props.visible ?  : null;
};

/**
 * @param {Array} pics 图片数组 [ {img: ''}, {img: ''} ]
 * @param {boolean} visible 是否展示大图预览
 * @param {number} currentIndex 当前是第几张图片,数组下标
 * @param {function} close 关闭当前图片预览
 */
class LargePriview extends Component {
    constructor(props) {
        super(props);
        this.state = {
            screenHeight: '100%',
            currentIndex: 1,
            toggleBarHeight: 0,
            pics: []
        };
    }
    componentWillMount = () => {
        if (navigator.userAgent.indexOf('cheshangtong') > -1) {
            this.setState({
                pics: JSON.parse(WBCST.getParamFromUrl('pic')),
                currentIndex: Number(WBCST.getParamFromUrl('index'))
            });
        } else {
            this.setState({
                pics: this.props.location.param.pic,
                currentIndex: this.props.location.param.index
            });
        }

        WBCST.toggleTitlePanel(
            {
                hideNavBar: true,
                bounces: 0,
                statusBarStyle: 'light'
            },
            data => {
                this.setState({
                    toggleBarHeight: data.toggleBarHeight
                });
            }
        );
    };
    componentDidMount() {
        const screenHeight = (document && document.body.clientHeight) || '100%';
        this.setState({
            screenHeight
        });
    }
    screenHeight = () => {
        const screenHeight = (document && document.body.clientHeight) || '100%';
        let clientWidth = document.querySelector('body').offsetWidth;
        const { toggleBarHeight } = this.state;
        let height =
            toggleBarHeight > 50 ? toggleBarHeight : toggleBarHeight + (45 / 375) * clientWidth;
        return screenHeight - height;
    };
    handleImgClick(show, index) {
        this.setState({
            currentIndex: index
        });
    }
    handleTop = () => {
        const { toggleBarHeight } = this.state;
        let clientWidth = document.querySelector('body').offsetWidth;
        let top =
            toggleBarHeight > 50 ? toggleBarHeight - (45 / 375) * clientWidth : toggleBarHeight;
        return top;
    };
    render() {
        const { screenHeight, currentIndex, pics } = this.state;

        return (
            
{ if (navigator.userAgent.indexOf('cheshangtong') > -1) { WBCST.closeCurrentPage(); } else { this.props.history.go(-1); } }}>
{currentIndex + 1}/{pics.length}
{ this.handleImgClick(true, index); }}> {pics.map((imgItem, imgIndex) => { return (
); })}
); } } export default LargePriview;

css代码:

$baseFontSize:32px !default;
//pixels to rems
@function pxToRem($px){
    @return $px / $baseFontSize * 1rem;
}

.imgs-large-wrapper {
    height: 100%;
    width: 100%;
    background: #000000;
    overflow: hidden;
    .pre-status{
        background: #000000;
    }
    .imgs-top-float {
        width: 100%;
        height: pxToRem(88px);
        display: flex;
        align-items: center;
        padding: 0 pxToRem(30px);
        flex-direction: row;
        justify-content: space-between;
        .close{
            background: url('./../../img/close.png');
            background-repeat: no-repeat;
            background-size: 100%;
            width: pxToRem(60px);
            height: pxToRem(60px);
        }
        .imgs-index-style {
            width: 100%;
            font-size: pxToRem(34px);
            font-family: PingFang-SC-Bold,PingFang-SC;
            color:#FFFFFF;
            text-align: center;
        }
        .right{
            width: pxToRem(60px);
        }
    }
    .imgs-carousel-box {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        img {
            width: 100%;
        }
    }
}

你可能感兴趣的:(react.js,前端,sass,html5)