【发布时间】:2015-07-29 01:25:44
【问题描述】:
我正在使用一个名为 magnific popup 的 jquery 脚本,我正在尝试访问我在回调函数中创建的变量,在另一个回调函数中,但我不知道该怎么做。我的 magnific init 代码如下所示:
$('.packery').magnificPopup({
delegate: '.js-modal',
type: 'ajax',
mainClass: 'mfp-zoom-in',
removalDelay: 500, //delay removal by X to allow out-animation
callbacks: {
elementParse: function(item) {
item.community = item.el.attr('data-community');
var communityClass = item.community;
console.log(item.community);
// this ^ does actually print the data-community
console.log('Parsing content. Item object that is being parsed:', item);
},
resize: function() {
console.log('Popup resized');
// resize event triggers only when height is changed or layout forced
$('.mfp-bg').addClass(communityClass);
}
}
});
如果我尝试设置 $('.mfp-bg').addClass(communityClass); 或 $('.mfp-bg').addClass(item.community);,我会收到未捕获的 ReferenceError:communityClass 未定义。
我无法在 elementParse 中将类应用于 mfp-bg,因为该元素尚未创建。
我知道我不能在 javascript 中使用来自不同函数的变量,但我对如何在 resize 回调中实际使用 data-community 属性有点困惑,因为我似乎可以只在 elementParse 回调中创建变量?
任何帮助将不胜感激,干杯。
【问题讨论】:
-
var communityClass;高于一切,然后communityClass = item.community;将位于两个函数都可以访问的范围内。
标签: javascript jquery magnific-popup