JavaScript-自制网页内弹窗[可移动][DOM][纯HTML]

JavaScript-自制网页内弹窗[可移动][DOM][纯HTML]

1. 实现思路

  • 编写弹窗样式,display默认为none,position为absolute;
  • 点击弹出窗口按钮,弹出的display会设置为block,即可显示弹窗;
  • 编写弹窗的鼠标按下、抬起、移动、区域离开事件函数
  • 在移动时候,获取当前鼠标x/y与上一个鼠标x/y的差值deltaX/deltaY,
    然后计算新的距离父组件的左距离、上距离,
    设定该窗口的ddDiv.style.left 、addDiv.style.top即可让弹窗跟随鼠标的移动;
  • 在点击弹窗内“确认”、“取消”按钮后,弹窗的display会设置为none,即可关闭弹窗。

2.完整HTML文件代码

<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8">
        <style>
            #root-div{
                width: 1000px;
                height: 1000px;

                border: 2px solid black;
            }
            #add-div{
                width: 600px;
                height: 400px;

                left: 200px;
                top: 200px;

                border: 1px solid rgb(111, 185, 204);

                position:absolute;

                display: none;
                
            }
            #title-div{
                width: 100%;
                height: 8%;
                background-color: cornflowerblue;
                color: white;
                font-weight: bold;
                font-size:1.4rem;
                user-select: none;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
            }
            #main-div{
                width: 100%;
                height: 84%;
            }
            #bottom-div{
                width: 100%;
                height: 8%;

                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: flex-end;
            }
            #ok-btn{
                margin-right: 10px;
                margin-top: 2px;
                margin-bottom: 5px;
                width: 100px;
                height: 30px;
                color: white;
                border: 0px;
                border-radius: 15px;
                background-color: rgb(91, 151, 241);
            }
            #ok-btn:active{
                background-color: rgb(65, 119, 199);
            }
            #no-btn{
                margin-right: 10px;
                margin-top: 2px;
                margin-bottom: 5px;
                width: 100px;
                height: 30px;
                color: white;
                border: 0px;
                border-radius: 15px;
                background-color: rgb(143, 142, 142);
            }
            #no-btn:active{
                background-color: rgb(97, 97, 97);
            }
            #add-btn{
                margin-right: 10px;
                margin-top: 10px;
                margin-left: 450px;
                width: 60px;
                height: 60px;
                color: white;
                border: 0px;
                font-weight: bold;
                font-size: 2rem;;
                border-radius: 30px;
                background-color: rgb(91, 151, 241);
            }
            #add-btn:active{
                background-color: rgb(65, 119, 199);
            }
        </style>
        <script>
            var downX = 0;
            var downY = 0;
            var isDown = false;
            function mouseDown(){
                const titleDiv = document.getElementById('title-div');
                titleDiv.innerHTML = '添加信息[准备...]';
                let disX = window.event.clientX;
                let disY = window.event.clientY;
                this.downX = disX;
                this.downY = disY;
                this.isDown = true
            }
            function mouseUp(){
                const titleDiv = document.getElementById('title-div');
                titleDiv.innerHTML = '添加信息';
                this.downX = 0;
                this.downY = 0;
                this.isDown = false;
            }
            function mouseLeave(){
                mouseUp();
            }
            function mouseMove(){
                const titleDiv = document.getElementById('title-div');
                const rootDiv = document.getElementById('root-div');
                const addDiv = document.getElementById('add-div');

                if(isDown === true){
                    titleDiv.innerHTML = '添加信息[移动中...]' 
                    let deltaX = window.event.clientX - this.downX;
                    let deltaY = window.event.clientY - this.downY;
                    this.downX = window.event.clientX;
                    this.downY = window.event.clientY;
                    let newOffsetLeft = addDiv.offsetLeft + deltaX;
                    let newOffsetTop = addDiv.offsetTop + deltaY;
                    let limitLeft = rootDiv.offsetWidth - addDiv.offsetWidth + 8;
                    let limitTop = rootDiv.offsetHeight - addDiv.offsetHeight + 8;

                    if(newOffsetLeft >= 8 && newOffsetLeft <= limitLeft){
                        if(newOffsetTop >= 8 && newOffsetTop <= limitTop){
                            addDiv.style.left = newOffsetLeft + 'px';
                            addDiv.style.top = newOffsetTop + 'px';
                        }
                    }
                }
            }
            function clickAdd(){
                const addDiv = document.getElementById('add-div');
                addDiv.style.display = 'block';
            }
            function clickOk(){
                const addDiv = document.getElementById('add-div');
                addDiv.style.display = 'none';
            }
            function clickNo(){
                const addDiv = document.getElementById('add-div');
                addDiv.style.display = 'none';
            }
        </script>
    </head>
    <body>
        <div id="root-div">
            <div id="add-div">
                <div id="title-div" onmousedown="mouseDown()" onmouseup="mouseUp()" onmousemove="mouseMove()" onmouseleave="mouseLeave()">添加信息</div>
                <div id="main-div"></div>
                <div id="bottom-div">
                    <button id="ok-btn" onclick="clickOk()">确认</button>
                    <button id="no-btn" onclick="clickNo()">取消</button>
                </div>
            </div>
            <button id="add-btn" onclick="clickAdd()">+</button>
        </div>
        
        
    </body>
</html>


3.自制可移动弹窗运行效果(gif)

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值