react中使用photoswipe

该博客介绍了如何在React应用中集成Photoswipe库,创建一个轻量级的图片预览组件。首先通过npm安装Photoswipe及相关样式文件,然后定义一个LightBox组件,利用组件生命周期方法初始化相册。当接收到新的图片路径时,组件会自动更新图片。最后展示如何在其他组件中导入并使用LightBox组件,实现图片预览功能。
摘要由CSDN通过智能技术生成

react中使用photoswipe

安装photoswipe

npm install photoswipe

定义一个LightBox组件

import React from 'react'
import PhotoSwipe from 'photoswipe'
import 'photoswipe/dist/photoswipe-ui-default'
import 'photoswipe/dist/photoswipe.css'
import 'photoswipe/dist/default-skin/default-skin.css'
import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default'


class LightBox extends React.Component {
    constructor(props) {
        // console.log(props)
        super(props);
        this.state = {
        //传过来的图片路径和大小
            imgSrc: props.imgSrc,
            width: props.width,
            height: props.height
        };
    }

    componentDidMount() {
        this.init()
    }
    componentWillReceiveProps(props) {
        if (props.imgSrc != this.state.imgSrc) {
            this.setState({
                'imgSrc': props.imgSrc,
                'width': props.width,
                'height': props.height
            }, () => {
                this.init()
            })
        }
    }
    init() {
        const { imgSrc, width, height } = this.state
        let openPhotoSwipe = function () {
            let pswpElement = document.querySelectorAll('.pswp')[0];
            let items = [
                {
                    src: imgSrc,
                    //这里的宽高必须
                    w: width,
                    h: height,
                },
            ];

            // define options (if needed)
            let options = {
                // history & focus options are disabled on CodePen        
                history: false,
                focus: false,
                showAnimationDuration: 0,
                hideAnimationDuration: 0
            };
            let gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
            gallery.init();
        };
        // openPhotoSwipe()
        document.getElementById('img-container').onclick = openPhotoSwipe;
    }
    render() {
        return (
            <div className="light-box">
            {/* 以下格式是必须的 */}
                <div className="pswp" tabIndex="-1" role="dialog" aria-hidden="true">
                    <div className="pswp__bg"></div>
                    <div className="pswp__scroll-wrap">
                        <div className="pswp__container">
                            <div className="pswp__item"></div>
                            <div className="pswp__item"></div>
                            <div className="pswp__item"></div>
                        </div>
                        <div className="pswp__ui pswp__ui--hidden">

                            <div className="pswp__top-bar" style={{ bottom: '0', top: 'inherit', maxWidth: '1600px' }}>
                                <div className="pswp__counter"></div>
                                <button className="pswp__button pswp__button--close" title="Close (Esc)"></button>
                                <button className="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                                <button className="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                                <div className="pswp__preloader">
                                    <div className="pswp__preloader__icn">
                                        <div className="pswp__preloader__cut">
                                            <div className="pswp__preloader__donut"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <button className="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
                            </button>
                            <button className="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
                            </button>
                            <div className="pswp__caption">
                                <div className="pswp__caption__center"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        )
    }
}
export default LightBox

导入组件并使用

import LightBox from '../lightBox/LightBox'

//传入图片路径和宽高
 <LightBox imgSrc={imgSrc} width={width} height={height} />
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值