circle follow mouse
by Alexandru Gatea
HTML
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="container">
<div class="coordinates">
<p>
Mouse X = <span class="coord x">0</span>; Y=<span class="coord y">0</span>;
</p>
</div>
<div class="box">
<div class="circle">
</div>
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
height: 100vh;
background: #eee;
}
.box {
width: 300px;
height: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #444;
overflow: hidden;
}
.circle {
display: block;
width: 30px;
height: 30px;
border-radius: 50%;
background: #ff8800;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
transition-delay: 1s;
transition: all 1s ease;
}
.coordinates {
text-align:center;
padding:20px;
}
.coord {
display:inline-block;
padding: 3px 5px;
border: 1px solid #ccc;
text-align:center;
min-width:40px;
}
JavaScript
jQuery(document).ready(function($) {
var parentOffset = $(".box").offset();
$(".box").on('mousemove', function(e) {
var x = e.pageX - parentOffset.left;
var y = e.pageY - parentOffset.top
$('.circle').css({
left: x,
top: y
});
$('.x').html(x);
$('.y').html(y);
});
});