day27-作业

1.制作一个div,能够通过鼠标对其进行拖拽


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            *{
                padding: 0px;
                margin: 0px;
                box-sizing: border-box;
            }
            .box{
                width: 200px;
                height: 200px;
                background-color: #5F9EA0;
                position: relative;
                top: 100px;
                left: 200px;
            }
        </style>
    </head>
    <body>
        <div class="box"></div>
        <script type="text/javascript">
            let box = document.querySelector('.box')
            box.addEventListener('mousedown', (evt)=>{
                // console.log(evt.clientX, evt.clientY)
                // console.log(evt.pageX, evt.pageY)
                // console.log(evt.offsetX, evt.offsetY)
                // console.log(box.offsetLeft, box.offsetTop)
                let ol = evt.offsetX
                let ot = evt.offsetY
                // console.log(ol, ot)
                downDrag()
                function downDrag(ol, ot){
                        box.addEventListener('mousemove', moveFunction)
                }
                function moveFunction(evt){
                    let top = evt.clientY - ot
                    let left = evt.clientX - ol
                    let rightLimit = innerWidth - parseInt(document.defaultView.getComputedStyle(box).width)
                    let bottomLimit = innerHeight - parseInt(document.defaultView.getComputedStyle(box).height)
                    // console.log(document.body.clientHeight)
                    if (top < 0){
                        top = 0
                    }else if (top > bottomLimit){
                        top = bottomLimit
                    }
                    if (left < 0){
                        left = 0
                    }else if (left > rightLimit){
                        left = rightLimit
                    }
                    // console.log(top, left)
                    box.style.top = top + 'px'
                    box.style.left = left + 'px'
                }
                window.addEventListener('mouseup', ()=>{
                    box.removeEventListener('mousemove', moveFunction)
                })
            })
            // box.addEventListener('mouseup', (evt)=>{
            //  let divTop = evt.pageY - oldY
            //  let divLeft = evt.pageX - oldX
            //  box.style.top = divTop + document.defaultView.getComputedStyle(box).top
            //  box.style.left = divLeft + + document.defaultView.getComputedStyle(box).left
            // })
        </script>
    </body>
</html>

可拖拽div
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 31-Justoneheart-北京-3 鸡血日 周围没有通过理财逆袭的,但有通过理财很好的改善生活的,感觉是高于...
    Justoneheart阅读 148评论 0 0
  • 一.图片3s自动切换,鼠标事件触发停止切换。 效果: 二、实现上题的功能的前提下,点击小图标可以切换到对应的图片 效果:
    Deathfeeling阅读 332评论 2 1
  • 1、实现点击按钮,滚动条走动和百分比走动 2、实现秒表
    七一欧阅读 239评论 0 1
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,163评论 1 32
  • (续jQuery基础(1)) 第5章 DOM节点的复制与替换 (1)DOM拷贝clone() 克隆节点是DOM的常...
    凛0_0阅读 1,392评论 0 8