How to create fade-in effect in div?

Asked

Viewed 1,360 times

1

I have a div which receives data from the Mysql database and lists the images. I placed a lightbox to make more interesting the visualization of images and an effect of fade-in so that whenever you change the category of the menu*refresh* it was only given in the div and not on the full page. Only when I have open the lightbox in some other category than the default, which displays all of them, does not open.

 <!-- Script do Fade In -->
    <script>
        $(document).ready(function(){
            var content = $('#modal');

            //pre carregando o gif
            loading = new Image(); loading.src = 'images/loading_ajax.gif';
            $('#menu-portifolio a').live('click', function( e ){
                e.preventDefault();
                content.html( '<img src="images/loading_ajax.gif" />' );
                var href = $( this ).attr('href');
                $.ajax({
                    url: href,
                    success: function( response ){
                        //forçando o parser
                        var data = $( '<div>'+response+'</div>' ).find('#modal').html();

                        //apenas atrasando a troca, para mostrarmos o loading
                        window.setTimeout( function(){
                            content.fadeOut('slow', function(){
                                content.html( data ).fadeIn();
                            });
                        }, 500 );
                    }
                });
            });
        });
    </script>


    <!-- Script do Lightbox -->
    <script>
        $(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(50);
                $('#mascara').fadeTo("slow",0.8);

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

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

            $("#mascara").click( function(){
                $(this).hide();
                $(".window").hide();
            });

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

The results of the SQL query, in case images are being displayed by categories.

Categoria 01 shows only images that are of this category.

When loading the page it automatically shows the images that are in the category all.

There the lightbox works normally, but when switching to Categotia 01 for example the lightbox no longer works, in any other category.

Is there any other script effect fade-in to indicate me, since I am still a layman in the matter of jQuery and AJAX.

  • I don’t get it @Hermesnetto.

  • Hi, do not understand the problem please rewrite this part: "only when I have open the lightbox in some other category than the default, which displayed all it does not open."

  • Hello, Hermes Netto, hello. Please use the http://jsfiddle.net website and put all the code necessary to reproduce your problem on it, and explain that problem then so we can help you. Thank you.

  • I understand your problem, I just can’t figure out all this paraphernalia you’ve written. You want to click on a category and filter the corresponding "images", but doing so with a "ephetto", right?

  • See if this plugin does something similar. Whenever I use Ligthbox I use it: http://lokeshdhakar.com/projects/lightbox2/

  • Almost that Edgar Muniz Berlinck, when clicking on the category I want him to just reload the div, avoiding giving refresh on the entire page understood?

  • I already got him to reload only the div with that script upstairs, but he gives a certain conflict with the lightbox.

Show 2 more comments

1 answer

1

var data = $( '<div>'+response+'</div>' ).find('#modal').html();

It would be interesting to assign an 'id' or 'class' to your div, make your life easier (and maybe help the script) to identify the element on your page, something like this:

var data = $( '#div'+response.id ).find('#modal').html();

Another thing, assuming that for each category there will be a div[id=modal], it would be interesting to also exchange #modal for . modal, only for the purpose of validating your html code.

-- If you can send the code returned from your request and the html, it would be of great help to us and to you. Try the jsfiddle.

  • I made the change you told me but now it’s charging non-stop...

  • Put the result in an online script debugger such as jsfiddle. It would be much easier and faster to detect and fix the problem.

  • To be honest I’m trying it right now, but I haven’t got anything yet..

  • var data = $( '#'+response.id ).find('#modal').html(); - either HTML is invalid or this is exactly the same as var data = $('#modal').html(); - ids are unique, going against this will cause bugs in some browsers.

  • var data = $( '#div'+response.id ).find('#modal').html(); Assuming your Sponse.id is 2; This code will look for the div "#div2" and inside it will look for the html content of the div #modal. $('#modal').html() Return the contents of the div #modal, if there is more than one, will return the last found.

Browser other questions tagged

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