Hide for 10 seconds

I am making a small script that the element should disappear for only 2 seconds, and then return to appearing alone, I leave the link here Fiddle

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('img').click(function() {
                $(this).fadeOut();
                setTimeout(function() {
                    $(this).fadeIn();

                }, 1000);
                //$(this).toggle();
            });
        });
    </script>

im trying to use attenuation and set time but not working

+4
source share
6 answers

thison $(this).fadeIn();no longer points to an image.

Try the following:

 $(document).ready(function() {
            $('img').click(function() {
                var img = this; /* Store it on variable img */
                $(img).fadeOut();
                setTimeout(function() {
                    $(img).fadeIn(); /* Can access variable img here */
                }, 1000);
                //$(this).toggle();
            });
        });

Fiddle: https://jsfiddle.net/7rfypomx/1/

+3
source

$(this)refer to the window you must use $('img').fadeIn();so that the image reappears

 $(document).ready(function() {
            $('img').click(function() {
                $(this).fadeOut();
                setTimeout(function() {
                    $('img').fadeIn();

                }, 1000);
                //$(this).toggle();
            });
        });

https://jsfiddle.net/ftf6wr8L/

+2
source

, .

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $('img').click(function() {
            $(this).fadeOut().delay(1000).fadeIn();
        });
    });
</script>
+2

, :

$(document).ready(function() {
        $('img').click(function() {
            var img = $(this);
            $(this).fadeOut();
            setTimeout(function() {
                img.fadeIn();

            }, 1000);
            //$(this).toggle();
        });
    });
+1

$(this) ex. var ty = $(this);

faceIn().

$(ty).fadeIn();

I also updated your existing script link. Check here

0
source

 $(document).ready(function() {
        $('img').click(function() {
            $(this).fadeOut(function(){
               $(this).fadeIn(15000);
            });
      });
});
    table,
        th,
        td {
            border: 1px solid black;
        }
        
        img {
            width: 25px;
            height: 25px;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
        <tr>
            <th><img src="img/1.png"></th>
            <th><img src="img/2.png"></th>
        </tr>
        <tr>
            <th><img src="img/3.jpg"></th>
            <th><img src="img/4.jpg"></th>
        </tr>
    </table>
Run codeHide result
0
source

Source: https://habr.com/ru/post/1692250/


All Articles