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

These are five line of JS code and very simple to read. I do not know why this happening.

<html>
<head>
<title>Page Title</title>
<style>
*{margin:0;}
@keyframes move{
0%{
height:0;
width:0;
}
100%{
height:30px;
width:30px;
}
}
</style>
</head>
<body>
<div style="height:100px;width:100px;position:relative;background:red;">
<span style="height:30px;width:30px;position:absolute;
transform:translate(-50%,-50%);animation:6s move infinite;background:blue;">
</div>
<script>
const div = document.querySelector('div')
div.addEventListener('click',function(e){

const child =div.children[0]
child.style.top=e.clientY     - e.target.offsetTop + 'px'
child.style.left=e.clientX   - e.target.offsetLeft + 'px'
})
/*I  do not know why this happens when
 I click the div at the bottom right twice at the
 same location, and the second time I  click the div, 
the span tag move to the top left.
*/


/*If I remove the e.target.offsetTop and left, that will 
not happen, but I want to know why adding e.target.offsetTop 
   and left  make unwanted change.
*/
</script>
</body>
</html>

1 Answer

Please, do not respond, and I figure out why this happens.