Search code examples
javascriptjqueryglow

Glowing image with jQuery/Javascript


I have two images. One that is normal, and another that is more colourised. I want to have this image displaying on top of the other and having a "glowing" effect where it switches between transparent and opaque every second or so. I also need to stop this effect when the user presses a particular button. How would I go about doing this with jQuery or Javascript?


Solution

  • jsBin demo

    :

    <button id="stop">STOP IT!</button>
    
    <div class="images">
         <img src="img1.jpg" />
         <img src="img2.jpg" class="glowed"/>
    </div>
    

    :

    var playing = true;
    
    function loop(){
      if(playing){
        $('.images img:eq(1)').fadeIn(700, function(){
          $(this).fadeOut(700,loop);
        });
      }
    }
    
    loop(); // start loop
    
    
    $('#stop').click(function(){
      playing=0;
    }); 
    

    Just position the two images absolute:

      .images img{
        position:absolute;
      }
    
      .glowed{
        box-shadow: 0px 0px 30px 2px #cf5
      }
    

    I used box shadow, but you could use a glowed .png image instead.