Hoà*_*yễn 40 css background-image ios
这是我的代码:
background-color:#fff;
background-attachment:fixed;
background-repeat:no-repeat;
background-size:cover;
-moz-background-size: cover;
-webkit-background-size: cover;
-o-background-size: cover;
background-position: center center;
Run Code Online (Sandbox Code Playgroud)
它适用于台式机,iPad和Android手机:
在iPhone上的Chrome和Safari上,背景太大了:
Mat*_*cca 62
你有这种情况background-attachment:fixed
.在移动设备上,我通常会放入查询background-attachment:scroll
内部@media
.
正如@RyanKimber指出的那样,固定附加图像使用整个<body>
尺寸.在移动设备上,这可能会变得非常高,从而将您的图像吹走.将附件设置为scroll
允许封面图像在其自己的容器内拉伸.
Vik*_*amp 50
在不添加任何新html
节点或使用@media
查询的情况下,只使用一个css来阐述Ryan的答案.
如果你想在包括iOS在内的所有设备上保持
cover
大小的fixed
背景,而不添加新的节点,那么诀窍就是在元素(主体)本身而不是背景上进行固定定位,因为固定的背景和封面尺寸混乱iOS版.
它也像iOS上的魅力一样在制作中使用:https://www.doklist.com/
这段代码不起作用,因为iOS使用的是hight document
而不是viewport
:
body {
background: url(https://www.w3schools.com/css/trolltunga.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Run Code Online (Sandbox Code Playgroud)
现在这是魔术,body:after
是固定的,而不是背景:
body:after{
content:"";
position:fixed; /* stretch a fixed position to the whole screen */
top:0;
height:100vh; /* fix for mobile browser address bar appearing disappearing */
left:0;
right:0;
z-index:-1; /* needed to keep in the background */
background: url(https://www.w3schools.com/css/trolltunga.jpg) center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用身体本身,"位置:固定;溢出-y:滚动",但我不想弄乱身体的位置和我的整体布局.
所以在身体上这样做:以后是一个非常容易的修复.我已经在Mac和iOS上测试了解决方案,包括firefox,safari,chrome.
我还为此创建了一个带有2个示例的github repo:https://github.com/thesved/fixed-cover-background
小智 16
这里有一个简单的解决方法,使Safari浏览器中的图片能够正常显示(仅在Safari浏览器中滚动,而不是在其他媒体中固定)
@supports ( -webkit-touch-callout : none) {
.selector {
background-attachment:scroll
}
}
@supports not ( -webkit-touch-callout : none) {
.selector {
background-attachment: fixed;
}
}
Run Code Online (Sandbox Code Playgroud)
这也给我带来了许多问题.问题是iOS使用正文的整个高度和宽度而不是视口来决定大小.
我们的解决方案是创造一个新的<div class="backdrop"></div>
.
我们将背景应用于此div并给它定位:绝对; 顶部:0; 底部:0; 左:0; 右:0.
由于此div现在是视口的大小,因此background-size: cover
工作得很好.
这篇文章回答了您的问题:为什么 CSS background-size: cover 在 iOS 上的纵向模式下不起作用?
并非所有浏览器都能识别背景大小的 cover 关键字,因此,只需忽略它即可。
因此,我们可以通过将背景大小设置为 100% 宽度或高度(具体取决于方向)来克服该限制。我们可以定位当前方向(以及 iOS 设备,使用设备宽度)。有了这两点,我认为你可以在 iOS 上以纵向模式使用 CSS background-size:cover
请参阅该帖子以获取更多资源。
归档时间: |
|
查看次数: |
56121 次 |
最近记录: |