Skip to main content

April 29, 2024

Dynamically Generated Quotes in Webflow with CMS and GSAP

4 min read

Creating engaging web experiences often involves dynamically displaying content in interesting ways to capture users' attention. In this blog post, we'll explore how to create a Dynamic CMS Quote Generator in Webflow, a popular website building platform known for its powerful design capabilities and ease of use. This tutorial will guide you through setting up a CMS (Content Management System) to manage quotes and show you two different methods to display these quotes dynamically on your website.

The first method involves displaying a random quote every time a page loads. This approach is straightforward and effective for surprising visitors with fresh content on each visit. The second method leverages the power of GreenSock Animation Platform (GSAP) combined with its ScrollTrigger plugin to reveal a quote dynamically as the user scrolls into a specific section of the page. This scroll-triggered animation can add a layer of interactivity and engagement that keeps users interested.

We'll provide step-by-step instructions for both methods, including the necessary CSS for styling and JavaScript for functionality. By the end of this post, you'll have a clear understanding of how to implement these dynamic features in Webflow, enhancing the visual appeal and user interaction of your site. Whether you're looking to spice up a blog, portfolio, or any project that could benefit from randomized or animated elements, this guide will equip you with the tools to do so efficiently and stylishly. Let's dive in!

Part 1: Generating a Quote on Page Load

Head Section

The head section contains the styles and scripts needed for both the page load and scroll functionalities, but for this part, we'll focus only on the styles needed for the page load animation.

Copy and paste the code below to the head section of your Webflow page.

<style>
  .quote_item {
    display: none;  /* Ensure all are not visible initially */
    opacity: 0;     /* Start with invisible for transition */
    transition: opacity 1s ease;  /* Transition for fading in */
  }
</style>

Body Section

The following code handles the page load functionality. Copy and paste this code into the body section of the page.

<script>
document.addEventListener("DOMContentLoaded", function() {
  // Select all elements with class '.quote_item' and choose one randomly
  const quotes = document.querySelectorAll('.quote_item');
  let randomQuoteIndex = Math.floor(Math.random() * quotes.length);
  const randomQuote = quotes[randomQuoteIndex];  // Define the randomly selected quote

  // Set the display to 'flex' and start the fade-in
  randomQuote.style.display = 'flex';  // Make it visible, but it's still transparent
  setTimeout(() => {
    randomQuote.style.opacity = 1;  // Start fading in
  }, 10);  // Short delay to ensure the display property is applied first
});
</script>

Explanation:

  • CSS Setup: Initially hides all quotes and sets them up for a fade-in transition.
  • JavaScript Functionality:Waits for the document to fully load.Selects all elements with the .quote_item class, picks one at random.Sets that quote's display to flex, making it part of the layout and thus visible.Uses a short delay to change the opacity from 0 to 1, creating a fade-in effect. This delay ensures the display change has rendered before starting the opacity transition.
  • Waits for the document to fully load.
  • Selects all elements with the .quote_item class, picks one at random.
  • Sets that quote's display to flex, making it part of the layout and thus visible.
  • Uses a short delay to change the opacity from 0 to 1, creating a fade-in effect. This delay ensures the display change has rendered before starting the opacity transition.

HTML Setup:

__wf_reserved_inherit

Part 2: Generating a Quote on Scroll Using GSAP's ScrollTrigger

Head Section

Include GSAP and ScrollTrigger libraries in addition to the initial styles.

Notice that these styles are slightly different from the implementation above. The transition property is missing because this is handled by GSAP.

<style>
  .quote_item-gsap {
    display: none;  /* Ensure all are not visible initially */
    opacity: 0;     /* Start with invisible for transition */
  }
</style>
<script defer src="<https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js>"></script>
<script defer src="<https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/ScrollTrigger.min.js>"></script>

Body Section

Here is the code that handles the scroll-triggered animations.

<script>
document.addEventListener("DOMContentLoaded", function() {
  gsap.registerPlugin(ScrollTrigger);

  // Select all elements with class '.quote_item-gsap' and choose one randomly
  const quotesGSAP = document.querySelectorAll('.quote_item-gsap');
  let randomGSAPIndex = Math.floor(Math.random() * quotesGSAP.length);
  let selectedQuoteGSAP = quotesGSAP[randomGSAPIndex];  // Define the randomly selected GSAP quote

  // Ensure the selected element is visible and part of the layout before applying GSAP animations
  selectedQuoteGSAP.style.display = 'flex';

  // Use GSAP with ScrollTrigger to fade in the quote
  gsap.to(selectedQuoteGSAP, {
    opacity: 1,  // Animate to full opacity
    duration: 1,  // Duration of the animation in seconds
    ease: "power1.in",
    scrollTrigger: {
      trigger: selectedQuoteGSAP,
      start: "top 80%",
      end: "bottom 70%",
      toggleActions: "play none none none"
    }
  });
});
</script>

