admin管理员组文章数量:1425849
So this is what I got.
<div class="box"></div>
.box {
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
background: yellow;
}
$(document).bind('mousemove', function(e){
$('.box').css({
top: e.pageY
left: e.pageX,
});
});
It works. The box follow the cursor. Problem is it follow the EDGE of the cursor. ie, the cursor holds the top left corner of the box. I want it to hold the center of the box, or another point that's not the edge. Any ideas how to do that?
Sorry if this dumb. I'm good enough with HTML/CSS but just starting with javascript and jQuery.
So this is what I got.
<div class="box"></div>
.box {
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
background: yellow;
}
$(document).bind('mousemove', function(e){
$('.box').css({
top: e.pageY
left: e.pageX,
});
});
It works. The box follow the cursor. Problem is it follow the EDGE of the cursor. ie, the cursor holds the top left corner of the box. I want it to hold the center of the box, or another point that's not the edge. Any ideas how to do that?
Sorry if this dumb. I'm good enough with HTML/CSS but just starting with javascript and jQuery.
Share Improve this question asked Nov 3, 2016 at 5:27 Magdi GamalMagdi Gamal 6781 gold badge12 silver badges28 bronze badges 1- there is an error on your line of codes there should be a ma after the pageY – Rani Moreles Rubillos Commented Nov 3, 2016 at 5:33
1 Answer
Reset to default 6Use this code instead
$(document).bind('mousemove', function(e){
$('.box').css({
top: e.pageY - $(".box").height()/2, // just minus by half the height
left: e.pageX - $(".box").width()/2 // just minus by half the width
});
});
本文标签: javascriptjQueryHow to make a div follow a cursor within its centerStack Overflow
版权声明:本文标题:javascript - jQuery - How to make a div follow a cursor... within its center - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745404980a2657209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论