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

Can't figure out these codes

I found this code on codepen but don't understand how it works. It also says js (babel) isn't this normal js?

Codepen link → https://codepen.io/electerious/pen/MQrRxX

document.querySelector('.button').onmousemove = (e) => {

    const x = e.pageX - e.target.offsetLeft
    const y = e.pageY - e.target.offsetTop

    e.target.style.setProperty('--x', `${ x }px`)
    e.target.style.setProperty('--y', `${ y }px`)

}

1 Answer

Blake Larson
Blake Larson
13,014 Points

The x and y javascript variables are finding the absolute position of where the mouse is on the button element. You can log them as you hover on the button..

document.querySelector('.button').onmousemove = (e) => {

    console.log({
        left: e.pageX - e.target.offsetLeft,
        top: e.pageY - e.target.offsetTop
    });
    const x = e.pageX - e.target.offsetLeft
    const y = e.pageY - e.target.offsetTop
// setProperty sets the --x and --y variable in the scss file..
// to the x and y variable which changes
// the absolute top and and left position of the ::before element
//  that is 0px height and width initially, 
// but changes to a height and width 400px
// when there is a hover on the button (&:hover::before { --size: 400px;} 
// so the ::before element changes position
    e.target.style.setProperty('--x', `${ x }px`) 
    e.target.style.setProperty('--y', `${ y }px`)
}

Also, if you take the overflow: hidden; style off of the .button you can see the blur follow the mouse around and wrap your head around the trick a little easier.