Page Load with Swipebox

1

I want the Swipebox to open the page with an image.

link

I did so:

jQuery

    if($('.swipebox')[0]){
        $('.swipebox')[0].click();
    }

    // Swipe Box
    $('.swipebox').on('click', function(e) {
        e.preventDefault();
        $.swipebox([
            { href: urlBase + '/imagem.jpg', title:'Safe' }
        ]);
    });

HTML

<a href="#" class="swipebox"></a>

Instead of opening Swipebox with the image, it opens on another page. But if I go to Chrome Console and type:

$('.swipebox').click(); , opens right.

Debug

I did this on and off!

$('#open-swipebox').click();    

$('#open-swipebox').click(function (){
    console.log('das');
});

<a href="#" id="open-swipebox"></a>
    
asked by anonymous 05.11.2015 / 17:47

1 answer

1

No load of the page you emulate the click on your link:

$(document).ready(function () {

  $('.swipebox').on('click', function(e) {
    e.preventDefault();
    $.swipebox([
        { href: urlBase + '/imagem.jpg', title:'Safe' }
    ]);
  });

  $('.swipebox').trigger('click');
});

However, in this way, I will activate the event for all items that have this class, I think it would be better to use an id for the link.

<a href="#" class="swipebox" id="meuLink"></a>

And to emulate the click:

$(document).ready(function () {
  $('#meuLink').trigger('click');
});

Complement to the answer

  The click event is being called before the declaration, just call the $('.swipebox').click() event before assigning the new behavior to the link / class;

    
05.11.2015 / 18:03