Open modal window automatically when opening the page

Asked

Viewed 9,260 times

1

I am trying to use the . load function of jquery, replacing the .click. because the intention is for the lightbox to open when loading the page and not clicking.

JS TO OPEN THE MODAL WINDOW BY CLICKING ON A LINK

  $(document).ready(function(){
    $("a[rel=modal]").click( function(ev){
      ev.preventDefault();

      var id = $(this).attr("href");

      var alturaTela = $(document).height();
      var larguraTela = $(window).width();

      //colocando o fundo preto
      $('#mascara').css({'width':larguraTela,'height':alturaTela});
      $('#mascara').fadeIn(1000); 
      $('#mascara').fadeTo("slow",0.8);

      var left = ($(window).width() /2) - ( $(id).width() / 2 );
      var top = ($(window).height() / 3) - ( $(id).height() / 3 );

      $(id).css({'top':top,'left':left});
      $(id).show(); 
    });

    $("#mascara").load( function(){
      $(this).hide();
      $(".janelao").hide();
    });

    $('.fechar').click(function(ev){
      ev.preventDefault();
      $("#mascara").hide();
      $(".window").hide();
    });
  });

JS TO OPEN THE MODAL WINDOW WITH PAGE LOADING

In this part I changed the $("a[rel=modal]"). click for $(window). load and IT WORKED! But it only opens the black background and not the div #janelao

   $(document).ready(function(){
    $(window).load( function(ev){
      ev.preventDefault();

Hence $("#mascara"). click( Function(){ tried to insert . load and $(window). load instead of . click and . show instead of . Hide and DID NOT work

     $("#mascara").load( function(){
      $(this).show();
      $(".janelao").show();
    });

So my problem is carrying the janelao div that doesn’t carry, only carries the black background

1 answer

2

Your problem is in the variable id

When changing $("a[rel=modal]").click( for $(window).load( you change the value of this and therefore when carrying out the operation var id = $(this).attr("href"); this variable is changed.

Change this assignment to var id = $("a[rel=modal]").attr("href"); that will work.

  • Thank you for your reply, Felipe. Finally I used this solution http://wbruno.com.br/jquery/abrir-modal-tuto-maujor-automaticamente-ao-load-pagina-jquery/

  • thanks friend.. that’s right.. this tip helped me a lot

Browser other questions tagged

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