Coding Camp
.html
and .css
files!<script>
element or include a .js
file.left
and top
styles.10vmin
left and top.
left:0vmin
left:3vmin
eye.js
hello
by placing it within the ()
x
coordinate within the webpage
pageX
left
value to the clicked value.window.innerWidth
x
value by the maximum, then multiply by 3.eye.js
function hello(event) {
/* Find the element by id */
const element = document.getElementById("pupil");
/* Find the new "x" value*/
const x = 3 * event.pageX / window.innerWidth;
/* Find the new "y" value*/
const y = 3 * event.pageY / window.innerHeight;
/* Update */
element.style["left"] = x + "vmin";
element.style["top"] = y + "vmin";
}
document.addEventListener('click', hello());
altogether.html
<!DOCTYPE html>
<html>
<head>
<style>
#eye {
position: absolute;
border-radius: 50%;
top: 50%;
left: 50%;
width: 6vmin;
height: 6vmin;
background: black;
}
#pupil {
position: absolute;
border-radius: 50%;
width: 3vmin;
height: 3vmin;
background: white;
}
</style>
<script>
function peep(e) {
const p = document.getElementById("pupil");
p.style["left"] = (3 * e.pageX / window.innerWidth).toString() + "vmin";
p.style["top"] = (3 * e.pageY / window.innerHeight).toString() + "vmin";
}
document.addEventListener('mousemove', peep);
</script>
</head>
<body>
<div id="eye">
<div id="pupil"></div>
</div>
</body>
</html>