javascript - 即使使用错误的 if 语句,Onscroll 也会保持触发功能
问题描述
我正在使用 onscroll 调用我的函数来播放视频,但我只希望它只触发一次该函数。我什至为它设置了条件,所以它只会发生一次但它不起作用。有什么想法吗?(用三个=固定)
我还可以play() failed because the user didn't interact with the document first
在新页面加载时进入我的登台站点。
window.onscroll = scrollfunc;
function scrollfunc() {
if (document.getElementById("vidhero").style.display = "none") {
document.getElementById("vidhero").style.display = "block";
document.getElementById("vidhero").play();
}
}
<html>
<body>
<h1>The video element</h1>
<video id="vidhero" width="320" height="240" style="display:none" src="https://www.w3schools.com/tags/movie.mp4">
Your browser does not support the video tag.
</video>
<div style="height: 100vh"></div>
</body>
</html>
解决方案
使用 === 进行比较
window.onscroll = scrollfunc;
function scrollfunc() {
if (document.getElementById("vidhero").style.display === "none") {
document.getElementById("vidhero").style.display = "block";
document.getElementById("vidhero").play();
}
}
推荐阅读
- xml - 双换栏问题
- python - 我可以让生成器中的第一个元素返回一些特殊/不同的东西吗?
- django - 在序列化程序 DRF 中创建 api 之前编写 if else 逻辑
- django - Django - 基于嵌套对象聚合分数
- flutter - Flutter - 可滚动视图内的 TabView
- python - 为什么我的 python 客户端在从 Internet 接收数据时挂起?
- java - 如何在 Android Studio 中将日期选择器对话框值转换为字符串,如(2021 年 1 月 13 日)?
- java - 后缀转换器中的中缀符号错误
- docusignapi - C#.net docusign Api 调用返回错误(底层连接已关闭:接收时发生意外错误)
- javascript - Ajax 向 Flask 中的 D3JS 发送数据