Home Web Front-end HTML Tutorial Complete an image carousel component using React

Complete an image carousel component using React

Mar 08, 2018 pm 05:36 PM
react picture components

This time I will bring you how to use React to complete a picture carousel component. What are the notes of using React to complete a picture carousel component? The following is a practical case. Get up and take a look.

Complete an image carousel component using React

Use React to implement the above functions. First, divide the components:

1

2

<SlideMS> <SlideM/>

 <SlideS/><SlideMS/>

Copy after login

The component SlideM represents the middle picture component, which always maintains a state: currentMIndex, which is the index value of the current Chinese image.
The component SlideS represents the small picture component and needs to maintain two states: currentSIndex and currentSPage, that is, the index value of the current small picture image and the current page number of the small picture;

The outer component SlideMS accepts several Parameters as props:

1

2

3

<SlideMS     mediumImageArr={mediumImageArr}    eachMediumImgWidth = {616}    smallImageArr={smallImageArr}     eachSmallImgWidth = {82}/>, document.getElementById(&#39;js-img-slide&#39;));

SlideM、SlideS再从SlideMS中获取参数进行内部渲染。

<SlideM     mediumImageArr={mediumImageArr}    currentMIndex={currentMIndex}    eachMediumImgWidth = {eachMediumImgWidth}    callback={(index)=>{self.setSmallIndex(index)}} /><SlideS      smallImageArr={smallImageArr}      currentSIndex={currentSIndex}      eachSmallImgWidth={eachSmallImgWidth}      eachMediumImgWidth = {eachMediumImgWidth}      callback={(index)=>{self.setMediumIndex(index)}}/>

Copy after login

When the switch button of the middle image is clicked, the image offset is calculated, and then the new currentMIndex is passed to the parent component. The parent component setState notifies the two child components of the currentMIndex. Changes have occurred;

There are many calculations involved in the small picture component, and these can be divided into methods, such as: calculating how many small pictures there are on a page based on the given width of the middle picture, calculating the total number of small picture pages, Determine which page the thumbnail is on based on the given index value.

When you click the left and right arrows of the small picture component, calculate the offset of the picture (first calculate how many small pictures there are in a medium picture), and then setState modifies the value of the currentSPage of the small picture. The parent component setState notifies the two sub-components that the currentMIndex has changed;

After the medium picture and small picture components received the new value changes in componentWillReceiveProps respectively, they setState again and updated the state inside the component, thus realizing the two communication between persons.

The above is an idea for implementation. In actual applications, the scene will be more complicated. For example, the first picture may need to play a video, and when the middle picture is clicked, it can be switched to the large picture mode. In the large picture mode, the carousel can be normal, and in When the large picture mode is turned off, the medium picture and small picture will automatically locate the last picture viewed just now. This will involve slightly more complex operations, and compatibility needs to be done on this basis.

A very embarrassing point: the animation effect during image carousel still uses jquery’s animate.

The above is the detailed content of Complete an image carousel component using React. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting?

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area?

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

How to install the Windows 10 old version component DirectPlay

6 Ways to Make Pictures Sharper on iPhone 6 Ways to Make Pictures Sharper on iPhone Mar 04, 2024 pm 06:25 PM

6 Ways to Make Pictures Sharper on iPhone

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

How to make ppt pictures appear one by one

How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader Mar 04, 2024 pm 05:49 PM

How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

What should I do if the images on the webpage cannot be loaded? 6 solutions

How to arrange two pictures side by side in wps document How to arrange two pictures side by side in wps document Mar 20, 2024 pm 04:00 PM

How to arrange two pictures side by side in wps document

See all articles