Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将div移离鼠标_Javascript_Jquery_Css - Fatal编程技术网

Javascript 如何将div移离鼠标

Javascript 如何将div移离鼠标,javascript,jquery,css,Javascript,Jquery,Css,我正在制作一个登录页,我希望我的徽标在鼠标光标后移动,这意味着我想将其从鼠标方向移开,但它只是随机移动,而不是以精确的方式移动 这只是一个html页面,我可以使用任何开源的首选javascript <!DOCTYPE html> <html lang="en"> <head> <style>.container { margin: 0 auto; width: 300px; height: 3

我正在制作一个登录页,我希望我的徽标在鼠标光标后移动,这意味着我想将其从鼠标方向移开,但它只是随机移动,而不是以精确的方式移动

这只是一个html页面,我可以使用任何开源的首选javascript

<!DOCTYPE html>
<html lang="en">
<head>
    <style>.container {
        margin: 0 auto;
        width: 300px;
        height: 300px;
        border: 1px #000 solid;
        position: relative;
      }
      .box {
        width: 50px;
        height: 50px;
        border: 1px #000 solid;
        position: absolute;
        right: 200px;
        background: red;
        transition: 0.5s;
      }
      </style>
  </head>
</head>
<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <section class="container">
      <div class="box"></div>
    </section>

    <script type="text/javascript">
  $(document).ready(function(){
    $('.box').hover(function() {
    $(this).box;
    $(this).css('position','absolute').css('top', Math.random()*200 + 'px').css('left', Math.random()*200 + 'px');
    });
    });
</script> 
</body>
</html>

.集装箱{
保证金:0自动;
宽度:300px;
高度:300px;
边框:1px#000实心;
位置:相对位置;
}
.盒子{
宽度:50px;
高度:50px;
边框:1px#000实心;
位置:绝对位置;
右:200px;
背景:红色;
过渡:0.5s;
}
$(文档).ready(函数(){
$('.box').hover(函数(){
$(此).box;
$(this.css('position','absolute').css('top',Math.random()*200+'px').css('left',Math.random()*200+'px');
});
});

我希望它后面紧跟着光标。

首先使用
getBoundingClientRect()
获取元素的位置(左、上、右和下)

然后使用鼠标坐标计算最近的边并移动长方体

下面是一个你将如何做的例子。您可以根据需要进行配置

检查工作代码段

$(文档).ready(函数(){
$(“.box bounds”).mouseenter(函数(e){
封闭边缘(e,本);
});  
});
函数moveDiv(鼠标、边缘、元素){
常量宽度=$(elem).outerWidth();
常量高度=$(元素).outerHeight();
交换机(边缘){
案例“左”:
$(elem).css({
左:mouse.pageX+5
});
打破
案例“权利”:
$(elem).css({
左:mouse.pageX-宽度-20
});
打破
案例“顶部”:
$(elem).css({
顶部:mouse.pageY+5
});
打破
案例“底部”:
$(elem).css({
顶部:mouse.pageY-高度-20
});
打破
}
}
功能关闭边缘(鼠标、元素){
设elembound=elem.getBoundingClientRect();
让elementLeftEdge=elembound.left;
让elementTopEdge=elembound.top;
让elementRightEdge=elembound.right;
让elementBottomEdge=elemBounding.bottom;
让mouseX=mouse.pageX;
让mouseY=mouse.pageY;
让topEdgeDist=Math.abs(elementTopEdge-mouseY);
让bottomEdgeDist=Math.abs(elementBottomEdge-mouseY);
让leftedgeist=Math.abs(elementLeftEdge-mouseX);
设rightededist=Math.abs(elementRightEdge-mouseX);
设min=Math.min(TopEdgeList、BottomEdgeList、LeftEdgeList、RightEdgeList);
让位;
开关(最小值){
案例左侧编辑:
position=“left”;
打破
案例右侧编辑:
position=“right”;
打破
案例主题专家:
position=“top”;
打破
案例底部编辑:
position=“bottom”;
打破
}
moveDiv(鼠标、位置、元素);
}
.container{
保证金:0自动;
高度:300px;
宽度:100%;
边框:1px#000实心;
位置:相对位置;
}
.框边界{
填充:10px;
位置:绝对位置;
}
.盒子{
宽度:50px;
高度:50px;
边框:1px#000实心;
右:200px;
背景:红色;
过渡:0.5s;
}


这个问题不是很清楚。您希望您的徽标完全跟随鼠标光标吗?如果你这样做了,那么你可以使用CSS为鼠标光标使用一个图像,如果有帮助的话?我在这里做了如下检查:@Archer我希望div像与光标相反的磁性效果一样移动。我想你只需要搜索一个库,它将为你提供你需要的效果。这听起来像是有人向你提供代码,你说“但它也需要这样做……”,并将以这种方式继续下去。堆栈溢出是针对特定的代码问题,而不是一般的代码请求。这太模糊了。试着按照你的要求去做,然后也许我们能帮上忙,但是你发布的内容与你试图描述的内容无关。它只是随机移动一个div。