DEV Community

Cover image for Building Better Websites with CSS and JavaScript Motion Design
Azam
Azam

Posted on

Building Better Websites with CSS and JavaScript Motion Design

Animations are a powerful tool for enhancing the user experience on a website. They can be used to add visual interest, draw attention to important elements, and create a sense of interactivity and responsiveness. Animations can also help to guide users through a website and make it easier for them to navigate and understand the content.

Here's a simple example of an animation that can be used to draw attention to a call-to-action button:

.button{
    background-color: #3498db;
    color:#fff;
    padding:.75rem 1.5rem;
    border:none;
    border-radius: 5px;
    transition: transform 0.3s ease-in-out;
}

.button:hover{
    transform: scale(1.1);
}
Enter fullscreen mode Exit fullscreen mode

In this code, we have a call-to-action button with a blue background color. When the user hovers over the button, it scales up slightly to draw attention and indicate that it is clickable. This creates a subtle but effective animation that can help to increase conversions and engagement.

Now, let's take a look at a more complex example. In this case, we'll use JavaScript to create a scroll-triggered animation that reveals content as the user scrolls down the page:

const revealElements = document.querySelectorAll('.reveal')

function revealOnScroll() {
    revealElements.forEach(element => {
        const elementTop = element.getBoundingClientRect().top;
        const windowHeight = window.innerHeight
        if (elementTop < windowHeight - 100) {
            element.classList.add('visible')
        } else {
            element.classList.remove('visible')
        }
    });
}
window.addEventListener('scroll', revealOnScroll)
Enter fullscreen mode Exit fullscreen mode
.reveal {
    opacity: 0;
    transform: translateY(30px);
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
}

.reveal.visible {
    opacity: 1;
    transform: translateY(0);

} 
Enter fullscreen mode Exit fullscreen mode

In this code, we have a set of elements with the "reveal" class that are initially hidden using CSS. When the user scrolls down the page, a JavaScript function checks if each element is within the viewport and adds the "visible" class if it is. This triggers an animation that fades in and translates the element slightly to create a smooth reveal effect.

This type of animation can be used to add interest and interactivity to long-form content such as blog posts or product pages. By breaking up the content into smaller sections that are revealed as the user scrolls, you can create a more engaging and memorable user experience.

I hope these examples help to illustrate how animations can bring a website to life and provide some inspiration for your own projects!

Top comments (8)

Collapse
 
desoga profile image
deji adesoga

A little suggestion. Instead of using photos to display code, it's better to use markdown so it's accessible to your readers. Cheers!

Collapse
 
eerk profile image
eerk

Or maybe the actual animation, if that's possible.

Collapse
 
azam4code profile image
Azam

Of course! I'll keep in mind next time. Thanks.

Collapse
 
desoga profile image
deji adesoga

Yeah, absolutely. I would suggest either using CodePen to display this or the Screen to GIF app.

Collapse
 
azam4code profile image
Azam

Thanks for your help! I'm new here, so I appreciate your guidance. I'll definitely keep it in mind for the future!

Collapse
 
desoga profile image
deji adesoga

You're welcome. I'm glad I could be of help.

Collapse
 
datarecove95829 profile image
Camila John

Great article. CSS and JavaScript motion design can add interactivity and visual interest to websites, making them more engaging and effective at communicating a message or promoting a product.

Collapse
 
azam4code profile image
Azam

Thank you for your feedback! I completely agree that CSS and JavaScript motion design can make a huge difference in the overall user experience of a website. By adding interactivity and visual interest, we can capture our audience's attention and effectively communicate our message or showcase our products. It's always exciting to see how animation and design can work together to create engaging and memorable digital experiences.