Hide Video Controls Until User Hover Over Video

后端 未结 4 792
渐次进展
渐次进展 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:48

    We can accomplish this through just a couple lines of jQuery, making use of .hover():

    Working Example

    $('#myvideo').hover(function toggleControls() { if (video.hasAttribute("controls")) { video.removeAttribute("controls") } else { video.setAttribute("controls", "controls") } })

    Edit I mistakenly left the variable video in the code above. I changed it to this so that you won't have to manage variables that grab an ID.

    $('#myvideo').hover(function toggleControls() {
        if (this.hasAttribute("controls")) {
            this.removeAttribute("controls")
        } else {
            this.setAttribute("controls", "controls")
        }
    })
    

    HTML

    
    

    Update: You mentioned that you have several videos. So you can use this same logic, and just add additional selectors into $( ). Here's an example:

    $('#yourID1, #yourID2, #yourID3').hover(function toggleControls() { ...
    

    Doing that will listen or wait until it detects that you're hovering over one of those IDs.

    Updated fiddle

提交回复
热议问题