jQuery构建最小Toast消息脚本

  • 源码大小:7.68KB
  • 所需积分:1积分
  • 源码编号:19JP-3477
  • 浏览次数:515次
  • 最后更新:2023年06月17日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

一个小的jQuerytoast通知脚本,它在用户当前视图下方向用户显示一条快速消息,只持续几秒钟,或者直到用户手动关闭它。

如何使用它:

1.在页面中添加吐司信息。

  1. <div class="toast-container toast-pos-right toast-pos-bottom">
  2.  
  3. <!-- Toast -->
  4. <div class="toast" id="toast-name-1">
  5. <a href="#" class="close-toast">&#10006;</a>
  6. <b>Messege 1!</b> Lorem ipsum dolor sit amet, consectetur adipisicing elit.
  7. </div>
  8. <!-- Toast -->
  9. <div class="toast" id="toast-name-2">
  10. <a href="#" class="close-toast">&#10006;</a>
  11. <b>Messege 2!</b> Lorem ipsum dolor sit amet, consectetur adipisicing elit.
  12. </div>
  13. </div>

2.创建触发器来切换toast消息。

  • 使用“toast auto”类强制自动淡出通知。
  • 使用“toast-pos-xxxxx”类将“toast”容器放置在您想要的任何位置。
  1. <a href="#" class="toast-trigger" data-toast="toast-name-1">
  2. Normal Toast
  3. </a>
  4.  
  5. <a href="#" class="toast-trigger toast-auto" data-toast="toast-name-2">
  6. Auto FadeOut Toast
  7. </a>

3.toast消息所需的CSS样式。

  1. .toast-container {
  2. width: 90%;
  3. max-width: 580px;
  4. margin: 0 auto;
  5. }
  6.  
  7. [class*="toast-pos-"] {
  8. position: absolute;
  9. padding: 10px;
  10. }
  11.  
  12. .toast-pos-top {
  13. top: 0;
  14. }
  15.  
  16. .toast-pos-right {
  17. right: 0;
  18. }
  19.  
  20. .toast-pos-bottom {
  21. bottom: 0;
  22. }
  23.  
  24. .toast-pos-left {
  25. left: 0;
  26. }
  27.  
  28. .toast {
  29. display: none;
  30. padding: 20px;
  31. margin: 20px 0;
  32. background: #eeeeee;
  33. color: #333333;
  34. }
  35.  
  36. .close-toast {
  37. float: right;
  38. text-decoration: none;
  39. color: #ffffff;
  40. vertical-align: middle;
  41. }

4.启用toast消息的主脚本。在最新的jQuery库之后复制并插入以下代码段。就是这样。

  1. <script src="/path/to/cdn/jquery.min.js"></script>

5.启用toast消息的主脚本。在最新的jQuery库之后复制并插入以下代码段。就是这样。

  1. $(".toast-trigger").click(function(e){
  2. e.preventDefault();
  3. datatoast = $(this).attr("data-toast");
  4. if ( $( this ).hasClass( "toast-auto" ) && !$("#" + datatoast).is(":visible") ){
  5. $("#" + datatoast).fadeIn(400).delay(2000).fadeOut(400);
  6. }
  7. else if ( !$("#" + datatoast).is(":visible") ){
  8. $("#" + datatoast).fadeIn(400);
  9. };
  10. });
  11.  
  12. $(".close-toast").click(function(e){
  13. e.preventDefault();
  14. closetoast = $(this).parent().attr("id");
  15. $("#" + closetoast).fadeOut(400);
  16. });

预览截图