如何修改代码实现Magnific Popup内联弹窗的animate.css动画
需求背景
作为jQuery开发新手,已完成Animate.css与Magnific Popup图片画廊的动画联动,当前需要让内联(inline)类型弹窗复用完全一致的入场、出场动画效果。现有初始配置代码如下:
$(document).ready(function() { $('.pop-up-image').magnificPopup({ removalDelay: 300, gallery:{enabled:true}, type: 'image', image: { verticalFit: true, markup: '<div class="mfp-figure">'+ '<figure>'+ '<div class="mfp-header">'+ '<div class="mfp-top-bar">'+ '<div class="mfp-title"></div>'+ '<div class="mfp-counter"></div>'+ '<div class="mfp-close"></div>'+ '</div>'+ '</div>'+ '<div class="mfp-content-container">'+ '<div class="mfp-img"></div>'+ '</div>'+ '<div class="mfp-footer">'+ '<figcaption class="mfp-figcaption">'+ '<div class="mfp-bottom-bar-container">'+ '<div class="mfp-bottom-bar">'+ '<div class="mfp-description"></div>'+ '</div>'+ '</div>'+ '</figcaption>'+ '</div>'+ '</figure>'+ '</div>' }, callbacks: { beforeOpen: function() { this.st.image.markup = this.st.image.markup.replace('mfp-figure', 'mfp-figure animated ' + this.st.el.attr('data-effect')); }, markupParse: function(template, values, item) { values.title = item.el.attr('title'); }, beforeClose: function() { this.content.addClass('zoomOut'); }, close: function() { this.content.removeClass('zoomOut'); }, }, }); });
现有实现的核心问题
原配置的beforeOpen回调逻辑是直接修改this.st.image.markup,该配置项仅对type: 'image'的图片弹窗生效。内联弹窗有独立的渲染逻辑,使用专属的inline.markup配置,完全不会触发原有的类名替换操作,因此动画类无法被注入到内联弹窗的DOM节点上,动画自然不生效。
具体修改方案
- 拆分公共配置:把所有通用的回调、基础配置(比如
removalDelay)抽成公共配置对象,分别给图片类触发器、内联类触发器绑定初始化,避免重复写逻辑 - 优化
beforeOpen的类名注入逻辑:不再硬绑定修改图片专属的markup,而是根据当前打开的弹窗类型,动态定位对应类型的markup根节点,注入animated类和触发器上data-effect属性指定的自定义动画 - 保留原有的关闭动画逻辑:
this.content是Magnific Popup对所有类型弹窗通用的内容容器引用,无需额外修改,内联弹窗打开后也能正常获取该引用,应用关闭时的zoomOut动画 - 给内联弹窗触发器加上和图片弹窗一致的
data-effect属性,指定想要的入场动画
修改后可直接复用的完整代码
$(document).ready(function() { // 抽离公共配置,图片、内联弹窗复用 const commonPopupConfig = { removalDelay: 300, callbacks: { beforeOpen: function() { // 获取触发器上配置的自定义入场动画 const customEffect = this.st.el.attr('data-effect'); // 针对图片类型弹窗,注入动画类 if (this.st.type === 'image') { this.st.image.markup = this.st.image.markup.replace( 'mfp-figure', `mfp-figure animated ${customEffect}` ); } // 针对内联类型弹窗,注入动画类 if (this.st.type === 'inline') { this.st.inline.markup = this.st.inline.markup.replace( 'mfp-content', `mfp-content animated ${customEffect}` ); } }, markupParse: function(template, values, item) { values.title = item.el.attr('title'); }, beforeClose: function() { this.content.addClass('zoomOut'); }, close: function() { this.content.removeClass('zoomOut'); } } }; // 初始化图片画廊弹窗(沿用原自定义markup配置) $('.pop-up-image').magnificPopup({ ...commonPopupConfig, gallery: {enabled: true}, type: 'image', image: { verticalFit: true, markup: '<div class="mfp-figure">'+ '<figure>'+ '<div class="mfp-header">'+ '<div class="mfp-top-bar">'+ '<div class="mfp-title"></div>'+ '<div class="mfp-counter"></div>'+ '<div class="mfp-close"></div>'+ '</div>'+ '</div>'+ '<div class="mfp-content-container">'+ '<div class="mfp-img"></div>'+ '</div>'+ '<div class="mfp-footer">'+ '<figcaption class="mfp-figcaption">'+ '<div class="mfp-bottom-bar-container">'+ '<div class="mfp-bottom-bar">'+ '<div class="mfp-description"></div>'+ '</div>'+ '</div>'+ '</figcaption>'+ '</div>'+ '</figure>'+ '</div>' } }); // 初始化内联弹窗 $('.pop-up-inline').magnificPopup({ ...commonPopupConfig, type: 'inline' }); });
实际使用注意事项
- 内联弹窗的触发器(比如点击按钮)需要加上和图片触发器一致的
data-effect属性,比如data-effect="bounceIn",即可指定对应的入场动画 - 确保已经正确引入Animate.css的样式文件,所有用到的动画类都在样式文件中存在
内容的提问来源于stack exchange,提问作者james1984
相关产品推荐
相关产品推荐

