Hide Video Controls Until User Hover Over Video

后端 未结 4 1915
萌比男神i
萌比男神i 2021-02-14 07:28

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:36

    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");
        }
    });
    

提交回复
热议问题