当前位置: 首页 > article >正文

html+css+js实现Notification 通知

实现效果:

代码实现:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Notification 通知</title>
  <style>
    *{
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    .source{
      display: flex;
      margin-top: 50px;
      margin-left: 50px;
      width: 870px;
      height: 80px;
      padding: 24px;
      border: 1px solid #ecf5ff;
    }
    button{
      width: 110px;
      height:40px;
      margin-right: 20px;
      color: #606266;
      border: 1px solid #c0c4cc;
      background-color: #fff;
      border-radius: 3px;
    }
    button:hover{
      border: 1px solid #409eff;
      color:#409eff ;
    }
    button.active{
      border: 1px solid #409eff;
      color:#409eff ;
    }
    .modal-container {
      position: fixed;
      top: 20px;
      right: 20px;
      width: 330px;
    }
    .modal{
      width: 330px;
      border: 1px solid #c0c4cc;
      padding: 14px 24px 14px 18px;
      border-radius: 5px;
      margin-bottom: 14px;
      transform: translateX(350px);
      transition: transform 0.3s ease-in-out;
    }
    .open{
      transform: translateX(0);
    }
    .modal .header{
      display: flex;
      justify-content: space-between;
      font-weight: 700;
      font-size: 16px;
      color: #303133;
      padding-bottom: 5px;
    }
    i{
      cursor: pointer;
      font-style: normal;
      font-weight: 300;
      color: #606266;
    }
    .modal .footer{
      color: #606266;
      font-size: 14px;
    }
  </style>
</head>
<body>
  <div class="source">
    <button class="close">可自动关闭</button>
    <button class="unclose">不会自动关闭</button>
  </div>
  <div class="modal-container">
    <!-- 模态框将被动态添加到这个容器中 -->
    <!-- <div class="modal">
      <div class="header">标题名称<i>X</i></div>
      <div class="footer">这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案</div>
    </div> -->
  </div>
  <script>
    // 1.Modal构造函数封装模态框
    function Modal(title='',message=''){
      // console.log(title,message);
      // 创建modal模态框盒子
      // 1.1创建div标签
      this.modalBox=document.createElement('div')
      // 1.2给div标签添加modal类
      this.modalBox.className='modal'
      // 1.3给modal盒子内部填充两个div标签
      this.modalBox.innerHTML=`
      <div class="header">${title} <i>X</i></div>
      <div class="footer">${message} </div>
      `
      // console.log(this.modalBox);
    }
    new Modal('提示','这是一条不会自动关闭的消息')

    // 2.给构造函数原型对象身上挂载open方法
    Modal.prototype.open=function(){
      document.querySelector('.modal-container').append(this.modalBox);
      setTimeout(() => {
        this.modalBox.classList.add('open');
      }, 300);
      // 点击关闭
      this.closeBtn=this.modalBox.querySelector('.close-btn');
      this.modalBox.querySelector('i').addEventListener('click', () => {
        this.close();
      });
    }

    // close方法
    Modal.prototype.close=function(){
      this.modalBox.classList.remove('open');
      setTimeout(() => {
        this.modalBox.remove();
      }, 300); // 等待动画完成后移除元素
    }
    // 绑定点击事件
    document.querySelector('.close').addEventListener('click', function() {
      const modal = new Modal('标题名称','这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案');
      modal.open();
      // 设置模态框在3秒后自动关闭
      setTimeout(() => {
        modal.close();
      }, 3000);
    });
    document.querySelector('.unclose').addEventListener('click',function(){
      const unclose=new Modal('提示','这是一条不会自动关闭的消息')
      unclose.open()
    })
  </script>
</body>
</html>

 


http://www.kler.cn/news/365182.html

相关文章:

  • sql-labs靶场第二十关测试报告
  • DirectDraw和Direct3D的区别
  • C语言初阶七:C语言操作符详解(1)
  • 三数之和(15)
  • Python入门(二)编程中的“真”与“假”,单双向选择的判断
  • 【C++】C++11基础入门
  • 洞察云上风险,主机安全尽在掌握
  • 【LeetCode】1297、子串的最大出现次数
  • AI 开启财富密码:探索多元化赚钱之路
  • 【PyTorch][chapter31][transformer-4]
  • sharpkeys-键盘部分按键不好用,用其它不常用按键代替
  • 【机器学习】13. 决策树
  • 现代Web界面交互新利器!来探一探这个魔法组件库——MagicUI
  • 【SPIE出版,EI检索稳定】2024年人机交互与虚拟现实国际会议(HCIVR 2024,11月15-17日)
  • 系统架构设计师教程 第18章18.8 安全架构设计案例分析 笔记
  • Android修改第三方应用相机方向
  • paddleocr使用FastDeploy 部署工具部署 rknn 模型
  • 250MS/s 4通道16bit PCIE采集卡
  • 【YOLOv11改进[损失函数]】使用结合InnerIoU和Focaler的各种损失函数助力YOLOv11更优秀
  • Xshell远程连接工具详解
  • 什么是标准差?详解
  • Android Kotlin中协程详解
  • docker安装postgres扩展age以及使用nodejs连接
  • TCP单包数据大于1460字节会被拆包的问题
  • Python项目引入其他项目作为子模块
  • 漏洞技术分析实践_整数溢出