Magnific popup: Get current element in callback

浪子不回头ぞ 提交于 2019-11-27 23:00:25

For Magnific Popup v0.9.8

var magnificPopup = $.magnificPopup.instance,
              cur = magnificPopup.st.el;
console.log(cur.attr('myatt'));

First, i recommend to you to use the data attribute ( data-custom="foo" ) or a known attribute.

HTML :

  <a href="photo.jpg" class="popup" data-custom="dsaad">Do it!</a>
  <a href="photo.png" class="popup" data-custom="mycustomdata">Do it!</a>

jQuery :

$('.popup').magnificPopup({
  type : 'image',
  callbacks : {
    open : function(){
      var mp = $.magnificPopup.instance,
          t = $(mp.currItem.el[0]);

      console.log( t.data('custom') );
    }
  }
});

I do not know if a better way exists. Actually you can read their documentation about public methods and you will see there. I tested the code above and everything works fine :)

For v. 0.9.9:

this.currItem.el

// "item.el" is a target DOM element (if present)
// "item.src" is a source that you may modify
open: function(item) {}

and use data-attributes, to example data-myatt - that get:

$(this).data('myatt')

The clicked element can be accessed within the callback using:

this.st.el

Inside the callback, "this" refers to $.magnificPopup.instance.

Under public properties:

"magnificPopup.st.el // Target clicked element that opened popup (works if popup is initialized from DOM element)"

Also, inside open: function(item) {}, this.content might help.. It will return the div of the content being shown. useful with the change: function () {} as well. Hope it helps someone like me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!