简体   繁体   English

仅当按钮当前未被悬停时才逐渐淡入它们

[英]Gradually fade buttons only if they aren't currently being hovered over

I have forward and backward buttons for an image gallery. 我有一个用于图像库的前进和后退按钮。 Currently, they fade away when the mouse is idle. 当前,它们在鼠标空闲时消失。 Any movement on the page will show them again. 页面上的任何移动都会再次显示它们。 Here's the code: 这是代码:

//fade out previous/next buttons on mouse idle
var timer;
$(document).mousemove(function() {
    if (timer) {
        clearTimeout(timer);
        timer = 0;
    }
    $('.fader').fadeIn();
    timer = setTimeout(function() {
        $('.fader').fadeOut(1500)
    }, 1000)
})

This works fine, but I also would like the buttons to not fade out at all if they are currently being hovered. 这可以正常工作,但如果按钮当前处于悬停状态,我也希望按钮完全不消失。

As a test, I've tried this code: 作为测试,我尝试了以下代码:

//both buttons are stored in class .fader

//when buttons are hovered over
$(".fader").hover(
    function(){
        //give them this class
        $(this).toggleClass('is-hover');
    })

//if buttons have .is-hover class, print test statement to console
if($(".fader").hasClass('is-hover')){
    console.log("true");
}

I'm hoping to see the test "true" statement printed to the console, but it's not happening. 我希望看到打印到控制台的测试“ true”语句,但是它没有发生。

Ultimately, I'd like to wrap the first function in the second. 最终,我想将第一个函数包装在第二个函数中。 If buttons are not being hovered over, then perform this timed fadeout of buttons. 如果按钮没有悬停,请执行按钮的淡入淡出操作。


Here is where the buttons are in the HTML: 这是HTML中按钮的位置:

<!-- Swiper -->
<div ng-show="show" class="swiper-container">
    <div class="swiper-wrapper"></div>
    <div class="swiper-pagination"></div>

    <!--The buttons-->

    <div class="swiper-button-next fader"></div>
    <div class="swiper-button-prev fader"></div>
</div>

You can use CSS for the button hovering... 您可以使用CSS进行按钮悬停...
But to track mouse movement on the document, a script is needed. 但是要跟踪鼠标在文档上的移动,需要一个脚本。
You can use both! 您可以同时使用! ;) ;)

 // To track mouse movement and fadein all buttons var timer; $(document).on("mousemove",function(){ $(".fader").addClass("faderShowOnMouseMove"); if(typeof(timer)!="undefined"){ clearTimeout(timer); } timer = setTimeout(function(){ $(".fader").removeClass("faderShowOnMouseMove"); },1000); }); 
 /* Base style for buttons */ .fader { opacity: 0; transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -webkit-transition: opacity 1s ease-in-out; } /* For single button hovering */ .fader:hover { opacity: 1; } /* Used with JS mouse movement */ .faderShowOnMouseMove{ opacity: 1; } /* Just for this demo ;) */ .swiper-pagination{ border:1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- Swiper --> <div ng-show="show" class="swiper-container"> <div class="swiper-wrapper"> Hover over here! <i>(right below this text)</i><br> <div class="swiper-pagination"> <!--The buttons--> <button class="fader">1</button> <button class="fader">2</button> <button class="fader">3</button> </div> </div> </div> 

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

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