Full-Page Animated Gradient with Controls

Last updated 3 weeks, 3 days ago | 42 views 75     5

Tags:- HTML JQuery CSS

Seventh page link: Full-Page Animated Random + Moving Gradient Background

Awesome! Let’s now enhance the full-page animated gradient with manual controls, so users can:

  • Manually trigger a new random gradient

  • Control animation speed

  • Toggle animation direction (forward/backward)

This makes your background interactive and gives users more engagement options.


Full-Page Animated Gradient with Controls

✅ Features:

  • Click a button to generate new gradient colors

  • Adjust animation speed using a range slider

  • Reverse the animation direction


Complete HTML + CSS + JS (All-In-One)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Interactive Gradient Background</title>
  <style>
    html, body {
      margin: 0;
      height: 100%;
      font-family: Arial, sans-serif;
      color: white;
      overflow: hidden;
    }

    body::before {
      content: "";
      position: fixed;
      top: 0; left: 0;
      width: 100%; height: 100%;
      z-index: -1;
      background-size: 400% 400%;
      background-image: linear-gradient(45deg, #ff7e5f, #feb47b, #86fde8);
      animation: moveGradient 15s ease infinite;
      transition: background-image 2s ease;
    }

    @keyframes moveGradient {
      0% { background-position: 0% 50%; }
      50% { background-position: 100% 50%; }
      100% { background-position: 0% 50%; }
    }

    .content {
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      gap: 20px;
      text-align: center;
    }

    h1 {
      font-size: 3rem;
      text-shadow: 2px 2px 6px rgba(0,0,0,0.5);
    }

    .controls {
      background: rgba(0, 0, 0, 0.5);
      padding: 20px 30px;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0,0,0,0.3);
    }

    button, input[type="range"] {
      margin: 10px;
      padding: 10px 20px;
      font-size: 1rem;
      border: none;
      border-radius: 8px;
      cursor: pointer;
    }

    button {
      background-color: #ffffff22;
      color: white;
    }

    input[type="range"] {
      width: 200px;
    }

    label {
      display: block;
      margin-top: 10px;
      font-weight: bold;
    }
  </style>
</head>
<body>

  <div class="content">
    <h1>Interactive Gradient Background</h1>
    <div class="controls">
      <button onclick="applyNewGradient()">New Gradient ????</button>
      <button onclick="toggleDirection()">Toggle Direction ????</button>
      <label>Speed (seconds)</label>
      <input type="range" min="5" max="30" value="15" step="1" oninput="adjustSpeed(this.value)" />
      <span id="speedValue">15s</span>
    </div>
  </div>

  <script>
    let direction = 'normal';
    let animationDuration = 15;

    function getRandomColor() {
      const letters = "0123456789ABCDEF";
      let color = "#";
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    }

    function generateGradient() {
      const angle = Math.floor(Math.random() * 360);
      return `linear-gradient(${angle}deg, ${getRandomColor()}, ${getRandomColor()}, ${getRandomColor()})`;
    }

    function applyNewGradient() {
      const gradient = generateGradient();
      const styleSheet = document.styleSheets[0];

      for (let rule of styleSheet.cssRules) {
        if (rule.selectorText === "body::before") {
          rule.style.backgroundImage = gradient;
        }
      }
    }

    function adjustSpeed(seconds) {
      animationDuration = seconds;
      document.getElementById('speedValue').textContent = `${seconds}s`;

      const styleSheet = document.styleSheets[0];
      for (let rule of styleSheet.cssRules) {
        if (rule.selectorText === "body::before") {
          rule.style.animation = `moveGradient ${seconds}s ease infinite ${direction}`;
        }
      }
    }

    function toggleDirection() {
      direction = direction === 'normal' ? 'reverse' : 'normal';
      adjustSpeed(animationDuration); // re-apply animation with new direction
    }
  </script>

</body>
</html>

✨ Features Breakdown

  • body::before: Full-screen gradient layer, animated

  • JavaScript updates:

    • background-image with random gradients

    • animation-duration for speed slider

    • animation-direction toggle button

  • Clean layout and intuitive controls


✅ Try These Ideas Too

  • Save favorite gradients to local storage

  • Add a dark mode toggle

  • Add gradient rotation animation