打开featherlight灯箱时,是否可以运行代码?再关门时呢?是否有钩子(Hook)或其他东西,以便在打开灯箱后可以运行代码?我在github页面(https://github.com/noelboss/featherlight)上找不到示例

从HBlackorby回答后的附录:

我的设置是这样的:HTML:

<ul data-featherlight
    data-featherlight-type="ajax"
    data-featherlight-filter="a.box"
    data-featherlight-before-open="MYFUNCTION">

    <li><a class="box" href="content1">Link</a></li>
    <li><a class="box" href="content2">Link2</a></li>
    <li><a class="box" href="content3">Link3</a></li>

</ul>

在我单独的JavaScript文件中,有以下几行代码:
MYFUNCTION = function(){
    console.log('Test')
}

不幸的是,打开之前未调用此函数。还尝试用“之后”代替“之前”。我也没有错误。当我在HTML中调用Featherlight时,这些代码行必须在哪里?

最佳答案

在featherlight配置中,您可以设置beforeOpen,afterOpen,beforeClose和afterClose事件挂钩,这些挂钩将在发生这些事件时为您调用一个函数:

https://github.com/noelboss/featherlight/#installation

如果要将框与JS绑定(bind),则可以在传递给它的配置中指定它们:
let myConfig = { beforeOpen: yourFunctionName1, beforeContent: yourFunctionName2, beforeClose: yourFunctionName3, afterOpen: yourFunctionName4}$('.myElement').featherlight($content, myConfig);
另外,您也可以使用标签的HTML数据属性指定这些事件。例如:
<img src="" id="" data-featherlight-before-open="yourFunction1()" />

关于javascript - 在羽毛灯灯箱打开和关闭时运行代码,例如回调,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57662509/

10-09 12:34