javaScript,如何让一个div盒子随着鼠标移动而移动

前言:如果想让一个div的盒子随着鼠标移动而移动,那么就必须获取到鼠标的坐标!

文章目录:

  1. 原JavaScript实现
  2. 如果使用JQuery实现

关键字

  • 获取左坐标

    • var left = event.pageX ;

  • 获取下坐标

    • var top = event.pageY ;

在Idea里面,这个获取方式已经过时了的,不多说,直接看下面代码,v+v就可以直接html运行测试

虽然event获取方式过时了,但还是可以用的!

QQ录屏20230517142105

一、原JavaScript js

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>

</head>
<body>
<div class="layui-btn-container">



    <button id = "buttonid" style="cursor: pointer" onmouseover="pri1('我叫张三')" onmouseout="out()" >我叫张三</button>
    <button id = "buttonid2"style="cursor: pointer" onmouseover="pri1('我叫李四')"onmouseout="out()">我叫李四</button>
    <button id = "buttonid3"style="cursor: pointer" onmouseover="pri1('我叫王五')"onmouseout="out()" >我叫王五</button>
    <button id = "buttonid4"style="cursor: pointer" onmouseover="pri1('我叫赵六')"onmouseout="out()">我叫赵六</button>
    <button id = "buttonid5"style="cursor: pointer" onmouseover="pri1('我叫李琦')" onmouseout="out()">我叫李琦</button>
    <button id = "buttonid6"style="cursor: pointer" onmouseover="pri1('我叫王八')" onmouseout="out()">我叫王八</button>
    <button id = "buttonid7"style="cursor: pointer" onmouseover="pri1('我叫老旧')" onmouseout="out()">我叫老旧</button>
    <button id = "buttonid8"style="cursor: pointer" onmouseover="pri1('我叫试试')" onmouseout="out()">我叫试试</button>

    <div id = "div1" style="width:300px; height:300px; border: 1px solid red; display: none;cursor: pointer"></div>




</div>


<script>

   function pri1(name){
       var left = event.pageX ;
       var top = event.pageY ;
       document.getElementById("div1").innerText = name;
       document.getElementById("div1").style.display="block";
       document.getElementById("div1").style.left=left+"px";
       document.getElementById("div1").style.top=top+40+"px";
       document.getElementById("div1").style.position="absolute";
   }

   function out(){

       document.getElementById("div1").style.display="none";

   }

</script>
</body>
</html>

二、如果使用JQuery也实现相关的操作呢??

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
<body>
<div class="layui-btn-container">



    <button id = "buttonid" style="cursor: pointer" onmouseover="prin2('我叫张三')" >我叫张三</button>
    <button id = "buttonid2"style="cursor: pointer" onmouseover="prin2('我叫李四')">我叫李四</button>
    <button id = "buttonid3"style="cursor: pointer" onmouseover="prin2('我叫王五')" >我叫王五</button>
    <button id = "buttonid4"style="cursor: pointer" onmouseover="prin2('我叫赵六')">我叫赵六</button>
    <button id = "buttonid5"style="cursor: pointer" onmouseover="prin2('我叫李琦')" >我叫李琦</button>
    <button id = "buttonid6"style="cursor: pointer" onmouseover="prin2('我叫王八')" >我叫王八</button>
    <button id = "buttonid7"style="cursor: pointer" onmouseover="prin2('我叫老旧')" >我叫老旧</button>
    <button id = "buttonid8"style="cursor: pointer" onmouseover="prin2('我叫试试')">我叫试试</button>

    <div id = "div1" style="width:300px; height:300px; border: 1px solid red; display: none;cursor: pointer"></div>




</div>
<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="//unpkg.com/layui@2.8.3/dist/layui.js"></script>
<script>
    

    function  prin2(name) {
       // 加载 LayUI 提供的相关模块
       layui.use(['layer', 'jquery'], function () {
           // 得到模块实例对象
           var layer = layui.layer;
           var $ = layui.jquery;


           var left = event.pageX ;
           var top = event.pageY ;

           $("#div1").text(name);
           $("#div1").css("left",left);
           $("#div1").css("top",top+40);
           $("#div1").css("position","absolute");
           $(function(){
               $(this).hover(function(){
                   $("#div1").show();
               },function(){
                   $("#div1").hide();
               });
           });
       });


   }

</script>
</body>
</html>

总结:

通过这个模板,可以根据自己网页的情况进行修改成符合自己页面的效果!不多说了,希望多多关注半杯!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半杯可可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值