Today I have learned carousel in javascript

 Carousel in javascript:

An image Slider or Image Carousel is an expedient way to show multiple images on a website. Alluring flashy images can draw many visitors to the site.

 In this post, we will create the above image slider using HTMLCSS and JavaScript. Let’s begin with creating the image slider. 

 Step – 1: To create the structure for the image slider using HTML we will use a parent div called container and inside that div, we will insert all the images from the respective sources.

Step – 2: Once we have created the HTML structure for our image slider, the next step is to style the slider using CSS. We will add styles to the images, backgrounds etc. We will also style the dots and make our images responsive and browser-friendly using CSS. 

Step – 3: After adding styles to the slider, the last thing left is to use javascript to add the functionality of auto changing of images after a specific time interval. In the code snippet below, at the very beginning, we took all the div elements with class name ‘image-sliderfade’ in an array and did the same for divs with class name ‘dots’ by using getElementByClassName() listener. After that, we set the display for all of the divs, containing images. In the last for-loop, we have erased the class name for each element of array dots[].

After we are done with these, we set the display as a block of the image to be shown and append the class name to the corresponding element of the dots[] array. Function setTimeout is used for calling the function showslides() in an interval of 2 seconds.

 image:[flower1,flower2,flower3],currentImageIndex: 0,
               }
    }
    nextImage = () => {
      this.setState((prevState) => ({
        currentImageIndex: (prevState.currentImageIndex + 1) % 4,
      }));
    };
prevImage = () => {
      this.setState((prevState) => ({
        currentImageIndex:
          prevState.currentImageIndex === 0 ? 3 : prevState.currentImageIndex - 1,
      }));
    };
<div class="container">
          <div class="row justify-content-center">
            <div class="col-10">
              <div className="image-slider text-center">
                <h4
                  className="skillnew"
                  align="center"
                  style={{ color: "rgb(32, 43, 93)", fontWeight: "700" }}
                >
                  Flowers
                </h4>

                <Icon
                  icon="fa-solid:less-than"
                  color="gray"
                  style={{ cursor: "pointer" }}
                  onClick={this.prevImage}
                  className="lefti"
                />

<img
                  className="conimgnew equal-width-image"
                  src={this.state.image[this.state.currentImageIndex]}
                  alt={`Image ${this.state.currentImageIndex + 1}`}
                />
<Icon
                  icon="fa-solid:greater-than"
                  color="gray"
                  onClick={this.nextImage}
                  style={{ cursor: "pointer" }}
                  className="righti"
                />
</div>
Output:


Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript