【发布时间】:2021-10-02 01:43:51
【问题描述】:
我正在尝试解决如何仅在单击而不是之前加载我的选项卡的内容。每次切换到另一个选项卡时,我都需要重新加载内容,这是因为我需要在每个选项卡中嵌入自动播放的视频,并且我需要重新加载,这样视频就不会在后面继续播放。而且我需要每次只播放一个视频。我已经尝试了很多东西,目前我没有任何我认为可行的东西。
现在我的标签的代码如下:
const tabSystem = {
init() {
document.querySelectorAll('.tabs-menu').forEach(tabMenu => {
Array.from(tabMenu.children).forEach((child, ind) => {
child.addEventListener('click', () => {
tabSystem.toggle(child.dataset.target);
});
if (child.className.includes('is-active')) {
tabSystem.toggle(child.dataset.target);
}
});
});
},
toggle(targetId) {
document.querySelectorAll('.tab-content').forEach(contentElement => {
contentElement.style.display = contentElement.id === targetId ? 'block' : 'none';
document.querySelector(`[data-target="${contentElement.id}"]`).classList[contentElement.id === targetId ? 'add' : 'remove']('is-active');
})
},
};
tabSystem.init();
margin: 0;
font-size: 0;
padding: 0;
}
.tabs-menu {
cursor: pointer;
font-family: 'Inter', sans-serif;
}
.tab-item {
font-size: 15px;
background-color: #A6B7C7;
display: inline-block;
padding: 13px 20px;
color: white;
margin: 0;
}
.is-active {
color: black;
}
.tab-content {
display: none;
padding: 0px;
}
.show-content {
display: block;
background: lightgray;
}
<ul class="tabs-menu">
<li class="tab-item is-active" data-target="first-tab"><a>Español</a></li>
<li class="tab-item" data-target="second-tab" id="Tab02"><a>English</a></li>
<li class="tab-item" data-target="third-tab"><a>Português</a></li>
</ul>
<div class="tab-content" id="first-tab">
<iframe width="560" height="315" src="https://www.youtube.com/embed/7N9XW_mQ7hI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<div class="tab-content" id="second-tab">
<iframe width="560" height="315" src="https://www.youtube.com/embed/bdvidv6RXJY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<div class="tab-content" id="third-tab">
<iframe width="560" height="315" src="https://www.youtube.com/embed/rSnaEd-dwTg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
如果有人知道如何解决这个问题,我将不胜感激。
【问题讨论】:
-
您可以remove iframe 元素并再次add 它们。或者,考虑使用YouTube Player API 停止和启动视频。另见YouTube iframe API: how do I control an iframe player that's already in the HTML?
标签: javascript html iframe tabs reload