Trigger lightbox with javascript

送分小仙女□ 提交于 2019-12-13 14:20:40

问题


I want to use this library: http://lokeshdhakar.com/projects/lightbox2/

I can't attach rel="lightbox" to each image so I want to use jQuery to trigger the lightbox.

I was thinking about something like:

$('img').click(function(){
  //triger lightbox for this image
  //use self src as href
});

How can I trigger the lightbox for one image?


回答1:


Solved the problem by wrapping the img tag in an a tag and triggering click on a after that.

$('img').click(function () {
    $(this).wrap('<a href="' + $(this).attr("src") + '" rel="lightbox" />');
    $(this).parent('a').trigger('click');
});



回答2:


That plugin has to have the rel="lightbox" to work. That's mentioned on the How to use

You can add that attribute with jquery using

$('img').attr('rel', 'lightbox');

You will need to add that line inside your document ready function.

If that still doesn't work for you then I'd suggest you use this other plugin

http://fancyapps.com/fancybox/



来源:https://stackoverflow.com/questions/10820277/trigger-lightbox-with-javascript

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