如果在移动设备上,将 html 背景更改为视频中的图像
我目前正在开发一个使用自动播放视频背景的网站,但是这在移动设备上扩展得很差,并且仅位于屏幕的前三分之一...
是否可以在 CSS 中检测移动用户,并将背景更改为合适的肖像图像?
您可以在此处查看网页: https://content.obstrude.com/login
如您所见,该视频在 PC 上效果很好,但在移动设备上却占据了屏幕的前三分之一!
video {
width: 100%;
height: auto;
object-fit: cover;
position: fixed;
top: 0;
left: 0;
z-index: -1;
}
img {
opacity: 0;
filter: alpha(opacity=50);
/* For IE8 and earlier */
}
<html>
<head>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<title>
OBSTRUDE - LOGIN
</title>
</head>
<body onload="document.body.style.opacity='1'">
<video autoplay playsinline muted>
<source src="/static/img/login.mp4" type="video/mp4">
</video>
</body>
</html>
I'm currently developing a website which uses an autoplay video background, however this scales badly on mobile and only sits in the top third of the screen...
Would it be possible in CSS to detect a mobile user, and change the background to a suitable portrait image?
You can view the webpage here: https://content.obstrude.com/login
As you can see, the video works nicely on PC, however on mobile fills the top third of the screen!
video {
width: 100%;
height: auto;
object-fit: cover;
position: fixed;
top: 0;
left: 0;
z-index: -1;
}
img {
opacity: 0;
filter: alpha(opacity=50);
/* For IE8 and earlier */
}
<html>
<head>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<title>
OBSTRUDE - LOGIN
</title>
</head>
<body onload="document.body.style.opacity='1'">
<video autoplay playsinline muted>
<source src="/static/img/login.mp4" type="video/mp4">
</video>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以使用 媒体查询 来检测屏幕宽度窄。
像这样的事情:
查看这篇关于此事的文章https: //css-tricks.com/a-complete-guide-to-css-media-queries/
对于您的确切用例,我建议完全取消视频,而使用 css 动画来实现相同的效果影响。然后,您可以调整它们,以便按照您想要的方式在移动设备上制作动画。
Yep you could use a media query to detect a narrow screen width.
Something like this:
Check out this article on the matter https://css-tricks.com/a-complete-guide-to-css-media-queries/
For your exact use case, I would recommend doing away with the video altogether and instead using css animations to achieve the same effect. You can then instead adjust them to also animate on mobile in the way that you want.
这是使用
media-queries
的基本示例。调整浏览器大小以查看其工作情况。
Here is a basic example using
media-queries
.Resize the browser to see it work.
您还可以将
display
属性从none
切换到block
,反之亦然,以根据屏幕的大小在视频和图像之间切换。You can also toggle
display
property fromnone
toblock
and vise-versa to switch between video and image depending upon the size of the screen.您可以使用媒体查询来实现:
You can use media queries for that: