当前位置:首页 > JavaScript

js实现toast

2026-04-06 16:29:41JavaScript

实现Toast通知的基本方法

使用纯JavaScript实现Toast通知,可以通过动态创建DOM元素并添加动画效果来完成。以下是一个基础实现示例:

js实现toast

function showToast(message, duration = 3000) {
  const toast = document.createElement('div');
  toast.className = 'toast-notification';
  toast.textContent = message;

  document.body.appendChild(toast);

  setTimeout(() => {
    toast.classList.add('show');
  }, 10);

  setTimeout(() => {
    toast.classList.remove('show');
    setTimeout(() => {
      document.body.removeChild(toast);
    }, 300);
  }, duration);
}

配套CSS样式

Toast通知需要基本的样式来确保美观和动画效果:

js实现toast

.toast-notification {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: #333;
  color: white;
  padding: 12px 24px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.toast-notification.show {
  opacity: 1;
}

进阶实现方案

对于更复杂的Toast系统,可以考虑以下增强功能:

class Toast {
  constructor() {
    this.container = document.createElement('div');
    this.container.className = 'toast-container';
    document.body.appendChild(this.container);
  }

  show(message, options = {}) {
    const { duration = 3000, type = 'default' } = options;
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.textContent = message;

    this.container.appendChild(toast);
    setTimeout(() => toast.classList.add('show'), 10);

    setTimeout(() => {
      toast.classList.remove('show');
      setTimeout(() => toast.remove(), 300);
    }, duration);
  }
}

// 使用示例
const toast = new Toast();
toast.show('操作成功', { type: 'success', duration: 2000 });

配套增强CSS

.toast-container {
  position: fixed;
  bottom: 20px;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  z-index: 1000;
}

.toast {
  margin-bottom: 10px;
  padding: 12px 24px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  transform: translateY(20px);
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast-default {
  background-color: #333;
  color: white;
}

.toast-success {
  background-color: #4CAF50;
  color: white;
}

.toast-error {
  background-color: #F44336;
  color: white;
}

注意事项

  • 确保Toast不会影响页面其他元素的交互
  • 考虑移动端适配,可能需要调整位置和大小
  • 对于频繁触发的Toast,需要管理队列避免重叠
  • 添加无障碍访问属性如role="alert"提高可访问性

以上实现方案可以根据具体需求进行调整和扩展,如添加图标、进度条或交互按钮等高级功能。

标签: jstoast
分享给朋友:

相关文章

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swipe…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js实现求导

js实现求导

实现数值求导的方法 在JavaScript中实现求导通常采用数值方法,因为JavaScript不是符号计算语言。以下是常见的数值微分方法: 中心差分法 中心差分法提供较高精度的导数近似: func…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…