In a recent project of mine I decided to add a ‘lightbox’ that allows users to zoom in on and click through images, and rather than creating all of that code from scratch, I turned to React Image Lightbox.

在我最近的一个项目中,我决定添加一个“灯箱”,该灯箱允许用户放大并单击图像,而不是从头开始创建所有代码,我转向了React Image Lightbox 。

On its Readme, RIL is described as a ‘flexible lightbox component for displaying images in a React project’. It comes prepackaged with all the standard features of a lightbox, and it’s also mobile-friendly with a pinch-to-zoom feature.

在其自述文件中,RIL被描述为“用于在React项目中显示图像的灵活灯箱组件”。 它预先包装了灯箱的所有标准功能,并且具有捏合缩放功能,便于移动使用。

In this tutorial I’ll go through the process of installing React Image Lightbox and show you step-by-step how to build your lightbox component.

在本教程中,我将介绍安装React Image Lightbox的过程,并逐步向您展示如何构建灯箱组件。

将React Image Lightbox添加到您的项目中 (Adding React Image Lightbox to Your Project)

Installing RIL is straightforward. Simply use npm:

安装RIL非常简单。 只需使用npm:

$ npm install react-image-lightbox

Now that you’ve got RIL installed we can start creating our lightbox. First you need to add the custom CSS that comes with RIL to your project. Import it into the top of your App.js file:

现在您已经安装了RIL,我们可以开始创建灯箱了。 首先,您需要将RIL附带的自定义CSS添加到您的项目中。 将其导入到App.js文件的顶部:

// in App.jsimport 'react-image-lightbox/style.css';

生成灯箱文件 (Building the Lightbox File)

Now we’ll set up the file that holds our lightbox — my file is called Photos.js.

现在,我们将设置保存灯箱的文件-我的文件称为Photos.js。

First, we’ll import the Lightbox component from ‘react-image-lightbox’ and add the URLs of our images. To add the URLs create a const (outside of the class) called ‘images’ that is equal to an array of your image URLs:

首先,我们将从'react-image-lightbox'导入Lightbox组件,然后添加图像的URL。 要添加URL,请创建一个名为“ images”的const(在类外部),该const等于图像URL的数组:

Next we’ll add the constructor for our class. We’ll have two things in our state: ‘photoIndex’ and ‘isOpen’.

接下来,我们将为我们的类添加构造函数。 我们的状态将有两件事:“ photoIndex”和“ isOpen”。

Now that our state is set up, we’ll create two constants that match our state inside of the render block:

现在,我们已经建立了状态,我们将在render块中创建两个与我们的状态相匹配的常量:

Next we’ll fill out the return block for our page. My page consists of a heading and three rows of pictures.

接下来,我们将为页面填写return块。 我的页面包含一个标题和三行图片。

First, you’ll notice that on each of my images I have an ‘onClick’ function. This opens the lightbox and sets the starting image to whichever image was clicked.

首先,您会注意到,在我的每张图像上,我都有一个“ onClick”功能。 这将打开灯箱,并将起始图像设置为单击的图像。

My component is styled using a custom Bootstrap class to create a 3x3 grid. I also have 9 pictures in my ‘images’ array, one for each square on the grid, so you’ll have to update the photoIndex in your onClick function to match which image in the array you want to open on click.

我的组件使用自定义的Bootstrap类设置样式,以创建3x3网格。 我的“图像”数组中也有9张图片,网格上的每个正方形一张,因此您必须更新onClick函数中的photoIndex以匹配您要在单击时打开的数组中的哪张图像。

Second, you’ll notice the Javascript that makes RIL function; this is placed just below the closing div tag in the return statement. This code is straight from the RIL example page, and it’s what makes the lightbox function. For my project I didn’t have to modify it at all to get the functionality I was looking for, which is what makes RIL so great.

其次,您会注意到使RIL起作用的Javascript。 它被放置在return语句中的div标记的正下方。 这段代码直接来自RIL示例页面 ,这就是使灯箱起作用的原因。 对于我的项目,我无需进行任何修改即可获得我一直在寻找的功能,这就是RIL如此出色的原因。

The resulting lightbox
产生的灯箱

After all that your lightbox should be up and running! You can sift through photos using either your keypad or the arrows on the lightbox. To close the lightbox just click off of the photo.

毕竟,您的灯箱应该已经启动并运行了! 您可以使用键盘或灯箱上的箭头浏览照片。 要关闭灯箱,只需点击图片旁边的即可。

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/swlh/how-to-use-react-image-lightbox-b29b55f3da62


http://www.taodudu.cc/news/show-5347233.html

