Home > Web Front-end > JS Tutorial > Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)

Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)

不言
Release: 2018-11-07 15:34:09
Original
2438 people have browsed it

Carousel images are often the center of attention, used in photo galleries or taking large center stage on many contemporary websites. While Adobe Flash has often been the tool of choice for working with CSS3 and JavaScript in the past, carousels can be easily implemented without a lot of code.

The technique I used here is one of the easiest ways to implement a simple carousel with a nice cross-fade transition using standard JavaScript and CSS3.

Basic HTML is trivial. Just put a few images into a div container:

<div class="slides">
  <img  src="image/cup.jpg"  alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/flower.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/flowers.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/strawberry.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/greatwall.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
</div>
Copy after login

Use CSS to stack all images inside the container and define transitions (browser-specific prefixes may have to be used for transitions):

/* the slide container with a fixed size */
.slides {
  box-shadow: 0px 0px 6px black;
  margin: 0 auto;
  width: 500px;
  height: 300px;
  position: relative;
  }
  /* the images are positioned absolutely to stack. opacity transitions are animated. */
  .slides img { 
  display: block;
  position: absolute; 
  transition: opacity 1s;
  opacity: 0;
  width: 100%;
  }
  /* the first image is the current slide. it&#39;s faded in. */
  .slides img:first-child { 
  z-index: 2; 
  /* frontmost */
  opacity: 1;
  }
  /* the last image is the previous slide. it&#39;s behind the current slide and it&#39;s faded over. */
  .slides img:last-child {
  z-index: 1; 
  /* behind current slide */
  opacity: 1;
  }
Copy after login

After this simple setting, all that remains is to change the order of the carousel to advance the carousel display. The following code snippet periodically moves the first image (the current picture) to the end of the container, making the next image the current picture. Due to the CSS rules defined above, the changes animate with a cross-fade.

function nextSlide() {
    var q = function(sel) {
     return document.querySelector(sel); 
     }   
    q(".slides").appendChild(q(".slides img:first-child"));
    }
    setInterval(nextSlide, 3000)
Copy after login

The above is the analysis of the steps. The following is the complete code





	
	
	


<div class="slides">
  <img  src="image/cup.jpg"  alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/flower.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/flowers.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/strawberry.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
  <img  src="image/greatwall.jpg" alt="Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)" >
</div>

Copy after login

The effect is as follows:

Simple carousel image playback effect implemented by JavaScript and CSS (source code attached)

The above is the detailed content of Simple carousel image playback effect implemented by JavaScript and CSS (source code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template