根据串流是否处于活动状态,显示或隐藏页面元素。这样你可以构建在游戏开始时消失的串流前落地页界面,以及在游戏进行时出现的串流中控件。
可见性切换
给任何你想切换的元素添加 .yom-toggle-visibility 类和 data-yom-stream-id:
<!-- Visible before stream, hidden during stream -->
<div class="yom-toggle-visibility" data-yom-stream-id="1">
<h2>Welcome! Click below to start playing.</h2>
<button class="yom-launch-button" data-yom-stream-id="1">Play Now</button>
</div>
<!-- Hidden before stream, visible during stream -->
<div class="yom-toggle-visibility" data-yom-stream-id="1" style="display: none;">
<p>Game is running. Use the controls below:</p>
<button onclick="window.postToPlayer('1', 'SpawnBot', {amount: '1'})">Add Bot</button>
</div>
工作方式:
- 当串流变为活动状态(
playerReady)时,元素会切换:可见的淡出,隐藏的淡入。 - 当串流关闭(
streamClosed)时,元素会切换回原来的状态。
CSS 过渡
淡入淡出动画为 300ms。添加下面的 CSS,让过渡更平滑:
.yom-toggle-visibility {
transition: opacity 0.3s ease;
}
容器钩子
在 .yom-player-placeholder 上使用 data-hook,在串流运行时给任何容器元素添加 .yom-active 类:
<div id="game-section">
<div class="yom-player-placeholder"
data-yom-stream-id="1"
data-game-id="my-game"
data-hook="#game-section"
style="width: 100%; aspect-ratio: 16/9;">
</div>
</div>
现在你可以用 CSS 为活动状态设置样式:
#game-section.yom-active {
background: #000;
padding: 0;
max-width: 100%;
}
.yom-active 类在串流开始时添加,在串流结束时移除,让你完全用 CSS 控制布局变化。