美文网首页
移动拖拽效果

移动拖拽效果

作者: zkzhengmeng | 来源:发表于2022-12-23 11:25 被阅读0次

移动拖拽效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            a {
                text-decoration: none;
            }
            html,
            body {
                width: 100%;
                height: 100%;
            }
            .box {
                width: 100%;
                top: 0;
                left: 0;
                bottom: 0;
                right: 0;
                height: 100%;
                background: rgba(0, 0, 0, 0.4);
                position: fixed;
                display: none;
            }
 
            .box1 {
                width: 60%;
                height: 60%;
                margin: 50 auto;
                background: #fff;
                position: absolute;
                left: 300px;
                top: 100px;
                border-radius: 2px;
                box-shadow: 0 1px 3px rgb(0 0 0 / 30%);
            }
            .list {
                height: 50px;
                background-color: #4491e1;
                font-size: 18px;
                line-height: 50px;
                position: relative;
                padding: 0 20px;
                display: flex;
                justify-content: space-between;
                color: #fff;
            }
            #ipt {
                color: #fff;
            }
        </style>
    </head>
    <body>
        <input type="button" value="点击弹出窗口" id="btn" />
        <div class="box">
            <div class="box1">
                <div class="list">
                    <div>标题</div>
                    <a href="javascript:;" id="ipt">x</a>
                </div>
                <div>放内容</div>
            </div>
        </div>
        <script type="text/javascript">
            //点击按钮弹出窗口
            let btn = document.querySelector('#btn')
            let box = document.querySelector('.box')
            btn.onclick = function() {
                // btn.style.display = 'none'   
                box.style.display = 'block'
            }
            //点击按钮关闭窗口
            let ipt = document.querySelector('#ipt')
            ipt.onclick = function() {
                box.style.display = 'none'
                btn.style.display = 'block'
            }
            //拖拽
            let list = document.querySelector('.list')
            let box1 = document.querySelector('.box1')
            list.onmousedown = function(e) {
                console.log(e);
                console.log(box1.offsetLeft);
                console.log(box1.offsetTop);
                let offsetX = e.pageX - box1.offsetLeft
                let offsetY = e.pageY - box1.offsetTop
 
                document.onmousemove = function(d) {
                    let left = d.pageX - offsetX;
                    let top = d.pageY - offsetY;
 
                    box1.style.left = left + 'px'
                    box1.style.top = top + 'px'
                }
                //鼠标抬起,清除鼠标移动事件         
                document.onmouseup = function() {
                    document.onmousemove = null;
                }
            }
        </script>
    </body>
</html>

相关文章

  • 移动拖拽效果

    移动拖拽效果

  • 移动端 移动拖拽效果

  • 封装移动端 vue 拖拽指令

    封装移动端 vue 拖拽指令 通过vue自定义指令,将拖拽行为封装为指令 使用transform做移动效果,需要注...

  • 拖拽,碰撞检测

    1. 拖拽 1.1 拖拽原理 鼠标拖拽效果的实现,就是在鼠标摁下和移动的时候,修改元素的定位值的效果。 1.1.1...

  • 高仿支付宝选择功能列表

    可删除,拖拽,移动,点击,拥有点击效果,添加效果、删除效果 源码链接: https://github.com/Tu...

  • 2019-03-15 文件拖拽移动

    期望效果:拖拽文件移动文件位置(相当于剪切),且拖拽过程中目标文件有背景样式。类似于windows的桌面拖拽 每个...

  • Android View的移动、缩放、旋转组合效果实战

    首先确定功能效果 1.一个View由关闭按钮、文字、拖拽按钮组成2.拖拽文字部分移动这个View3.在拖拽按钮处拖...

  • js拖拽自动排列

    上一次写了拖拽,其实主要还是想实现拖拽之后实现自动排列,跟手机屏幕那样移动图标可以自动排列,先看效果: 很常见的一...

  • 余弦函数的曲线特性

    一、实现效果 通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间的变大。效果如下: 二、原理说明 1、上面...

  • 拖拽API

    实现拖拽效果 目前实现拖拽效果 HTML5拖拽 源元素事件例子 目标元素事件 从本地拖放图片到页面中 实现拖拽

网友评论

      本文标题:移动拖拽效果

      本文链接:https://www.haomeiwen.com/subject/hryvqdtx.html