reactjs - 反应中的可见性传感器在移动设备上不起作用
问题描述
我正在使用 react-visibility-sensor 来播放和暂停视频,当它不在像 web 应用程序的 tiktok 的视口中时,它在桌面上运行良好,但我不明白为什么它在移动设备上不起作用(用 chrome 和ios中的野生动物园)
import React, { useRef, useState } from "react";
import VisibilitySensor from "react-visibility-sensor";
import "./Video.css";
export default function Video({
url,
}) {
const [playing, setPlaying] = useState(false);
const videoRef = useRef(null);
function onChange(visibility) {
if (visibility) {
videoRef.current.play();
setPlaying(true);
} else {
videoRef.current.pause();
setPlaying(false);
}
}
function onVideoPress() {
if (playing) {
videoRef.current.pause();
setPlaying(false);
} else {
videoRef.current.play();
setPlaying(true);
}
}
return (
<div className="video">
<VisibilitySensor onChange={onChange}>
<video
playsInline
autoPlay
loop
muted
className="video_player"
onClick={onVideoPress}
ref={videoRef}
src={url}
/>
</VisibilitySensor>
</div>
);
}
解决方案
推荐阅读
- r - 无法在R中将字符转换为数字
- c# - 如何在 C# .NET Web API 中接收和拆分对象数组
- java - 通过远程 IIS 服务器进行 Java WS 客户端 HTTPS 传输
- python - 执行功能后 tkinter 锁定
- javascript - 在 Wordpress 的 wp-admin/post.php 页面中,我可以在浏览器控制台中输入什么来获取登录用户的显示名称?
- google-colaboratory - Colab - 使用 GDAL 拆分大 tiff 文件
- python - Matplotlib - 图表对象上的 plt.show()
- android - 根据 Volley 的响应代码将用户重定向到某个活动(Android)
- python-3.x - 使用 python/pandas 查找客户状态分类
- python - 使用列表比较和更新 CSV 文件