如何进行onclick切换以隐藏和显示视频播放器的UI?

时间:2019-01-15 23:14:25

标签: javascript

我想在单击视频播放器本身而不是单击元素时显示和隐藏视频播放器的元素detailsplay_pause_buttoncontrols_bar。我已经取得了一些进展,但是当我单击不应该发生的元素时,视频播放器的元素就会隐藏。因此,我如何才能检测到视频播放器被单击,而不是视频播放器的元素。

index.html:

var play_pause_button = document.getElementById("play_pause_button");
play_pause_button.style.visibility = "hidden";
var details = document.getElementById("details");
details.style.visibility = "hidden";
var controls_bar = document.getElementById("player_controller_bar");
controls_bar.style.visibility = "hidden";

document.getElementById("video_player_box").onclick = function () {
    if (controls_bar.style.visibility == "hidden") {
        play_pause_button.style.visibility = "visible";
        details.style.visibility = "visible";
        controls_bar.style.visibility = "visible";
    } else {
        play_pause_button.style.visibility = "hidden";
        details.style.visibility = "hidden";
        controls_bar.style.visibility = "hidden";
    }
}
body {
    margin: 0;
}
#video_player_box {
    position: relative;
    height: 100%;
    width: 100%;
    display: block;
    font-family: Helvetica, Arial, sans-serif;
    color: rgb(22,22,23);
}
#video_player_box video {
    height: 100%;
    width: 100%;
    pointer-events: none;
}
#video_player_box img {
    width: 50px;
    height: 50px;
}
#video_player_box #details {
    width: 100%;
    height: 12.5%;
    background-color: rgb(108, 171, 247);
    position: absolute;
    top: 0;
}
#video_player_box #play_pause_button {
    background-color: rgb(108, 171, 247);
    height: 100px;
    width: 100px;
    margin-top: -50px;
    margin-left: -50px;
    position: absolute;
    top: 50%;
    left: 50%;
    border-radius: 50%;
}
#video_player_box #play_pause_button img {
    width: 50%;
    height: 50%;
    z-index: 1;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -25% 0px 0px -25%;
}
#video_player_box #player_controller_bar {
    width: 100%;
    height: 50px;
    background-color: rgb(108, 171, 247);
    position: absolute;
    bottom: 0;
    display: flex;
    align-items: center;
}
.controller_bar_block1, .controller_bar_block3 {
    text-align: center;
    padding: 0px 5px 0px 5px;
    margin: 5px 0px 5px 0px;
}
.controller_bar_block2 {
    flex: 7;
    text-align: center;
    padding: 0px 5px 0px 5px;
}
#video_player_box #seek_bar {
    padding-top: 3px;
    background: rgb(108, 171, 247);
    border: rgb(22, 22, 23) 1px solid;
    border-radius: 50px;
    width: 100%;
    float: left;
    -webkit-appearance: none !important;
    height: 4px;
    flex: 7;
}
#video_player_box #seek_bar::-webkit-slider-thumb {
    -webkit-appearance: none !important;
     background: rgb(22, 22, 23);
     height: 1px;
     width: 1px;
     cursor: pointer;
     border-radius: 100%;
}
<html>
<head>
    <title></title>
</head>
<body>
    <div id="video_player_box">
        <div id="details"></div>
        <div id="play_pause_button">
            <img src="https://image.flaticon.com/icons/png/512/31/31128.png">
        </div>
        <video poster="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Big_buck_bunny_poster_big.jpg/220px-Big_buck_bunny_poster_big.jpg" src="https://dash.akamaized.net/akamai/bbb/bbb_640x360_60fps_1200k.mp4"></video>
        <div id="player_controller_bar">
            <div class="controller_bar_block1">00:00:00</div>
            <div class="controller_bar_block2">
                <input id="seek_bar" type="range" min="0" value="0" max="634">
            </div>
            <div class="controller_bar_block3">00:10:34</div>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您可以使用事件的target属性。 target属性引用实际单击的元素,即使您将处理程序附加到上升元素上也是如此

(请参阅https://developer.mozilla.org/en-US/docs/Web/API/Event/target

作为onClick处理程序的第一个参数,您将收到对该事件的引用

myElement.onclick = function (ev) {
    if (ev.target.id === 'details') { 
        // the clicked element is the details element
    }
}

例如,您还可以检查目标是否是视频元素。

console.log(ev.target.nodeName === 'VIDEO')