How to make an icon flash/blink, which is present in a web page

后端 未结 3 1926
无人共我
无人共我 2021-01-25 10:55

I started working on advanced java few days before(too late to start on that, I know). I am stuck with a specific task of making an icon (which is present on the task bar) blink

相关标签:
3条回答
  • 2021-01-25 11:08

    Small fix

    instead

    if(img.display == 'hidden')
    

    use

    if(img.style.visibility == 'hidden')
    
    0 讨论(0)
  • 2021-01-25 11:18

    HTML

    <img src='image/source' alt='blinking!' id='blinking_image' />
    

    Javascript

    var img = document.getElementById('blinking_image');
    
    var interval = window.setInterval(function(){
        if(img.style.visibility == 'hidden'){
            img.style.visibility = 'visible';
        }else{
            img.style.visibility = 'hidden';
        }
    }, 1000); //the 1000 here is milliseconds and determines how often the interval should be run.
    

    This creates an anonymous function inside the setInterval that runs every 1 second (1sec == 1000milisec). To see more about setInterval checkout the mdn here on it.

    Each time it runs it checks to see if the img is hidden or visible if it's hidden then it shows it if it's visible then it hides it. It does this by checking the style.visiblity property. Which you can learn more about here on the mdn.

    0 讨论(0)
  • 2021-01-25 11:30

    You might find opacity works better because the image is still there, which means it is still clickable if necessary. Also you can add a clear interval to stop the flashing.

    var mycounter = 0
    interval = window.setInterval(function () {
    if (img.style.opacity == '0.1') {
                img.style.opacity = '1';
    
                mycounter = mycounter + 1
                if (mycounter == 7) {
                    clearInterval(interval);
                }
            } else {
                img.style.opacity = '0.1';
            }
        }, 500); //the 1000 here is milliseconds and determines how often the interval 
    
    0 讨论(0)
提交回复
热议问题