[英]How can i get multiple modals?
我正在使用循环从YML文件生成元素,对于每个元素,我希望能够单击它们并在YML文件中概述不同的内容。 但是,当单击第一个元素时,它起作用,但是,当单击页面中的任何其他元素时,模态甚至不再出现。
{% 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">×</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> // 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>
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:
您将不得不将每个模式ID重命名为不同的名称,现在它们都是使用ID myModal
动态生成的。 同样,触发每个模式的按钮也必须知道要触发哪个模式。 您可以将模式的ID设置为特定于每组数据的name
,例如name
或为每个记录添加唯一的ID属性。
为了节省加载一堆模态...而不是根据数据动态创建模态(重复使用ID myModal
),尝试重用单个模态并将内容动态添加到该模态可能是个好主意根据点击的内容。 您仍然可以一一加载所有公司数据(将模式从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">×</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>
注意:您可能必须考虑如何以不同的方式构造数据。 也许给每个按钮一个唯一的ID,然后获取与该唯一ID相关的数据<button id="{{company.uniqueID}}">
。
您还可以根据名称“ 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.