Hide Video Controls Until User Hover Over Video

后端 未结 4 773
渐次进展
渐次进展 2021-02-14 07:16

i\'m trying to hide the video controls on my video, until the user hover over the video, then the controls shows up. Any idea or advice? Thanks. And I\'ve got more than

4条回答
  •  情书的邮戳
    2021-02-14 07:54

    One issue with @EnigmaRM's answer is that if jQuery somehow misses a hover event, the controls can be toggled the "wrong" way - that is, they disappear on mouse enter and reappear on mouse leave.

    Instead, we can ensure that the controls always appear and disappear correctly with event.type:

    $("#myvideo").hover(function(event) {
        if(event.type === "mouseenter") {
            $(this).attr("controls", "");
        } else if(event.type === "mouseleave") {
            $(this).removeAttr("controls");
        }
    });
    

提交回复
热议问题