您现在的位置是:首页 > 技术教程 正文

HTML 常见的JS弹窗

admin 阅读: 2024-03-30
后台-插件-广告管理-内容页头部广告(手机)

一、最简单的 JavaScript 弹窗代码实例

HTML代码

  1. <button id="myBtn">打开弹窗button>
  2. <div id="myModal" class="modal">
  3. <div class="modal-content">
  4. <span class="close">×span>
  5. <p>弹窗中的文本...p>
  6. div>
  7. div>

CSS代码

  1. /* 弹窗 (background) */
  2. .modal {
  3. display: none; /* 默认隐藏 */
  4. position: fixed; /* 固定定位 */
  5. z-index: 1; /* 设置在顶层 */
  6. left: 0;
  7. top: 0;
  8. width: 100%;
  9. height: 100%;
  10. overflow: auto;
  11. background-color: rgb(0,0,0);
  12. background-color: rgba(0,0,0,0.4);
  13. }
  14. /* 弹窗内容 */
  15. .modal-content {
  16. background-color: #fefefe;
  17. margin: 15% auto;
  18. padding: 20px;
  19. border: 1px solid #888;
  20. width: 80%;
  21. }
  22. /* 关闭按钮 */
  23. .close {
  24. color: #aaa;
  25. float: right;
  26. font-size: 28px;
  27. font-weight: bold;
  28. }
  29. .close:hover,
  30. .close:focus {
  31. color: black;
  32. text-decoration: none;
  33. cursor: pointer;
  34. }

JS代码

  1. // 获取弹窗
  2. var modal = document.getElementById('myModal');
  3. // 打开弹窗的按钮对象
  4. var btn = document.getElementById("myBtn");
  5. // 获取 元素,用于关闭弹窗
  6. var span = document.querySelector('.close');
  7. // 点击按钮打开弹窗
  8. btn.onclick = function() {
  9. modal.style.display = "block";
  10. }
  11. // 点击 (x), 关闭弹窗
  12. span.onclick = function() {
  13. modal.style.display = "none";
  14. }
  15. // 在用户点击其他地方时,关闭弹窗
  16. window.onclick = function(event) {
  17. if (event.target == modal) {
  18. modal.style.display = "none";
  19. }
  20. }

展示效果

二、弹窗添加头部和底部

HTML代码

  1. <button id="myBtn">打开弹窗button>
  2. <div id="myModal" class="modal">
  3. <div class="modal-content">
  4. <div class="modal-header">
  5. <span class="close">×span>
  6. <h2>弹窗头部h2>
  7. div>
  8. <div class="modal-body">
  9. <p>弹窗文本...p>
  10. <p>弹窗文本...p>
  11. div>
  12. <div class="modal-footer">
  13. <h3>弹窗底部h3>
  14. div>
  15. div>
  16. div>

CSS代码

  1. /* 弹窗 (background) */
  2. .modal {
  3. display: none; /* 默认隐藏 */
  4. position: fixed;
  5. z-index: 1;
  6. padding-top: 100px;
  7. left: 0;
  8. top: 0;
  9. width: 100%;
  10. height: 100%;
  11. overflow: auto;
  12. background-color: rgb(0,0,0);
  13. background-color: rgba(0,0,0,0.4);
  14. }
  15. /* 弹窗内容 */
  16. .modal-content {
  17. position: relative;
  18. background-color: #fefefe;
  19. margin: auto;
  20. padding: 0;
  21. border: 1px solid #888;
  22. width: 80%;
  23. box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
  24. -webkit-animation-name: animatetop;
  25. -webkit-animation-duration: 0.4s;
  26. animation-name: animatetop;
  27. animation-duration: 0.4s
  28. }
  29. /* 添加动画 */
  30. @-webkit-keyframes animatetop {
  31. from {top:-300px; opacity:0}
  32. to {top:0; opacity:1}
  33. }
  34. @keyframes animatetop {
  35. from {top:-300px; opacity:0}
  36. to {top:0; opacity:1}
  37. }
  38. /* 关闭按钮 */
  39. .close {
  40. color: white;
  41. float: right;
  42. font-size: 28px;
  43. font-weight: bold;
  44. }
  45. .close:hover,
  46. .close:focus {
  47. color: #000;
  48. text-decoration: none;
  49. cursor: pointer;
  50. }
  51. .modal-header {
  52. padding: 2px 16px;
  53. background-color: #5cb85c;
  54. color: white;
  55. }
  56. .modal-body {padding: 2px 16px;}
  57. .modal-footer {
  58. padding: 2px 16px;
  59. background-color: #5cb85c;
  60. color: white;
  61. }

