互动
最近评论

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AE-圣诞树</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
        }
        #video1 {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        #overlay {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 24px;
            cursor: pointer;
            z-index: 1;
        }
    </style>
</head>
<body>
    <div id="main">
        <video id="video1" autoplay loop muted preload="auto">
            <source src="https://tianfeng.space/wp-content/uploads/2022/12/1.mp4" type="video/mp4">
            <!-- https://tianfeng.space/wp-content/uploads/2022/12/圣诞树1.mp4(也可以替换上面的链接,是另一个效果) -->
            Your browser does not support the video tag.
        </video>
        <div id="overlay">点击这里开启声音</div>
    </div>

    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            var video = document.getElementById("video1");
            var overlay = document.getElementById("overlay");

            // 尝试自动播放(静音)
            video.play().catch(function(error) {
                console.warn("自动播放被阻止:", error);
            });

            // 添加一个事件监听器,用于当用户点击覆盖层时取消静音并隐藏覆盖层
            overlay.addEventListener('click', function() {
                if (video.muted) {
                    video.muted = false; // 取消静音
                    video.play(); // 再次尝试播放
                    overlay.style.display = 'none'; // 隐藏覆盖层
                }
            });
        });
    </script>
</body>
</html>


评论
你无需删除空行,直接评论以获取最佳展示效果
引用到评论