Click here to Skip to main content
15,671,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
so i have this js written to help animate my CSS transitions and it works fine when its just one per html page. But when i try to add more then one to any html file it only animates one. What I'm doing wrong? I'm still new to java any help is appreciated. Just wanna have more then one transition on my page.


window.onload = () => {
    const transition_el = document.querySelector('.transition');
    const anchors = document.querySelectorAll('a');

    setTimeout( () => {

    }, 500);

    for (let i = 0; i < anchors.length; i++) {
        const anchor = anchors[i];

        anchor.addEventListener('click', e => {
            let target =;


            setTimeout(() => {
                window.location.href = target;

            }, 500)




.transition-catching {
    content: url(/HP/Catching.svg);
    width: 27%; 
    z-index: 101;
    transition: 0.5s ease-out;
} {
    margin-left: -500px;


.transition-clean {
    content: url(/HPA/Clean.svg);
    width: 81%;
    margin-top: 30%;
    margin-left: -20%;
    z-index: 101;
    transition: 0.5s ease-out;
} {
    margin-left: 40px;

HTML this is what I have it called out within my html

<a href="contact.html" class="transition transition-catching is-active"></a>

      <a href="contact.html" class="transition transition-clean is-active"></a>

What I have tried:

I have tried adding the is active class to another call out but must of done it wrong.
Updated 21-Nov-22 0:44am
Richard Deeming 21-Nov-22 6:37am    
"... still new to java ..."
NB: Despite the similar names, Javascript and Java are two completely different languages.

1 solution

document.querySelector returns a single element which matches the selector. If you want to get all matching elements, you need to call document.querySelectorAll and iterate over the list.
window.onload = () => {
    setTimeout(() => document.querySelectorAll('').forEach(el => el.classList.remove('is-active')), 500);
    document.querySelectorAll('a.transition').forEach(anchor => anchor.addEventListener('click', e => {
        const target =;
        setTimeout(() => window.location.href = target, 500);
Document.querySelector() - Web APIs | MDN[^]
Document.querySelectorAll() - Web APIs | MDN[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900