【问题标题】:Modal not displaying with bulma css模态不显示与bulma css
【发布时间】:2018-04-28 22:33:10
【问题描述】:

我想在单击按钮但不工作时显示模式。代码如下:

    <button class="button is-warning is-pulled-right" onclick="refs.modalEdicion.open()">
        <span>Editar</span>
    </button>
    <div class="modal" id="modalEdicion">
        <div class="modal-background"></div>
        <div class="modal-content">
            <p class="image is-4by3">
            <img src="https://bulma.io/images/placeholders/1280x960.png" alt="">
            </p>
        </div>
        <button class="modal-close is-large" aria-label="close"></button>
    </div>

【问题讨论】:

    标签: html css bulma


    【解决方案1】:

    在开始之前,打开modal的简单方法应该是这样的;

    yourElem.classList.toggle('is-active')
    

    在我的项目中,我有很多模态。所以我并不总是想像上面那样使用。因此,我创建了一个基本的模态事件侦听器。我知道这对你来说还不够。因为打开和关闭modals还会有其他情况。

    在这种情况下,您可以打开和关闭模态框,甚至可以收听显示和关闭事件。

    我使用this Mozilla 资源来创建自定义事件。例如,您要创建两个名为 modal:showmodal:close 的事件。为此,您应该编写如下代码:

    演出活动

    var event = new Event('modal:show')
    
    yourElem.dispatchEvent(event);
    

    关闭事件

    var event = new Event('modal:close')
    
    yourElem.dispatchEvent(event);
    

    现在,我们可以监听上面的事件了。

    监听显示事件的示例

    yourElem.addEventListener('modal:show', function() {
        console.log("opened")
    })
    

    监听关闭事件的示例

    yourElem.addEventListener("modal:close", function() {
        console.log("closed")
    })
    

    我们知道如何从简单的方式部分打开和关闭模式。但有时用户可以点击模态背景或“X”取消按钮。如果是这样,我们需要处理这些事件。为此,我们可以使用此代码

    var modalClose = yourelem.querySelectorAll("[data-bulma-modal='close'], 
      .modal-background")
    
    modalClose.forEach(function(e) {
        e.addEventListener("click", function() {
    
            yourelem.classList.toggle('is-active')
    
                var event = new Event('modal:close')
    
                yourelem.dispatchEvent(event);
            })
    })
    

    就是这样。我们知道如何打开或关闭 Bulma 模式。甚至我们也可以收听他们的节目并关闭活动。让我们简单一点

    创建一个 BulmaModal 类

    class BulmaModal {
        constructor(selector) {
            this.elem = document.querySelector(selector)
            this.close_data()
        }
    
        show() {
            this.elem.classList.toggle('is-active')
            this.on_show()
        }
    
        close() {
            this.elem.classList.toggle('is-active')
            this.on_close()
        }
    
        close_data() {
            var modalClose = this.elem.querySelectorAll("[data-bulma-modal='close'], 
            .modal-background")
    
            var that = this
            modalClose.forEach(function(e) {
                e.addEventListener("click", function() {
    
                    that.elem.classList.toggle('is-active')
    
                    var event = new Event('modal:close')
    
                    that.elem.dispatchEvent(event);
                })
            })
        }
    
        on_show() {
            var event = new Event('modal:show')
    
            this.elem.dispatchEvent(event);
        }
    
        on_close() {
            var event = new Event('modal:close')
    
            this.elem.dispatchEvent(event);
        }
    
        addEventListener(event, callback) {
            this.elem.addEventListener(event, callback)
        }
    }
    

    用法

    var btn = document.querySelector("#btn")
    var mdl = new BulmaModal("#myModal")
    
    btn.addEventListener("click", function () {
        mdl.show()
    })
    
    mdl.addEventListener('modal:show', function() {
        console.log("opened")
    })
    
    mdl.addEventListener("modal:close", function() {
        console.log("closed")
    })
    

    让我们看看这个简单的sn-p

    class BulmaModal {
    	constructor(selector) {
    		this.elem = document.querySelector(selector)
    		this.close_data()
    	}
    	
    	show() {
    		this.elem.classList.toggle('is-active')
    		this.on_show()
    	}
    	
    	close() {
    		this.elem.classList.toggle('is-active')
    		this.on_close()
    	}
    	
    	close_data() {
    		var modalClose = this.elem.querySelectorAll("[data-bulma-modal='close'], .modal-background")
    		var that = this
    		modalClose.forEach(function(e) {
    			e.addEventListener("click", function() {
    				
    				that.elem.classList.toggle('is-active')
    
    				var event = new Event('modal:close')
    
    				that.elem.dispatchEvent(event);
    			})
    		})
    	}
    	
    	on_show() {
    		var event = new Event('modal:show')
    	
    		this.elem.dispatchEvent(event);
    	}
    	
    	on_close() {
    		var event = new Event('modal:close')
    	
    		this.elem.dispatchEvent(event);
    	}
    	
    	addEventListener(event, callback) {
    		this.elem.addEventListener(event, callback)
    	}
    }
    
    var btn = document.querySelector("#btn")
    var mdl = new BulmaModal("#myModal")
    
    btn.addEventListener("click", function () {
    	mdl.show()
    })
    
    mdl.addEventListener('modal:show', function() {
    	console.log("opened")
    })
    
    mdl.addEventListener("modal:close", function() {
    	console.log("closed")
    })
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css" rel="stylesheet"/>
    
    <div class="modal" id="myModal">
      <div class="modal-background"></div>
      <div class="modal-card">
        <header class="modal-card-head">
          <p class="modal-card-title">Modal title</p>
          <button class="delete" aria-label="close" data-bulma-modal="close"></button>
        </header>
        <section class="modal-card-body">
          <p>There is something here</p>
        </section>
        <footer class="modal-card-foot">
          <button class="button is-success">Save changes</button>
          <button class="button" data-bulma-modal="close">Cancel</button>
        </footer>
      </div>
    </div>
    
    <button id="btn">Click active Modal</button>

    我希望这个答案对布尔玛新手有所帮助。

    【讨论】:

      【解决方案2】:

      Bulma CSS 是一个纯 CSS 框架,所有的 javascript 行为都必须手动编写。这意味着对于模态框,所有用于隐藏和显示模态框的 CSS 类都已编写,您只需正确绑定事件即可。如果您访问 Modal 文档页面 (https://bulma.io/documentation/components/modal/),您会看到 No Javascript 警告说明

      Bulma 不包含任何 JavaScript 交互。你不得不 自己实现类切换。

      只需定义 refs.modalEdicion.open 函数以根据文档添加类 is-active 并在关闭按钮上绑定事件以删除相同的 CSS 类。如果您想通过单击叠加层来关闭模式,您可能还希望将事件绑定到叠加层元素。

      这是所需的实现。 https://codepen.io/anon/pen/KRaqxG

      【讨论】:

        【解决方案3】:

        本周我遇到了这个问题,我发现了这个link。它包含官方(据此)Bulma Modal Doc 页面的 javascript 代码。我将它复制并减少了一两行,它适用于代码中的所有 bulma 模态。

        请注意,这是非常开放的代码。 Ali 的回答是理想的选择,但如果您不想花时间为模式编写代码,那么只需在代码中复制此段即可。

        document.addEventListener('DOMContentLoaded', function () {
        
            // Modals
        
            var rootEl = document.documentElement;
            var allModals = getAll('.modal');
            var modalButtons = getAll('.modal-button');
            var modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
        
            if (modalButtons.length > 0) {
                modalButtons.forEach(function (el) {
                    el.addEventListener('click', function () {
                        var target = document.getElementById(el.dataset.target);
                        rootEl.classList.add('is-clipped');
                        target.classList.add('is-active');
                    });
                });
            }
        
            if (modalCloses.length > 0) {
                modalCloses.forEach(function (el) {
                    el.addEventListener('click', function () {
                        closeModals();
                    });
                });
            }
        
            document.addEventListener('keydown', function (event) {
                var e = event || window.event;
                if (e.keyCode === 27) {
                    closeModals();
                }
            });
        
            function closeModals() {
                rootEl.classList.remove('is-clipped');
                allModals.forEach(function (el) {
                    el.classList.remove('is-active');
                });
            }
        
            // Functions
        
            function getAll(selector) {
                return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
            }
        });
        

        【讨论】:

          【解决方案4】:

          好的,首先,您需要发布您的 javascript 和 css 以获得相关答案,但我将概述我这样做的方式。

          你可以像下面这样实现:

          //One Modal
          function OpenModal() {
              //Get element with Id= "modal"
              var modal = document.getElementById("modal");
              //Change style to display = "block"
              modal.style.display = "block";
          }
          
          //Multiple Modals
          function OpenMore(n) {
              //Get elements with class="modal" into an array
              var modal = document.getElementsByClassName("modal");
              //Change style of modal number [n] to display = "block"
              modal[n].style.display = "block";
          }
          
          //This will close the modal once you click on it
          window.onclick = function(event) {
          
              //For single modal
              var modal = document.getElementById("modal");
              //If the click was on the modal the modal style display = "none"
              if (event.target == modal) {
                  modal.style.display = "none";
              }
          
              //For multiple modals
              var more = document.getElementsByClassName("modal");
              //i represents which modal. It will go through all modals
              for (var i = 0; i < more.length; i++) {
                  //If the click was on the modal for one of the modals display = "none"
                  //for all of them
                  if (event.target == more[i]) {
                      more[i].style.display = "none";
                  }
              }
          }
          .modal {
              display: none;
              background-color: yellow;
              height: 100vh;
              width: 100vw;
              position: fixed;
              top: 0;
              left: 0;
          }
          #modal {
              display: none;
              background-color: yellow;
              height: 100vh;
              width: 100vw;
              position: fixed;
              top: 0;
              left: 0;
          }
          .button {
              margin: 50px auto;
          }
          <!-- For One Modal --> 
          <button class="button" onclick="OpenModal()"> SingleModal </button>
          <div id="modal"> hardidar </div>
          
          <!-- For Multiple Modals -->
          <button class="button" onclick="OpenMore(0)"> MultipleModal1 </button>
          <div class="modal"> 1st Modal </div>
          
          <button class="button" onclick="OpenMore(1)"> MultipleModal2 </button> 
          <div class="modal"> 2nd Modal </div>

          这个想法是,根据 css 规则,模态的初始显示是 display: none,一旦你点击按钮,Javascript 方法就会运行并将其更改为 display:block

          你可以改变这个行为来做很多事情。您可以切换课程,您可以更改transform: scale(),这是我的个人偏好。

          这个例子是一个有效的例子。

          【讨论】:

            【解决方案5】:

            试试这个代码

            $(document).ready(function() {
               $("#your_id_button").click(function() {
            
                 $("#id_modal").addClass("is-active"); // modal is open
            
               });
            
               $("#your_id_button_close").click(function() {
            
                 $("#id_modal").removeClass("is-active"); // modal is close
            
               });
            
            });`
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-11-27
              • 1970-01-01
              • 2021-08-25
              • 1970-01-01
              • 2023-02-01
              • 2018-02-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多