Javascript fade image in and out

后端 未结 2 603
执笔经年
执笔经年 2021-01-14 06:26

I am fairly new to JavaScript and i need a very simple script to fade an image in and out slowly on a loop. Any help would be much appreciated.

相关标签:
2条回答
  • 2021-01-14 06:36

    The easiest way would be using jQuery:

    <img src="..." id="myImage">
    
    
    <script type="text/javascript">
    jQuery(function(){
    
       // Fade In
       $("#myImage").fadeIn();
    
       // Fade Out
       $("#myImage").fadeOut();
    
    });
    </script>
    

    Documentation: http://api.jquery.com/fadeIn/

    You can also change the fade duration by passing a parameter

     // predefined slow (200ms)
     $("#myImage").fadeOut("slow");
    
     // predefined fast (600ms)
     $("#myImage").fadeOut("fast");
    
     // 1500 ms
     $("#myImage").fadeOut(1500);
    

    Update creating a loop:

    function fadeIn()
    {
       $(this).fadeIn( fadeOut );
    }
    
    function fadeOut()
    {
       $(this).fadeOut( fadeIn );
    }
    
    fadeIn.call($("#myImage"));
    
    0 讨论(0)
  • 2021-01-14 06:50

    There is a fadein function in jQuery

    $('#imgName').fadeIn('slow');
    
    0 讨论(0)
提交回复
热议问题