javascript - 您如何强制 Foundation Reveal 模式将 HTML 块保留在其写入的父 div 中?
问题描述
当使用 Foundation 6 的 Reveal 模式时,javascript 会自动将显示div
块放在结束body
标记之前,而不是最初写入 DOM 的位置。我需要一种方法来将模态代码保留在我最初编写它的位置,或者至少指定它的父代。
这对我来说是个问题,因为我正在使用 AJAX 来呈现我的页面的一部分(div#main
在下面的示例中),但是我的模态框一直在徘徊,因为它们被移到了外面div
。
我试图通过指定父 div 来打开模式(请参阅此问题的已接受答案)并使用onclick
事件而不是 Foundationsdata-open
方法来执行此操作。对 div 的放置位置也没有任何影响。
例子:
<html>
<head> ... </head>
<body>
<nav>...</nav>
<div id="main">
<h1>Section title</h1>
<p>This is some information about this topic...</p>
<button type="button" data-open="my-modal"></button>
<!-- This is where I add the reveal modal to the DOM -->
<div class="reveal" id="my-modal" data-reveal>
<p>This is my modal content. This should have been positioned inside div#main but it got moved way down here.</p>
</div>
<!-- I need it to show up before the closing tag of this div -->
</div>
<footer>...</footer>
<!-- This is where the reveal modal ends up -->
</body>
</html>
这是上述示例的代码笔。
解决方案
您需要appendTo
为 Reveal 覆盖 Foundation 默认变量。这可以通过以下方式完成:
- 添加一个
data-append-to="div#main"
到 Reveal div 以指定 Reveal 模态的父元素,如果适用,覆盖 div。这允许您为每个显示模式单独更改父元素。资料来源:基金会文档 Foundation.Reveal.defaults.appendTo = "div#main"
通过在使用Source初始化 Foundation 时指定覆盖默认值来全局更改 Reveal 模块的默认值: Foundation Docs
在我的测试中,将父元素设置为较小div
的元素内部body
不会对模态框的定位或背景叠加层的显示产生不利影响。
推荐阅读
- java - 使用 graphql-java-kickstart 找不到 GraphQL 类“kotlinx.coroutines.Dispatchers”
- rust - Rust 赋值和使用借用同时
- c - 如果 userret 中没有数据段,那么 extern char userret[] 代表什么?
- postgresql - postgres中表的自定义缓存
- ssas - SSAS表格多维数据集:多维数据集浏览器中缺少数据
- android-recyclerview - 在这个 recylerview、芯片或单选按钮上它们实际上是什么,如何实现它们
- javascript - HighCharts API饼图CSS如何选择下钻标签
- r - 如何用 R 插值数据?
- symfony - Symfony 5 守护认证系统和 API 密钥生成
- python - 是否可以使用独占终端节点在 DynamoDB 全局二级索引上生成 BETWEEN 查询?