简体   繁体   English

悬停时 setInterval 中的图像闪烁

[英]Image flickering in setInterval on hover

This is an image slider.这是一个图像滑块。 When I hover over the slider, everything is normal, play/pause button appears.当我将鼠标悬停在滑块上时,一切正常,播放/暂停按钮出现。 When I hover out of the slider display area, play and pause buttons disappear.当我将鼠标悬停在滑块显示区域之外时,播放和暂停按钮消失。 But when I hover over the area of the play/pause image itself, the image of play/pause flickers.但是当我将鼠标悬停在播放/暂停图像本身的区域上时,播放/暂停的图像会闪烁。

Is there a way to stop the flickering?有没有办法停止闪烁?

Play and pause images appear when you hover over the slider.当您将鼠标悬停在滑块上时,会出现播放和暂停图像。 When play is clicked, it changes to pause and vice versa.单击播放时,它变为暂停,反之亦然。 Play and pause turn automatic sliding on and off播放和暂停开启和关闭自动滑动

Code Snippets:代码片段:

HTML HTML

<section>
            <img src="img/arrow-left.png" alt="Previous" id="prev">
            <div id="outsideSlider">
                <img alt="play" src="img/play.png" id="play">
                <img alt="pause" src="img/pause.png" id="pause">

                <div id="slider">
                    <div class="slide">
                        <div class="sliderText">
                            <h2>Hark! Stay alert</h2>
                            <p>Some text</p>
                        </div>
                        <img src="img/slide1.jpg" alt="Slide 1">
                    </div>
                 </div>
   </section>

jQuery jQuery

setInterval(function(){
    playpause();
},1);

//function to switch between play button and pause button on click

function playpause(){
    if($('#pause').hasClass('activeButton'))
    {
        $('#slider,#play,#pause').click(function(){
            $('#pause').removeClass('activeButton');
            $('#play').addClass('activeButton');
            $('#pause').hide();
            $('#play').show();
            autoswitch=false;
        });
    }   
    //When play is clicked, activate pause button
else if($('#play').hasClass('activeButton'))
    {
        $('#slider,#play,#pause').click(function(){
            $('#play').removeClass('activeButton');
            $('#pause').addClass('activeButton');
            $('#play').hide();
            $('#pause').show();
            autoswitch=true;
        });
    }
}

I found the answer to this.我找到了这个问题的答案。

The problem was not in the code I posted above.问题不在我上面发布的代码中。 It was in this function:它在这个函数中:

function showhide()
{
    $('.slide,#play,#pause').mouseenter(function(){
        tt=setInterval(function(){  
            $('.activeButton').show();
        },1);
    }).mouseleave(function(){
        clearInterval(tt);
        $('.activeButton').hide();
    });
}

I moved the .show() function outside the setInterval and voila!我将 .show() 函数移到了 setInterval 之外,瞧! No flicker无闪烁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM