BACK TO Articles

Bring seasonal sparkle to your website with this simple script

By Tim McVinish

‘Tis the season for festive websites

Well, it’s the holiday season. While our homes, shopping centres, and everything else around us gets a sprinkle of holiday magic, websites are often overlooked. So today we’re going to explore creating a simple script that can be used to add a little festive flare to any website. This script will display a wreath and countdown to Christmas that, when clicked, triggers a whimsical snowflake animation. Let’s dive into the details!

Getting your digital decorating kit ready

Before we start decking the digital halls, let’s run through everything needed to add some festive flair to your website. You’ll need:

Essential tools

  • Access to your website’s code or the ability to add a script (more on that later)
  • A code editor
  • Basic understanding of HTML, CSS, and JavaScript
  • Holiday-themed images (PNG files with transparent backgrounds are preferred). We’re using a wreath.
  • Somewhere to host the image so our script can reference it

Step 1: Pulling out the digital decos

Just like with decorating the house, lets first open our cupboard and dig out our decorations. Personally I’m a minimalist, so we’ll keep things simple for this demo. Feel free to go as over-the-top-tacky-tinsel-too-many-fairy-lights-festive as you desire though.

const wrapper = document.createElement('div');
wrapper.id = 'the-wrapping';
    
const image = document.createElement('img');
image.id = 'digital-decoration';
image.src = '../../../assets/img/xmas-decoration-trimmed.png';

const countdown = document.createElement('div');
countdown.id = 'christmas-countdown';
countdown.textContent = 25 - new Date().getDate();
    
wrapper.appendChild(image);  
wrapper.appendChild(countdown);
document.body.appendChild(wrapper);

Here we’ve got a wrapper to contain everything, a png of a Christmas wreath, and a simple countdown display the sleeps till Christmas.

Step 2: Hanging the garlands

We’ll use a dash of super special Christams CSS to place our decorations on the page. This will include styles, positioning and animations. Again, feel free to add your own spin and create your own winter wonderland.

First, we need to define the styles for our Christmas-themed elements. This includes positioning, animations, and responsive design. Here’s how you can do it:


const styles = document.createElement('style');
  
styles.textContent = `
  #the-wrapping {
    position: fixed;
    display: flex;
    align-items: center;
    justify-content: center;
    bottom: 50px; // position as you desire
    right: 50px; // position as you desire
    width: 120px; // size as you desire
    height: 120px; // size as you desire
    text-align: center;
    font-size: 1rem;
    background-color: rgba(255, 255, 255, 0.9);
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
    border-radius: 50%;
    z-index: 1001;
    cursor: pointer;
    user-select: none;
  }

  #digital-decoration {
    position: absolute;
    top: 2px;
    left: 6px;
    width: 114px;
    z-index: 1001;
    animation: sway 2s ease-in-out infinite;
  }

  #christmas-countdown {
    width: 118px;
    font-size: 2rem;
    font-weight: bold;
    color: #333;
  }

  @keyframes sway {
    0%, 100% { transform: rotate(-5deg); }
    50% { transform: rotate(5deg); }
  }

  .snowflake {
    position: fixed;
    pointer-events: none;
    animation: fall linear forwards;
    z-index: 1000;
  }

  @keyframes fall {
    to {
      transform: translateY(100vh);
      opacity: 0;
    }
  }
`;
document.head.appendChild(styles);

Step 3: A sprinkle of javascript magic

Those with a keen eye may have noticed the snowflake class in our css. “But there were no snowflakes in step 1!?”. This is for a final touch, a fun little click animation that creates a snow shower upon clicking our festive finishings. With that said, let’s add a listener for click events on our decorations and then trigger a createSnowflake function.

const christmasDecoration = document.getElementById('the-wrapping');
christmasDecoration.addEventListener('click', () => {
  // Create snowflake effect
  for (let i = 0; i < 10; i++) {
    createSnowflake();
  }
});

function createSnowflake() {
  const snowflake = document.createElement('div');
  snowflake.innerHTML = '❄';
  snowflake.className = 'snowflake';
  snowflake.style.left = `${christmasDecoration.offsetLeft + Math.random() * 110}px`;
  snowflake.style.top = `${christmasDecoration.offsetTop}px`;
  snowflake.style.fontSize = `${Math.random() * 10 + 10}px`;
  snowflake.style.animationDuration = `${Math.random() * 8 + 1}s`;
    
  document.body.appendChild(snowflake);
    
  // Remove snowflake after animation
  setTimeout(() => {
    snowflake.remove();
  }, 3000);
}

Now, when our decorations are clicked a flurry of frosty flakes will float over our site. Magical!

Step 4: Spread the Christmas cheer

