Skip to content

bgackfround-attachment:fixed不兼容问题

在一些ios和某些移动端浏览器中,背景图片的fixed属性不起作用,图片会随着页面的滚动而滚动。

平时的写法:

css
body{
	width:100%;
	background-image:url("图片的地址");
	background-repeat:no-reapeat;
	background-position:center;
	background-attachment:fixed;
	z-index:-1;
}
body{
	width:100%;
	background-image:url("图片的地址");
	background-repeat:no-reapeat;
	background-position:center;
	background-attachment:fixed;
	z-index:-1;
}

兼容的写法:

css
body:before{
	content:'';
	position:fixed;
	z-index:-1;
	top:0;
	right:0;
	bottom:0;
	left:0;
	background:url("图片的地址") center 0 no-repeat;
	background-size:cover;
}
body:before{
	content:'';
	position:fixed;
	z-index:-1;
	top:0;
	right:0;
	bottom:0;
	left:0;
	background:url("图片的地址") center 0 no-repeat;
	background-size:cover;
}

程序员小洛文档