自定义点击弹出框

自定义点击弹出框

html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>自定义弹出框按钮示例</title>
<link rel="stylesheet" href="1.css">
</head>
<body>

<button id="myButton">点击我</button>

<div id="notification-container"></div>

<script src="1.js"></script>
<script>
    // 添加点击事件监听器到按钮
document.getElementById('myButton').addEventListener('click', function() {
  // 调用openNotification函数来展示自定义弹出框
  openNotification('success', '通知', '你点击了,哈哈', 1000); // 0 代表不自动关闭弹出框
});
</script>
</body>
</html>

右边弹出框

@keyframes slideOutRight {
    0% {
      opacity: 1;
      transform: translateX(0);
    }
  
    100% {
      opacity: 0;
      transform: translateX(100%);
    }
  }
  
  .notification-box.fadeOut {
    animation: slideOutRight 0.5s forwards;
  }
/* 弹出框的容器样式 */
#notification-container {
    position: fixed;
    top: 10px;
    right: 10px;
    width: 300px;
  }
  
  /* 弹出框通用样式 */
  .notification-box {
    background-color: #f2f2f2; /* 设置背景色 */
    border: 1px solid #ccc; /* 设置边框 */
    border-radius: 30px; /* 设置圆角 */
    padding: 20px;
    margin-bottom: 10px;
    color: #333;
    box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.2);
  }
  
  .notification-box h2 {
    margin-top: 0;
    font-weight: bold;
    font-size: 18px;
    text-align: center;
  }
  
  .notification-box p {
    font-size: 14px;
    margin: 15px 0;
    text-align: center;
  }
  
  .close-button {
    cursor: pointer;
    position: absolute;
    top: 5px;
    right: 5px;
    background-color: transparent; /* 将背景色设置为透明 */
    border: none;
    width: 24px;
    height: 24px;
    font-size: 18px;
    text-align: center;
    line-height: 22px; /* 设置行高,使X居中显示 */
    font-weight: bold;
    color: #999; /* 设置关闭按钮颜色 */
    transition: all 0.3s; /* 添加过渡效果 */
  }
  
  .close-button:hover {
    color: #333; /* 设置鼠标悬停时的关闭按钮颜色 */
  }
  

左边弹出框

@keyframes slideOutLeft {
    0% {
      opacity: 1;
      transform: translateX(0);
    }
  
    100% {
      opacity: 0;
      transform: translateX(-100%);
    }
}

.notification-box.fadeOut {
    animation: slideOutLeft 0.5s forwards;
}

/* 弹出框的容器样式 */
/* @keyframes slideOutRight {
    0% {
      opacity: 1;
      transform: translateX(0);
    }
  
    100% {
      opacity: 0;
      transform: translateX(100%);
    }
  }
  
  .notification-box.fadeOut {
    animation: slideOutRight 0.5s forwards;
  } */
/* 弹出框的容器样式 */
#notification-container {
    position: fixed;
    top: 70px;
    left: 10px;
    width: 300px;
  }
  
  .notification-box {
    background-color: #e7f0ff; /* 浅蓝色背景 */
    border: 1px solid #3498db; /* 深蓝色边框 */
    border-radius: 10px; /* 设置圆角 */
    padding: 15px;
    margin-bottom: 20px;
    color: #2980b9; /* 深蓝色文字 */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    font-family: Arial, sans-serif; /* 设置字体 */
    font-size: 14px; /* 设置字体大小 */
    line-height: 1.5; /* 设置行高 */
}


  
  .notification-box h2 {
    margin-top: 0;
    font-weight: bold;
    font-size: 18px;
    text-align: center;
  }
  
  .notification-box p {
    font-size: 14px;
    margin: 15px 0;
    text-align: center;
  }
  
  .close-button {
    cursor: pointer;
    position: absolute;
    top: 5px;
    right: 5px;
    background-color: transparent; /* 将背景色设置为透明 */
    border: none;
    width: 24px;
    height: 24px;
    font-size: 18px;
    text-align: center;
    line-height: 22px; /* 设置行高,使X居中显示 */
    font-weight: bold;
    color: #999; /* 设置关闭按钮颜色 */
    transition: all 0.3s; /* 添加过渡效果 */
  }
  
  .close-button:hover {
    color: #333; /* 设置鼠标悬停时的关闭按钮颜色 */
  }
  

js逻辑

// 打开一个通知
function openNotification(type, title, message, duration) {
    // 创建通知元素
    var notification = document.createElement('div');
    notification.className = 'notification-box';
    // 根据类型添加图标HTML
    var iconHTML = '';
/*     switch (type) {
      case 'success':
        iconHTML = '<img src="success-icon.png" alt="Success" />';
        break;
      case 'warning':
        iconHTML = '<img src="warning-icon.png" alt="Warning" />';
        break;
      case 'info':
        iconHTML = '<img src="info-icon.png" alt="Info" />';
        break;
      case 'error':
        iconHTML = '<img src="error-icon.png" alt="Error" />';
        break;
      // 默认不添加图标
      default:
        iconHTML = '';
    } */
    notification.innerHTML = `
      ${iconHTML}
      <h2>${title}</h2>
      <p>${message}</p>
      <button class="close-button" onclick="closeNotification(this)">×</button>
    `;
    
    // 添加到容器
    var container = document.getElementById('notification-container');
    container.appendChild(notification);
  
    // 设置自动关闭
    if (duration > 0) {
      setTimeout(function() {
        closeNotification(notification);
      }, duration);
    }
  }
  
  // 关闭一个通知
  function closeNotification(notification) {
    // 如果notification是button,那么取其parentNode
    if (notification.nodeName === 'BUTTON') {
      notification = notification.parentNode;
    }
    // 开始退场动画
    notification.classList.add('fadeOut');
    // 监听动画结束事件,然后移除通知
    notification.addEventListener('animationend', function() {
      notification.remove();
    });
  }


  
  

左边弹出显示

在这里插入图片描述

右边弹出显示

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elik-hb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值