首页 > 其他分享 >Js 之拖拽动画

Js 之拖拽动画

时间:2022-12-30 13:11:07浏览次数:33  
标签:function 动画 bHeight top Js var document 拖拽 left

一、效果图

 

二、示例代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <style type="text/css">
        .box{
            width: 800px;
            height: 800px;
            background-color: #f5f5f5;
            margin: 0 auto;
            position: relative;
        }
        .box .item{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 0;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="item"></div>
    </div>
    <script type="text/javascript">
        var o = document.querySelector(".item"), b = document.querySelector(".box");
        o.onmousedown = function(e){
            e = e || window.event;

            //鼠标相对于盒子的位置
            var offsetX = e.clientX - o.offsetLeft;
            var offsetY = e.clientY - o.offsetTop;

            //鼠标移动
            document.onmousemove = function (e) {
                e = e || window.event

                //判断是否超出
                var left = e.clientX - offsetX;
                var top = e.clientY - offsetY;
                var oWidth = o.offsetWidth, oHeight = o.offsetHeight;
                var bWidth = b.offsetWidth, bHeight = b.offsetHeight;

                if(left < 0){
                    left = 0;
                }
                if(left > bWidth - oWidth){
                    left = bWidth - oWidth;
                }

                if(top < 0){
                    top = 0;
                }
                if(top > bHeight - oHeight){
                    top = bHeight - oHeight;
                }

                o.style.left = left + "px";
                o.style.top = top + "px";
            }
            //鼠标抬起
            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
                if (o.releaseCapture) {
                    o.releaseCapture();//释放全局捕获 
                }
            }
            return false;
        }
    </script>
</body>
</html>

 

标签:function,动画,bHeight,top,Js,var,document,拖拽,left
From: https://www.cnblogs.com/yang-2018/p/17014677.html

相关文章

  • JSON学习
    1.JSON语法是JavaScript对象表示语法的子集。l 数据在名称/值对中l 数据由逗号分隔l 花括号保存对象l 方括号保存数组JSON 值可以是:l 数字(整......
  • 5-3 掌握 egg.js + 云 mongodb
    1egg.js1.1初始化初始化和项目启动方法#初始化$mkdiregg-example&&cdegg-example$npminitegg--type=simple#实际上执行的是npmicreate-egg$npm......
  • uglifyjs压缩.js文件为.min.js
    安装uglifyjscnpminstall-guglify-js查看版本uglifyjs--version可以正常看到版本号,安装成功 压缩js文件为.min.js(xxx:文件名)uglifyjsxxx.js......
  • 【Worker】js使用多线程实现倒计时
    tremaintime.js主要用于计算时间,放到assets目录,因为Worker需要请求获取这个文件的,要同源onmessage=function({data}){setInterval(()=>{postMessage(ini......
  • 【js逆向爬虫】-有道翻译js逆向实战
    目录​​网页分析​​​​初步代码实现​​​​逆向查找参数​​​​改写代码​​​​成果展示​​​​         我是毕加锁(锁!) 期待你的关注​​大......
  • js使用escape编码、unescape解码、
     一、js使用escape编码编码前//编码前的JSON.stringify(playitemArr)"console.error("编码前的JSON.stringify(playitemArr)");console.error(JSON.stringify(playi......
  • nodejs express multer 中文名乱码【转】
    文件上传服务器端接收的文件列表中文件名不支持中文,都是乱码,查询发现nodejs对中文支持的不好。找了半天,发现这个解决方法确实有效!!!!!核心代码//解决中文名乱码的问题f......
  • P1198 JSOI2008 最大数
    P1198JSOI2008最大数-洛谷|计算机科学教育新生态(luogu.com.cn)采用ST表维护RMQ。对于插入操作,设插入后数列长度变为\(n\),我们只需重新修改满足\(i+2^j-......
  • JS Date
    镇楼图Pixiv:torinoDate构造DateJS中Date提供了时间日期相关的操作Unix时间戳:指自1970年1月1日00:00:00UTC开始经过的毫秒数,为一整数值,这也是大多数编程语言......
  • JS高级
    this指向分析指向直接调用,指向window通过对象调用,指向对象call/apply总结:跟位置无关,跟调用方式有关。只有在执行的时候this指向才会被确定绑定规则:默认......