简体   繁体   中英

Loading slim box with .on()

I am using slimbox ( http://www.digitalia.be/software/slimbox ) a very popular lightweight lightbox for jQUery.

I have used it alot in the past, however now I am a little bit stuck. I had a form that is loading via AJAX, and therefore I need to use the .on() function to activate slimbox.

But I cannot get it work. I looked at the slimbox code and it doesnt make alot of sense to me. I tried this in my :

$("#content.settings").on("click","div.scroll a.slimbox", function (e) {
        e.preventDefault();
        $(this).slimbox();
    });

And that works, but only on the second click, for some reason it takes two clicks on the same object to function. Any ideas?

The problem is when the element is clicked first time slimbox is not yet initialized on the element, when you click first time it gets initialized then subsequent clicks works fine.

One possible solution is to trigger the click event once the widget is initialized

$("#content.settings").on("click","div.scroll a.slimbox", function (e) {
    var $this = $(this);
    e.preventDefault();
    if(!$this.data('slimboxed')){
        $this.slimbox().data('slimboxed', true).click();
    }
});

One way around is to call the plugin again after the ajax request is done. So add this in your success method

$("#content.settings div.scroll a.slimbox").slimbox();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM