博客
关于我
js实现模态框拖拽
阅读量:126 次
发布时间:2019-02-27

本文共 1961 字,大约阅读时间需要 6 分钟。

效果展示和需求分析

效果展示展示了一个完整的登录模态框界面,其中包括登录表单、背景遮罩和关闭按钮。模态框支持以下功能:

  • 点击登录按钮后,登录表单和遮罩层会同时显示
  • 点击关闭按钮后,表单和遮罩层会隐藏
  • 密码输入时支持明文查看或隐藏
  • 表单标题可以通过拖拽移动
  • 鼠标弹起后拖拽结束
  • 需求分析系统需求如下:

  • 登录后显示登录表单和遮罩层
  • 支持明文和隐藏密码显示切换
  • 允许用户拖拽表单标题区域
  • 拖拽结束后自动调整表单位置
  • 代码分析HTML代码

    账号登录

    关闭

    JS代码

    var eyeState = document.querySelector('#eye-state');var pswInput = document.querySelector('#psw');var login = document.querySelector('.login');var loginBg = document.querySelector('#bg');var loginForm = document.querySelector('form');var closeBtn = document.querySelector('.close-btn');var eyeFlag = 0;eyeState.onclick = setEye;login.onclick = goLogin;closeBtn.onclick = leaveLogin;loginForm.children[0].addEventListener('mousedown', dragForm);loginForm.onselectstart = function(e) {    e.preventDefault();};function setEye() {    if (!eyeFlag) {        eyeState.className = 'open';        pswInput.type = 'text';        eyeFlag = 1;    } else {        eyeState.className = 'close';        pswInput.type = 'password';        eyeFlag = 0;    }}function goLogin() {    loginBg.style.visibility = 'visible';    loginForm.style.display = 'block';    login.style.display = 'none';}function leaveLogin() {    loginBg.style.visibility = 'hidden';    loginForm.style.display = 'none';    login.style.display = 'block';}function dragForm(e) {    var x = e.pageX - this.parentNode.offsetLeft;    var y = e.pageY - this.parentNode.offsetTop;    document.addEventListener('mousemove', move);    this.addEventListener('mouseup', function() {        document.removeEventListener('mousemove', move);    });    function move(event) {        loginForm.style.left = event.pageX - x + 'px';        loginForm.style.top = event.pageY - y + 'px';    }}

    分析

  • 密码输入框通过 eyeFlag 变量控制 type 属性和小眼睛图标状态
  • 拖拽效果通过 mousemove 事件绑定给 document 实现
  • mouseup 事件绑定给标题元素确保拖拽结束时移除事件
  • 表单标题区域通过 mousedown 事件实现拖拽效果
  • 密度跟随鼠标实现基于鼠标位置的动态调整
  • 转载地址:http://neqf.baihongyu.com/

    你可能感兴趣的文章
    Node出错导致运行崩溃的解决方案
    查看>>
    node安装及配置之windows版
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    NOIp2005 过河
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>