模拟按下鼠标拖动弹出框

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        h2 {
            text-align: center;
        }

        #login {
            width: 500px;
            margin: 0 auto;
            position: fixed;
            display: none;
            /* top: 100px;
            left: 100px; */
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        table {
            width: 500px;
            height: 260px;
            background: white;
        }

        td,
        th {
            line-height: 35px;
            height: 40px;
        }

        input {
            width: 350px;
            height: 30px;
            border: lavender 1px solid;
            outline: none;
        }

        .fonts {
            text-align: right;
            font-size: 14px;
            width: 110px;
        }

        button {
            margin: 0 auto;
            background: white;
            border: lavender 1px solid;
            width: 270px;
            height: 38px;
            outline: none;
            cursor: pointer;
        }

        .box {
            font-size: 14px;
            line-height: 40px;
            text-align: center;
            width: 42px;
            height: 40px;
            border: lavender 1px solid;
            border-radius: 55%;
            background: white;
            position: absolute;
            top: -20px;
            right: -21px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <h2>点击,弹出登录框</h2>
    <div id="login">
        <div class="box">关闭</div>
        <table>
            <tr>
                <th colspan="2">登录会员</th>
            </tr>
            <tr>
                <td class="fonts">用户名:</td>
                <td><input type="text" id="uname" placeholder="请输入用户名"></td>
            </tr>
            <tr>
                <td class="fonts">登录密码:</td>
                <td><input type="password" id="pwd" placeholder="请输入登录密码"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><button>登录会员</button></td>
            </tr>
        </table>
    </div>
    <script>
        var h2 = document.querySelector(‘h2‘);
        var box = document.querySelector(‘.box‘);
        var login = document.querySelector(‘#login‘);
        var th = document.querySelector(‘th‘);
        var x, y;
        // 弹出层开始
        h2.addEventListener(‘click‘, function() {
            login.style.display = ‘block‘;
            document.body.style.backgroundColor = ‘#ccc‘;

        });
        box.onclick = function() {
                login.style.display = ‘none‘;
                document.body.style.backgroundColor = ‘‘;
            }
            // 弹出层结束
        th.addEventListener(‘mousedown‘, function(e) {
            // 计算点击后鼠标位置 距离 盒子上、 左边框的距离
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            th.style.cursor = ‘move‘;
            //在页面中的任何位置都可以移动鼠标,所以事件源是document
            document.addEventListener(‘mousemove‘, move)

            function move(e) {
                // 用鼠标在页面中点击的位置  减去 鼠标距离盒子左、上边框的距离 得到 盒子距离页面边框的距离
                login.style.left = e.pageX - x + ‘px‘;
                login.style.top = e.pageY - y + ‘px‘;
            }
            document.addEventListener(‘mouseup‘, function(e) {
                th.style.cursor = ‘default‘;
                document.removeEventListener(‘mousemove‘, move); //删除移动的监听事件
            })
        })
    </script>
</body>

</html>

原文地址:https://www.cnblogs.com/qtbb/p/11700839.html

时间: 2024-08-30 00:46:09

模拟按下鼠标拖动弹出框的相关文章

拖动弹出框

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>拖动弹出框</title> <style> .top_box{ font-weight:bold;">#6796cc; height:30px; line-height: 30px; color: #fff; padding-left

自定义jQuery浮动弹出框插件

自定义浮动弹出框使用说明 1. jquery.alert.js源代码如下: (function($){ var defaults = { width : 350, //浮动弹出框宽度 height : 200, //浮动弹出框高度 minWidth : 350, //浮动弹出框最小宽度 minHeight : 200, //浮动弹出框最小高度 fontFamily : "微软雅黑",//浮动弹出框内文字字体 fontSize : "12px",//浮动弹出框内文字大

robotframework 下对于web弹出框的处理

转发自:http://www.cnblogs.com/dreamyu/p/6877145.html 在web自动化测试中会遇到各种弹出框,在selenium中有对这些弹出框的处理. 弹出框一般有这么几类: 1.普通的弹出窗口,如果是可以定位的,直接定位到窗口,然后进行相应的操作. 2.如果是浏览器系统弹出框,alter类型的,可以使用方法: dr.switchTo().alert().accept();   或者dismiss(取消),记得在处理前先判断下是否有alter窗口,如果窗口出来的较慢

Android:继承ScrollView实现自定义向上滚动弹出框(背景半透明)

现在常见的效果:点击按钮向上弹出框展示信息,弹出后背景变为半透明,并且支持手势滑动关闭弹出框. 效果图如下: 下面上代码: 1.核心类:自定义向上弹出框 VerticalScrollView.java package app.popupbox.view; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.util.AttributeS

鼠标滑动弹出窗口

"$().ready(function() { var indx_target; $(""#grdTest th"").each(function(index) { if($(this).html().indexOf(""姓名"")>0  ){ indx_target=index; } }); $(""#grdTest tr"").find(""td:

jquery封装了一个简洁轻巧的可拖动可自定义样式的纯div+css带遮罩层的仿模态弹出框

最近封装上瘾,想起以前做的一个轻巧的弹出框,是样式和脚本分离的,于是重新封装了一下,把样式标签统一到js里了. 里面还有一些问题,但不影响使用,有兴趣的同学,可以参考完善下,有好的建议,请不吝赐教. var PopDialogDefaultConfig = { hasCover: true, //是否带遮罩层 colorOfCover: "#000", //遮罩层颜色 transparencyOfCover: 80, //遮罩层透明度(alpha值,opacity值通过换算得到) bo

弹出框插件

本插件ccShow是用来在前端显示弹出框,用户可以点击按钮,弹出一个框,也可以Ajax请求时,弹出提示语,请求中,等Ajax结束后,就显示已完成或者请求失败等情况.基于jQuery的插件,兼容各种浏览器. var ccShow = {}; var isSetTimeOut=false; var isShow = false, showType = ""; ccShow.showMsg = function (rb, type, willHide,callback,showTime) {

解决模拟弹出框情况下文档滚动的问题

用div元素来模拟弹出框很普遍,它可以自行定制各式各样的弹出框.弹出框一般用的是fixed绝对定位,也就是相对于浏览器窗口定位. 在文档高度大于窗口高度的情况下,弹出框会存在一个问题.当弹出框在显示的时候,滚动鼠标(PC端)或滑动页面(移动端)会导致文档滚动.还有在弹出框的高度也大于窗口高度时,滚动鼠标或滑动页面会先滚动弹出框,直到滚动条到头或到尾时,继续滚动弹出框仍然会导致文档的滚动. 这个问题会导致用户在隐藏弹出框返回时,页面显示不是显示弹出框前的位置.解决办法是给这个文档动态设置overf

jQuery实现弹出框拖拽

主要应用jQuery的on和off方法实现在 mousedown 的情况下进行mousemove. 其他:$(window).width() .$(window).height() ---- 获取浏览页面的宽高. pageX.pagerY(ie8不兼容)或 clientX.clientY --------- 获取鼠标当前位置的横轴纵轴. offset().left.offset().top------------------------- div距离左边界的长度和上边界的长度. 下面是简易实现方