Cards Stack With Scrolling Effect Using CSS

Cards Stack With Scrolling Effect Using CSS
Code Snippet:Card Gang
Author: Nicholas
Published: March 22, 2024
Last Updated: March 22, 2024
Downloads: 147
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a stack of cards with a scrolling effect using CSS. It positions cards vertically with a tilt. It’s helpful for showcasing items with an interactive layout.

You can use this code on your website’s product showcase page for a visually appealing display. It enhances user engagement with its interactive scrolling effect. It provides a modern and attractive way to present information or products to your visitors.

How to Create Cards Stack With Scrolling Effect Using CSS

1. Begin by creating the HTML structure for your cards stack. Inside the <main> element, create a <div> with an id of “stack”. Within this div, add individual card elements with the class “card”.

<main>
  <div id="stack">
    <div class="card">whatever</div>
    <div class="card">let&singleQuote;s go</div>
    <div class="card">not your worst</div>
    <div class="card">right on</div>
    <div class="card">party time</div>
    <div class="card">I think I get it</div>
    <div class="card">enough then</div>
  </div>
</main>

2. In your CSS file, define the styles for the cards stack. You can set the overall layout, such as background color and font styles. Additionally, apply styles to the “.card” class to customize each card’s appearance, including its position, size, and tilt effect.

* { margin: 0; font: inherit }

body {
  background: #e3e2ff;
  text-align: center;
}

main {  
  --tilt-angle: 3deg;
  position: relative;
  display: grid;
  grid-template-rows: 1fr max-content;
  height: 100vh;
  width: 30rem;
  margin: auto;
  font-size: 2.4rem;
  top: 10vh;
}

.card {
  position: sticky;
  top: calc(var(--top) * 1px);
  box-shadow: rgba(50, 50, 93, 0.25) 0px 13px 27px -5px, rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
  background-color: white;
  border-radius: 0.5em;
  padding: 3em;
  margin-bottom: 2em;
  transform: rotate(calc(var(--tilt-angle) * (2 * (var(--r) - 0.5))));
  transition: * 0.3s ease;
}

.card.stuck {
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); /* Add a box shadow effect */
  background-color: red;
}

.card:nth-child(4n) {
  --r: 0.7;
  --top: 0;

}

.card:nth-child(4n + 1) {
  --r: 0.2;
  --top: 50;
  
}

.card:nth-child(4n + 2) {
  --r: 0.9;
  --top: 100;
  
}

.card:nth-child(4n + 3) {
  --r: 0.4;
  --top: 150;
  
}

3. If you want to add interactivity, you can use JavaScript to observe when cards intersect with the viewport and apply the “stuck” class accordingly. Uncomment the provided JavaScript code and adjust it as needed. (Optional)

// got bored 

// const cards = document.querySelectorAll(".card");

// cards.forEach((card) => {
//   const observer = new IntersectionObserver(
//     ([e]) => e.target.classList.toggle("stuck", e.intersectionRatio < 1),
//     { threshold: [1] }
//   );

//   observer.observe(card);
// });

That’s all! hopefully, you have successfully created a Cards Stack With a Scrolling Effect on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X