Now, how do we deploy this? Well that will depend on where your site lives. If you have access to your code base then you can simply wrap the script in some <script> tags and paste it where you please. If you’re using a platform like WordPress, Wix, or Squarespace, then you should be able to inject a script into your site via your dashboard. Below are some guides for these platforms. If you’re using a different platform Google should be able to help you out. Most platforms will provide a way to inject scripts.

And that’s a wrap! Please feel free to share any magical creations or alterations you come up with.

Guides for adding scripts to popular website platforms:

The complete script

(function () {
	// content
	const wrapper = document.createElement('div');
	wrapper.id = 'the-wrapping';
    
	const image = document.createElement('img');
	image.id = 'digital-decoration';
	image.src = './path-to-your-image/your-image.png'; // replace with the path                            
        to your image

	const countdown = document.createElement('div');
        countdown.id = 'christmas-countdown';
        countdown.textContent = 25 - new Date().getDate();
    
        wrapper.appendChild(image);  
        wrapper.appendChild(countdown);
        document.body.appendChild(wrapper);
  
        // styles
        const styles = document.createElement('style');
  
        styles.textContent = `
	    #the-wrapping {
	      position: fixed;
	      display: flex;
	      align-items: center;
	      justify-content: center;
	      bottom: 50px;
	      right: 50px;
	      width: 120px;
	      height: 120px;
	      text-align: center;
	      font-size: 1rem;
	      background-color: rgba(255, 255, 255, 0.9);
	      box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
	      border-radius: 50%;
	      z-index: 1001;
	      cursor: pointer;
	      user-select: none;
	    }

	    #digital-decoration {
	      position: absolute;
	      top: 2px;
	      left: 6px;
	      width: 114px;
	      z-index: 1001;
	      animation: sway 2s ease-in-out infinite;
	    }

	    #christmas-countdown {
	      width: 118px;
	      font-size: 2rem;
	      font-weight: bold;
	      color: #333;
	    }

	    @keyframes sway {
	      0%, 100% { transform: rotate(-5deg); }
	      50% { transform: rotate(5deg); }
	    }

	    .snowflake {
	      position: fixed;
	      pointer-events: none;
	      animation: fall linear forwards;
	      z-index: 1000;
	    }

	    @keyframes fall {
	      to {
	        transform: translateY(100vh);
	        opacity: 0;
	      }
	    }
	  `;
	   document.head.appendChild(styles);

	  // functionality
	  const christmasDecoration = document.getElementById('the-wrapping');
	  christmasDecoration.addEventListener('click', () => {
	    // Create snowflake effect
	    for (let i = 0; i < 10; i++) {
	      createSnowflake();
	    }
	  });

	  function createSnowflake() {
	    const snowflake = document.createElement('div');
	    snowflake.innerHTML = '❄';
	    snowflake.className = 'snowflake';
	    snowflake.style.left = `${christmasDecoration.offsetLeft + Math.random()   
            * 110}px`;
	    snowflake.style.top = `${christmasDecoration.offsetTop}px`;
	    snowflake.style.fontSize = `${Math.random() * 10 + 10}px`;
	    snowflake.style.animationDuration = `${Math.random() * 8 + 1}s`;
    
	    document.body.appendChild(snowflake);
    
	    // Remove snowflake after animation
	    setTimeout(() => {
	      snowflake.remove();
	    }, 3000);
	  }
	})();

Looking for something specific?

Search our Archive to find content that piques your interest.
SEARCH

Recents Posts

December 20, 2024
Automating holiday availability updates to keep your team in sync
By Jay Gorasia As the holiday season approaches, keeping your team organised and informed becomes more important than ever. Today's gift is a practical and time-saving solution - a no-code notification system to share team availability. Built with Power Automate and SharePoint, this simple tool will ensure everyone is on the same page, whether they’re…
Read more
December 18, 2024
A Project Managers guide: Wrap up projects like holiday gifts
By Taz Jones The holiday season isn’t just about decking the halls. It’s also the perfect time to close out projects, reflect on achievements, and set the stage for a fresh start next year. Just like a well-wrapped gift, a smooth project finish leaves everyone happy. Here are five practical tips to keep your projects…
Read more
December 17, 2024
Two ChatGPT prompts to make Christmas and the holiday season more joyful
By Jess Wong Planning a dinner party or getting ready for the holiday season can be overwhelming. From creating menus that cater to dietary needs to packing for a well-deserved getaway, there’s a lot to juggle. These practical dinner party ChatGPT prompts and holiday prep ChatGPT prompts will help streamline your planning, keep you organised,…
Read more
December 12, 2024
Checklist: Preparing your loved ones for a safe and cyber-secure Christmas 
By Rio Heral Caring for your loved ones during the festive season involves more than just ensuring they’re warm and well-fed. Cybercriminals ramp up their phishing scams at Christmas, targeting those who may not be as tech-savvy. Use this checklist to help your loved one stay safe online and enjoy a worry-free holiday season.  1.…
Read more