-4

How can I fade in and out an image on a loop? I want the image to fade in and out all the time until the user put the mouse hover the image, that's when I want the image to be shown, and when the mouse leaves the image- the fading will continue.

How can I do it with html/css/javascript/jquery?

user2254436
  • 491
  • 3
  • 10
  • 24

3 Answers3

1

Here's a jsFiddle explaining how to achieve that ;)

html

<div id="your_flipping_img" class="animate"></div>

CSS

#your_flipping_img{
    background-color:red;
    width:150px;
    height:150px;
}

.animate{
    -webkit-animation: flicking 2s; /* Chrome, Safari, Opera */
    -webkit-animation-iteration-count: infinite;
    animation: flicking 2s infinity;
    animation-iteration-count: infinite;
}

@-webkit-keyframes flicking {
  0%   {opacity:1}
  25%  {opacity:0}
  50%  {opacity:1}
  75%  {opacity:0}
  100% {opacity:1}
}

/* Standard syntax */
@keyframes flicking {
  0%   {opacity:1}
  25%  {opacity:0}
  50%  {opacity:1}
  75%  {opacity:0}
  100% {opacity:1}
}

JS

$('#your_flipping_img').bind('mouseover', function(){
   $('#your_flipping_img').removeClass('animate');
})

$('#your_flipping_img').bind('mouseout', function(){
    $('#your_flipping_img').addClass('animate');
})
jcgaudette
  • 56
  • 5
0

A simple function for toggle fading :

var hover = false;
function myFunction()
{
    if(!!hover)return;
    $("#pic").fadeToggle();
    setTimeout(myFunction(), 600);
}

Prevent it if hover :

$("#pic").mouseenter(function()
{
    hover = true;
    $(this).fadeIn()
})
$("#pic").mouseleave(function()
{
    hover = false;
    myFunction();
})

As simple

0
<script type="text/javascript">
    var timer;
    var opacityValue = 0; 
    function animate(event, object)
    {
        if(event == 'stop') {
            clearTimeout(timer);
        } else { 
            timer = setTimeout(function(){
                object.animate({
                    opacity:opacityValue
                });
                opacityValue = opacityValue ? 0 : 1;
                animate('start',object); 
            },1000);
        }
    }

    $('.imageClass').hover(function(){
        animate('stop',$(this));
    },function(){
        animate('start',$(this));
    });
    animate('start',$('.imageClass'));
</script>
M Rostami
  • 4,035
  • 1
  • 35
  • 39