Explanation:

  • CSS Setup: Keeps the GSAP-managed quotes invisible and out of the layout initially.
  • JavaScript Functionality:Registers the ScrollTrigger plugin.Selects all elements with .quote_item-gsap, picks one at random, makes it visible (display: flex).Uses GSAP with ScrollTrigger to animate the opacity when the quote scrolls into view, making it fade in with the specified trigger and duration settings.
  • Registers the ScrollTrigger plugin.
  • Selects all elements with .quote_item-gsap, picks one at random, makes it visible (display: flex).
  • Uses GSAP with ScrollTrigger to animate the opacity when the quote scrolls into view, making it fade in with the specified trigger and duration settings.

HTML:

__wf_reserved_inherit

In conclusion, integrating dynamic elements like a CMS Quote Generator into your Webflow site can significantly enhance the user experience, making your website not only more engaging but also more visually appealing. Throughout this tutorial, we’ve covered two powerful methods to display quotes: one that shows a random quote on each page load and another that reveals a quote through a compelling scroll-triggered animation using GSAP and ScrollTrigger.

By implementing these features, you're not only adding a layer of sophistication to your site but also keeping your content fresh and interactive, which can help retain visitors longer and encourage them to explore more of your site. Whether you choose to implement the page load feature, the scroll animation, or both, you now have the tools and knowledge to make your Webflow projects more dynamic and responsive to user interactions.

Remember, the key to effective web design is not just in the aesthetics but also in how the content interacts with users. We hope this guide inspires you to experiment with more interactive features in Webflow, pushing the boundaries of what your website can achieve. Happy designing!

marketing health check clipboard showing score

Your Marketing Isn't Bringing in Leads and You Can't Figure Out Why

Score your marketing across 7 key areas in 5 minutes and find out exactly what's holding back your leads and what to fix first.

Questions You Might Be Asking

No — search engines see all the quote items in the CMS-bound DOM, so every quote is crawlable regardless of which one is visible to the user. The randomization happens in CSS and JavaScript after the HTML is delivered. To be safe, make sure your quotes aren't wrapped in display: none at the markup level — keep them in the DOM and hide them via opacity or a visually-hidden utility so bots still index them.

Yes — wrap the selection logic inside a ScrollTrigger callback that fires onEnter or at specific progress thresholds. Each time the trigger activates, grab a fresh random index and swap the visible quote with a cross-fade. Keep a reference to the last index shown so you don't pick the same quote twice in a row — that's the most common issue with this pattern.

Set the initial opacity and display in your CSS, not in GSAP's set() call. GSAP runs after the script loads, which can produce a brief flash if CSS rules haven't established the hidden state first. Declare display: none and opacity: 0 in your stylesheet, then let GSAP toggle display: flex and animate opacity from there. This avoids the FOUC regardless of script timing.

In Webflow, up to a few hundred is fine since the CMS delivers them as HTML and JavaScript just picks one. Past 500 or so, page weight starts to matter. For much larger sets, move selection server-side or use Webflow's CMS filters to randomize the query itself, so only one quote ships in the HTML. The page-load and scroll patterns in this tutorial assume all quotes are present in the DOM.

Stop losing deals to a website that's holding you back.

Book a 30-minute strategy session. You'll leave with a clearer picture of what your site needs to do and a rough budget range, whether you hire us or not.

background cta image collage of websites

Other posts in this category

video thumbnail showcasing video and how to build trust with testimonials in the background

How to build trust quickly with potential clients

Building trust with potential clients is something that will be the difference between making a sale or not. Here's how you can build trust quickly.

A three-way comparison graphic: three website mockups side by side labeled DIY Builder, Template, and Custom, each with a small price tag and a "best when" note beneath it, and an arrow underneath showing the progression as a business grows.

Is a Custom Website Worth It, or Should I Use a Template or DIY Builder?

Custom, template, or DIY builder? Here's how the three options compare, plus a simple decision framework.

confetti over Vimeo Video playing

How to Add a Confetti Animation When a Vimeo Video Starts Playing

Learn how to trigger a canvas confetti animation when a Vimeo video starts playing using the Vimeo Player API play event