相关文章:

  • itchat库 账号安全无法登陆网页微信
  • “凤巢”将全面接管百度搜索推广将更利于SEO
  • “阿拉丁”、“凤巢”以及热门的“移动搜索”成为重点,今年,将推动市场占有率第一的百度继续变革
  • 凤巢能否成功关键还看用户体验
  • 百度凤巢算法面经
  • 凤巢系统_2
  • 凤巢系统_1
  • 百度凤巢 | MTMS多任务多场景统一精排模型
  • 我对凤巢的浅显认知
  • 凤巢面试题
  • 国际餐饮巨头百胜集团提交NFT及元宇由相关商标申请】
  • 【安徽老乡鸡】签约企企通,打造国内餐饮行业采购数字化标杆
  • TVM虚拟机配置
  • 复盘2020美股IPO:累计融资额破纪录,估值上演冰与火之歌
  • 计算机网络教研室活动总结,2019-2020学年第一学期网络工程教研室磨课活动总结...
  • 百胜餐饮集团
  • 航天器系统方案设计的作用
  • 起源太空NEO-01航天器搭载长征六号运载火箭成功发射
  • 航天器空间环境效应数值模拟方法
  • 航天器系统设计的程序是什么?
  • 华为OD机试 - 航天器(Python) | 机试题+算法思路+考点+代码解析 【2023】
  • 航天器编队飞行(2):通用的相对轨道描述
  • 【华为OD机试模拟题】用 C++ 实现 - 航天器(2023.Q1)
  • 【西安安泰】线束测试仪基于航天器低频电缆测试中的应用
  • 华为OD机试题 - 航天器(JavaScript)| 含思路
  • matlab强化学习DDPG算法改编/菜鸟理解2——航天器三轴姿态稳定器学习算例
  • 航天器 3-D 任务规划器附matlab代码
  • 题目0013-航天器
  • 自适应反步控制matlab,刚体航天器的反步自适应滑模大角度姿态机动控制方法与流程...
  • 航天器动力学建模笔记

如何使用React Image Lightbox相关推荐

  1. React Image Lightbox

    React Image Lightbox 基于 React 的图片预览组件(支持选择上一张下一张,支持放大缩小等操作) A flexible lightbox component for displa ...

  2. 13 种用于网站设计和开发的 React 图库

    原文链接:https://tzy1997.com/articles/rea323su/ 在本文中,我们将介绍13种用于网站设计和开发的 React 图片库示例. React & CSS Gri ...

  3. 使用Spring Boot,JHipster和React构建照片库PWA

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 从本质上 ...

  4. React.js入门基础一

    React A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES 下面是为什么在你选择之前需要再考虑一下:(摘录) 一开始 React 会极大地减慢你的开 ...

  5. react中使用photoswipe

    react中使用photoswipe 安装photoswipe npm install photoswipe 定义一个LightBox组件 import React from 'react' impo ...

  6. react hooks_使用Spring Boot,JHipster和React构建照片库PWA

    react hooks "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多 ...

  7. react非常适合入门者学习使用的后台管理框架

    项目简介 该项目提供一个非常简洁的后台管理ui界面,非常适合初学者学习使用.项目结构: 项目地址:GitHub项目地址 技术栈 - react - antd - react-router-dom - ...

  8. 在 react 里使用 antd

    在 react 里使用 antd 在 powershell 里npm i antd 引入方式: import '../node_modules/antd/dist/antd.css'

  9. 在React Hook里使用history.push跳转

    在React Hook里使用history.push跳转 react hook里用不了this.props.history的解决方法 首先引入 import { useHistory } from ' ...

最新文章

  1. python list.pop 的方法的使用
  2. mybatis-plus 多列映射成数组_JavaScript 为什么需要类数组
  3. 你是一个职业的页面重构工作者吗?
  4. cmake找不到opencv解决
  5. JSF or PHP or ADF?!! :(
  6. 六十九、Springboot整合JDBC,连接Mysql
  7. opentld matlab视频,计算机视觉中的相关算法的源代码
  8. 项目微管理 - 总结也是新的开始
  9. 一步一步写算法(之图的保存)
  10. 专用字典_字典生成工具_crunch
  11. Xutils-Android中数据存储和网络传输的框架
  12. 大学毕业后拉开差距的真正原因--写给将要毕业的自己
  13. 数码相机冲洗照片的数据
  14. 深圳房价链家数据分析
  15. 吴恩达NLP课程资料
  16. python控制软件自动化测试,资讯详情-用python来使用Airtest 自动化工具-柠檬班-自动化测试-软件测试培训-自学官网...
  17. python3.6 base64转图片及图片转base
  18. 什么是数据库连接池?数据库连接池的机制?
  19. 蓝懿ios网络请求bock
  20. 液晶屏背光板的分类及知识点

热门文章

  1. php widgets,Yii框架小部件(Widgets)用法实例详解
  2. 三.数据库基础-Oracle入门到精通学习笔记
  3. STM32必备知识点(面试和工作用的到)
  4. 数据本质价值的一些思考
  5. 吴忠军 价值中国网主页
  6. JSP软件(包括jsp、java、SQL)
  7. 可行性分析报告-机房收费系统
  8. python 3.8.2(32-bit)安装教程_Python 3.8.2详细图文安装教程(附安装包)
  9. 计算机网络统考历年卷,201710月全国自学考试04741《计算机网络原理》历年真题试题(卷)与答案解析.doc...
  10. 14- ESP8266通过mqtt服务器接收控制指令