JS代码(JS代码一般用作弹窗的开关,功能点和普通弹窗相同)

  1. // 获取弹窗
  2. var modal = document.getElementById('myModal');
  3. // 打开弹窗的按钮对象
  4. var btn = document.getElementById("myBtn");
  5. // 获取 元素,用于关闭弹窗 that closes the modal
  6. var span = document.getElementsByClassName("close")[0];
  7. // 点击按钮打开弹窗
  8. btn.onclick = function() {
  9. modal.style.display = "block";
  10. }
  11. // 点击 (x), 关闭弹窗
  12. span.onclick = function() {
  13. modal.style.display = "none";
  14. }
  15. // 在用户点击其他地方时,关闭弹窗
  16. window.onclick = function(event) {
  17. if (event.target == modal) {
  18. modal.style.display = "none";
  19. }
  20. }

展示效果

三、在底部显示的弹窗

HTML代码

  1. <button id="myBtn">打开弹窗button>
  2. <div id="myModal" class="modal">
  3. <div class="modal-content">
  4. <div class="modal-header">
  5. <span class="close">×span>
  6. <h2>弹窗头部h2>
  7. div>
  8. <div class="modal-body">
  9. <p>弹窗文本...p>
  10. <p>弹窗文本...p>
  11. div>
  12. <div class="modal-footer">
  13. <h3>弹窗底部h3>
  14. div>
  15. div>
  16. div>

CSS代码

  1. /* 弹窗 */
  2. .modal {
  3. display: none; /* 默认隐藏 */
  4. position: fixed;
  5. z-index: 1;
  6. left: 0;
  7. top: 0;
  8. width: 100%;
  9. height: 100%;
  10. overflow: auto;
  11. background-color: rgb(0,0,0);
  12. background-color: rgba(0,0,0,0.4);
  13. -webkit-animation-name: fadeIn;
  14. -webkit-animation-duration: 0.4s;
  15. animation-name: fadeIn;
  16. animation-duration: 0.4s
  17. }
  18. /* 弹窗内容 */
  19. .modal-content {
  20. position: fixed;
  21. bottom: 0;
  22. background-color: #fefefe;
  23. width: 100%;
  24. -webkit-animation-name: slideIn;
  25. -webkit-animation-duration: 0.4s;
  26. animation-name: slideIn;
  27. animation-duration: 0.4s
  28. }
  29. /* 关闭按钮 */
  30. .close {
  31. color: white;
  32. float: right;
  33. font-size: 28px;
  34. font-weight: bold;
  35. }
  36. .close:hover,
  37. .close:focus {
  38. color: #000;
  39. text-decoration: none;
  40. cursor: pointer;
  41. }
  42. .modal-header {
  43. padding: 2px 16px;
  44. background-color: #5cb85c;
  45. color: white;
  46. }
  47. .modal-body {padding: 2px 16px;}
  48. .modal-footer {
  49. padding: 2px 16px;
  50. background-color: #5cb85c;
  51. color: white;
  52. }
  53. /* 添加动画 */
  54. @-webkit-keyframes slideIn {
  55. from {bottom: -300px; opacity: 0}
  56. to {bottom: 0; opacity: 1}
  57. }
  58. @keyframes slideIn {
  59. from {bottom: -300px; opacity: 0}
  60. to {bottom: 0; opacity: 1}
  61. }
  62. @-webkit-keyframes fadeIn {
  63. from {opacity: 0}
  64. to {opacity: 1}
  65. }
  66. @keyframes fadeIn {
  67. from {opacity: 0}
  68. to {opacity: 1}
  69. }

JS代码

  1. // 获取弹窗
  2. var modal = document.getElementById('myModal');
  3. // 打开弹窗的按钮对象
  4. var btn = document.getElementById("myBtn");
  5. // 获取 元素,用于关闭弹窗 that closes the modal
  6. var span = document.getElementsByClassName("close")[0];
  7. // 点击按钮打开弹窗
  8. btn.onclick = function() {
  9. modal.style.display = "block";
  10. }
  11. // 点击 (x), 关闭弹窗
  12. span.onclick = function() {
  13. modal.style.display = "none";
  14. }
  15. // 在用户点击其他地方时,关闭弹窗
  16. window.onclick = function(event) {
  17. if (event.target == modal) {
  18. modal.style.display = "none";
  19. }
  20. }

展示效果

第一次写文章,不会导入视频

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索
排行榜