Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

eslam said
PLUS
eslam said
Courses Plus Student 6,734 Points

animation code

i have 2 svgs and i added a mouseover event but i feel like my code is repetitve if iam right is there is any way to make it more minimal ?

const previousBtn = document.querySelector('.previous');
const nextBtn = document.querySelector('.next');


// Previous Button hover
previousBtn.addEventListener('mouseover', ()=>{
    const previousCircle = document.querySelector('.previous-circle svg');
    const leftArrow = document.querySelector('.previous-arrow svg');
    previousCircle.style.transform = 'translateX(-10px)';
    previousCircle.style.transition = '0.5s';
    leftArrow.style.transform = 'translateX(-20px)';
    leftArrow.style.transition = '0.5s';
});
previousBtn.addEventListener('mouseleave', ()=>{
    const previousCircle = document.querySelector('.previous-circle svg');
    const previousArrow = document.querySelector('.previous-arrow svg');
    previousCircle.style.transform = 'none';
    previousCircle.style.transition = '0.5s';
    previousArrow.style.transform = 'none';
    previousArrow.style.transition = '0.5s';
});
// Next button hover
nextBtn.addEventListener('mouseover', ()=>{
    const nextCircle = document.querySelector('.next-circle svg');
    const nextArrow = document.querySelector('.next-arrow svg');
    nextCircle.style.transform = 'translateX(10px)';
    nextCircle.style.transition = '0.5s';
    nextArrow.style.transform = 'translateX(20px)';
    nextArrow.style.transition = '0.5s';
});
nextBtn.addEventListener('mouseleave', ()=>{
    const nextCircle = document.querySelector('.next-circle svg');
    const nextArrow = document.querySelector('.next-arrow svg');
    nextCircle.style.transform = 'none';
    nextCircle.style.transition = '0.5s';
    nextArrow.style.transform = 'none';
    nextArrow.style.transition = '0.5s';
});

1 Answer

Steven Parker
Steven Parker
231,269 Points

Functions are often good for compacting repetitive code:

const previousBtn = document.querySelector('.previous');
const nextBtn = document.querySelector('.next');

function addHoverEvent(element, event, circle, arrow, cxform, axform) {
    element.addEventListener(event, ()=>{
        const theCircle = document.querySelector(circle);
        const theArrow = document.querySelector(arrow);
        theCircle.style.transform = cxform;
        theCircle.style.transition = '0.5s';
        theArrow.style.transform = axform;
        theArrow.style.transition = '0.5s';
    });
}
// Previous Button hover
addHoverEvent(previousBtn, 'mouseover', '.previous-circle svg', '.previous-arrow svg',
              'translateX(-10px)', 'translateX(-20px)');
addHoverEvent(previousBtn, 'mouseleave', '.previous-circle svg', '.previous-arrow svg',
              'none', 'none');
// Next button hover
addHoverEvent(nextBtn, 'mouseover', '.next-circle svg', '.next-arrow svg',
              'translateX(-10px)', 'translateX(-20px)');
addHoverEvent(nextBtn, 'mouseleave', '.next-circle svg', '.next-arrow svg',
              'none', 'none');
eslam said
eslam said
Courses Plus Student 6,734 Points

I don't know what to say, it sure take you long time to write this, thank you so much bro i appreciate it, god bless you

Steven Parker
Steven Parker
231,269 Points

Always happy to help. And it really wasn't so bad, I just took one of your calls, wrapped it in the new function, and made it generic by converting anything that differed between the repeated code elements into a parameter. The fact that your code was very clear and clean to start with was very helpful!

Happy coding!