Load Page with Swipebox

Asked

Viewed 120 times

1

I want you to open the page and open a Swipebox with an image.

http://brutaldesign.github.io/swipebox/#Try

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 him opening Swipebox with the image, open it on another page. But if I go to the Chrome Console and type:

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

Debug

I did it here and it doesn’t work !

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

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

<a href="#" id="open-swipebox"></a>
  • You want when you open the page, automatically from a Trigger on your link?

  • Exactly. A trigger. But I tried and it didn’t work.

1 answer

1


In the page load 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, this way will enable 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 event click is being called before the statement, just call the event $('.swipebox').click() before assigning the new behavior to the link/class;

  • Something silly, but tried to declare the click before invoking it?

  • As such declare ?

  • I did a simple debug and it doesn’t work. It’s inside the ready.

  • 1

    Try to invert, take a look as I did in the reply (I changed it now). Also I used your example in fiddle: https://jsfiddle.net/PetrusStarken/9of7h7kc/

  • Wow, old man. It was the inversion. I was calling the trigger before the declaration of the function. It was noobisse that. Thanks.

  • Also figured it out now, I’ll change the answer to make it clear what it was.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.