简体   繁体   English

我如何获得多个模态?

[英]How can i get multiple modals?

I am using a loop to generate elements from a YML file, and for each element I want to be able to click on them and have different content outlined in the YML file. 我正在使用循环从YML文件生成元素,对于每个元素,我希望能够单击它们并在YML文件中概述不同的内容。 However when clicking on the first element it works, but when clicking on any other element in the page the modal no longer even appears. 但是,当单击第一个元素时,它起作用,但是,当单击页面中的任何其他元素时,模态甚至不再出现。

{% for company in site.data.stories %}
    <div class="col-md-6 col-sm-6  col-xs-12 company" style="height: 600px;">
        <div class="img-container">

            <!-- Trigger/Open The Modal -->
            <button id="myBtn">
            <img class="center" style="width: 486px; height: 364.5px;" id="{{company.image_url}}" src="/assets/img/stories/{{company.image_url}}.png" alt="{{company.name}}"></button>

            <!-- The Modal -->
            <div id="myModal" class="modal">
            <!-- Modal content -->
            <div class="modal-content">
            <div class="modal-header">
                <span class="close">&times;</span>
                <h2>{{company.name}}</h2>
            </div>
            <div class="modal-body">
                <strong>Date of Event: </strong>{{company.date_of_event}}
                <p>{{company.fullstory}}</p>
            </div>
            <div class="modal-footer">
            <h3>The End</h3>
            </div>
            </div>
            </div>
        </div>

            <h3><a href="{{company.site_url}}" class="center">{{company.name}}</a></h3> 
            <p><b>Date of Event: </b> {{company.date_of_event}}</p>
            <p>{{company.description}}</p>
    </div>
    {% endfor %}

Java Script: Java脚本:

<script> // Modal Script
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal 
btn.onclick = function() {modal.style.display = "block";}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {modal.style.display = "none";}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {if (event.target == modal) {modal.style.display = "none";}}
</script>

Each element in the YML file is outlined like this: YML文件中的每个元素的轮廓如下:

name: The Delgado Household Story
  description: The Joy Project helps a single mother of eight with time and donations.
  image_url: delgado_photo2
  site_url: #
  date_of_event: Coming Soon...
  width: 90
  margin: 5
  fullstory: 

You will have to rename each modal ID something different, right now they are all being dynamically generated with ID myModal . 您将不得不将每个模式ID重命名为不同的名称,现在它们都是使用ID myModal动态生成的。 Also the buttons that trigger each modal will have to know which modal to fire also. 同样,触发每个模式的按钮也必须知道要触发哪个模式。 You could set the ID's of the modal to something specific to each set of data, like the name or add a unique ID property to each record. 您可以将模式的ID设置为特定于每组数据的name ,例如name或为每个记录添加唯一的ID属性。

To save loading a bunch of modals... Instead of dynamically creating the modals based off of your data (reuse of the ID myModal ), it may be a good idea to try and reuse a single modal and dynamically add the content to that modal depending on the content clicked. 为了节省加载一堆模态...而不是根据数据动态创建模态(重复使用ID myModal ),尝试重用单个模态并将内容动态添加到该模态可能是个好主意根据点击的内容。 You could still load all the company data one-by-one (leave the modal out of the for loop in html), then when the modal button is clicked, inject the rest of the data to be displayed inside the modal. 您仍然可以一一加载所有公司数据(将模式从html的for循环中移出),然后单击模式按钮时,将其余要显示的数据注入模式中。

{% for company in site.data.stories %}
<div class="col-md-6 col-sm-6  col-xs-12 company" style="height: 600px;">
    <div class="img-container">

        <!-- Trigger/Open The Modal -->
        <button id="{{company.uniqueID}}">
        <img class="center" style="width: 486px; height: 364.5px;" id="{{company.image_url}}" src="/assets/img/stories/{{company.image_url}}.png" alt="{{company.name}}"></button>
    </div>

        <h3><a href="{{company.site_url}}" class="center">{{company.name}}</a></h3> 
        <p><b>Date of Event: </b> {{company.date_of_event}}</p>
        <p>{{company.description}}</p>
</div>
{% endfor %}


<!-- The Modal -->
<div id="myModal" class="modal">
    <!-- Modal content -->
    <div class="modal-content">
        <div class="modal-header">
            <span class="close">&times;</span>
            <h2>{{company.name}}</h2>
        </div>
        <div class="modal-body">
            <strong>Date of Event: </strong>{{company.date_of_event}}
            <p>{{company.fullstory}}</p>
        </div>
        <div class="modal-footer">
            <h3>The End</h3>
        </div>
    </div>
</div>

Note: You might have to think about how to structure the data differently. 注意:您可能必须考虑如何以不同的方式构造数据。 Maybe giving each button a unique ID and fetching the data related to that unique ID, <button id="{{company.uniqueID}}"> . 也许给每个按钮一个唯一的ID,然后获取与该唯一ID相关的数据<button id="{{company.uniqueID}}">

You could also set the ID fetching based off of the name "The Delgado Household Story", etc... <button id="{{company.name}}"> , then inject based off of the button ID, something like get button ID name, find and match name in dataset, then set modal data based off of properties in that 'name' 您还可以根据名称“ The Delgado Household Story”等设置ID提取,... <button id="{{company.name}}"> ,然后根据按钮ID进行注入,例如get button ID name, find and match name in dataset, then set modal data based off of properties in that 'name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM