search
HomeWeb Front-endCSS TutorialHow to achieve slide effect with css? How to implement slideshow (code example)

The content of this article is to introduce how to achieve the slide effect with css? How to implement slideshow (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Below we will use code to implement the effect of slide switching (fade in and out) step by step:

1. Create an html file and write demo

First we need to set the image list on the page, contained in a div box. Something like the following:

<div id="stage">
	<a href="img/How to achieve slide effect with css? How to implement slideshow (code example)"><img  src="/static/imghwm/default1.png"  data-src="img/2.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/2.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/2.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/3.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/3.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/4.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/4.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/5.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/5.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/6.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/6.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/7.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/7.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
	<a href="img/8.jpg"><img  src="/static/imghwm/default1.png"  data-src="img/8.jpg"  class="lazy"      style="max-width:90%"  style="max-width:90%" alt="How to achieve slide effect with css? How to implement slideshow (code example)" ></a>
</div>

In this example, all images have links, but this is not required. If you remove the link, you just need to change some CSS and JavaScript to reference 'img' instead of 'a'.

2. Use CSS to overlay images

Here is the CSS code we used for the following demo:

#stage {
    margin: 1em auto;
    width: 382px;
    height: 292px;
  }

  #stage a {
    position: absolute;
  }
  #stage a img {
    padding: 10px;
    border: 1px solid #ccc;
    background: #fff;
  }

  #stage a:nth-of-type(1) {
    animation-name: fader;
    animation-delay: 4s;
    animation-duration: 1s;
    z-index: 20;
  }
  #stage a:nth-of-type(2) {
    z-index: 10;
  }
  #stage a:nth-of-type(n+3) {
    display: none;
  }

  @keyframes fader {
    from { opacity: 1.0; }
    to   { opacity: 0.0; }
  }

By setting the link For position:absolute, we take all images out of the document stream and stack them together. We then need to specify a width and height for the #stage to reserve space on the page for the slide show. This is equal to the image dimensions plus padding (10px each side) and borders (1px each side).

We then use some nth-of-type() selectors to put the first image on top of the stack, the second image at the back of the stack, and the rest of the images hidden from the display.

Finally, we assign an animation keyframe to the top image, telling it to wait 4 seconds before fading out, setting opacity:0. All that's missing now is a bit of JavaScript to move the facing image to the bottom of the stack so that the next image can appear and fade out in sequence.

3. Use JavaScript to trigger the effect

All you need here is to assign an event handler to the image that is triggered when the keyframe animation ends. It takes the most important photo and moves it to the back:

window.addEventListener("DOMContentLoaded", function(e) {
    var stage = document.getElementById("stage");
    var fadeComplete = function(e) { stage.appendChild(arr[0]); };
    var arr = stage.getElementsByTagName("a");
    for(var i=0; i < arr.length; i++) {
      arr[i].addEventListener("animationend", fadeComplete, false);
    }

  }, false);

The new image on top is now assumed to be the nth-of-type(1) attribute, including keyframe animation --fader, and so on Other images.

That's it! No bloated code, no plugins, no libraries, just a few lines of vanilla JavaScript that works in all modern browsers.

4. Rendering:

Run the above code to get a simple fade-in and fade-out slideshow:

How to achieve slide effect with css? How to implement slideshow (code example)

Summary : The above is the fade-in and fade-out slide effect achieved in this article. You can try it yourself to deepen your understanding. I hope it will be helpful to your study.

The above is the detailed content of How to achieve slide effect with css? How to implement slideshow (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.