如何在 CSS 中设计通知容器?

做过

我有一项任务 - 为某些 Web 应用程序制作通知显示。通知出现在窗口的右下角,并在一段时间后消失。或者,可以通过单击通知项来消除通知。如果有多个通知,则应将它们堆叠起来。每个新通知都会将通知堆栈向上移动。如果通知堆栈的增长大于窗口的高度,则顶部较旧的通知必须移动到下一个堆栈,该堆栈必须显示在较新的通知堆栈的左侧。同样,如果第二个堆栈达到窗口的高度,那么最旧的通知应该移动到第三个堆栈。

从示意图上看,它应该如下所示:通知堆栈

我能够获得显示通知的正确顺序,但我无法将通知容器定位在窗口的右边缘。

function notify(msg) {
  let element = document.createElement('div');
  element.classList.add('notification');
  element.innerHTML = msg;
  let timeout;
  element.onclick = () => element.remove();
  element.onmouseenter = () => clearTimeout(timeout);
  element.onmouseleave = () => createTimeout();
  createTimeout();
  let recentElement = container.firstElementChild;
  if(recentElement) recentElement.before(element);
  else container.appendChild(element);
  indicator.innerHTML='Last msg is '+msg;
  function createTimeout() {
    timeout = setTimeout(() => { 
      element.remove() 
    }, 10000);
  }
}

let notifyIndex = 0;

x1.onclick = () => {
  notify(++notifyIndex);
}
x10.onclick = () => {
  for (let i = 0; i < 10; i++)
    notify(++notifyIndex);
};
x30.onclick = () => {
  for (let i = 0; i < 30; i++)
    notify(++notifyIndex);
};
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
}
.action-list button {
  width: 4rem;
  margin .5rem
}
#container {
  position: fixed;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-end;
  flex-wrap: wrap-reverse;
  align-content: flex-end;
  align-items: flex-end;
  bottom: 0;
  right: 30px;
  max-height: 100vh;
}
.notification {
  transition: all 500ms ease-in-out;
  box-shadow: 0 0 1rem #0004;
  margin: 0 1rem 1rem 0;
  background: #f9f99f;
  color: #000;
  border: 1px solid #0008;
  padding: .2rem 1rem;
  cursor: default;
  user-select: none;
}
<div id="container"></div>

<div>
<div id="indicator">Last msg is 0</div>
<div class="action-list">
  <button id="x1">x1</button>
  <button id="x10">x10</button>
  <button id="x30">x30</button>
</div>
</div>

我设置了 30 像素的缩进,这样你就可以看到通知越过了窗口的边缘。这个想法是最近的通知堆栈不应该越过窗口的边缘。

我究竟做错了什么?

曼努埃尔·科雷亚
position: fixed;
display: flex;
flex-direction: column-reverse;
justify-content: flex-end;
flex-wrap: wrap-reverse;
align-items: flex-end;
bottom: 0;
right: 30px;
max-height: 100vh;

试试这个,让我知道它是否有效!

Tldr:您的 Align-content: flex-end 使其向右增长

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章