【问题标题】:Background-Attachment: Fixed Doesn't Work On iOS背景附件:已修复在 iOS 上不起作用
【发布时间】:2017-05-17 03:42:37
【问题描述】:

我正在尝试解决我在 iOS 设备上遇到的固定背景问题。我宁愿不必为这个网站重新设计所有东西,我希望一些 CSS 更改可以修复它。 This 是网站在 iPhone 上的样子,this 是它应该的样子。我使用的 CSS 代码如下:

.container {
    min-width: 320px;
    max-width: 480px;
    margin: 0 auto;
}

.fixed-background {
    height: 800px;
    -webkit-backgound-size: cover;
    -o-backgound-size: cover;
    -moz-backgound-size: cover;
    background-size: cover;
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-position: center center;
    text-align: center;
    overflow: auto;
}

我还尝试使用 @media 查询来修复 iOS 使用 stackoverflow 上的一些帖子,但这似乎没有任何效果:

@media screen and (min-color-index:0) and (-webkit-min-device-pixel-ratio:0) {
    .fixed-background {
        background-attachment: scroll;
    }
} 

HTML

<div class="fixed-background bg-1">
    <div class="container">
        <div class="title">
            <h1>ROOK PROPERTY<br>MANAGEMENT INC.</h1>
            <h2>CONDOMINIUM MANAGEMENT</h2>
        </div>
    </div>
</div>

【问题讨论】:

标签: html ios css iphone background-attachment


【解决方案1】:

对于我所有具有固定背景的 div,我添加了 class="parallax iparaxify paraxify" 类 在我的主 css 文件中,我有:

.parallax {
width: 100%;
background url(../images/bg.jpg) no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
} 

最后让它为除 i 产品之外的所有东西都设置视差

.paraxify {
  background-attachment: fixed;
  background-position: center center;
  background-size: cover;
}

最后使用 jquery 为 ipad、iphone 和 ipod 停用 position:fixed

// adds mobile class, and mobile os to html tag
jQuery(document).ready(function($){
var deviceAgent = navigator.userAgent.toLowerCase();

if (deviceAgent.match(/(iphone|ipod|ipad)/)) {
$('.iparaxify').removeClass('paraxify');
}
});

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题,这就是我解决它的方法。

    首先,您需要将 body 和 html 声明为 100% 宽和 100% 高:

    html, body{
    	width: 100%;
    	height: 100%;
    }

    然后,页面上的滚动不能由 body 完成:您必须将其包装在容器上。这个容器需要三个参数:overflow:scroll, width: 100% and height: 100%。我建议将整个网站包装在其中:

    #wrapper{
    		width: 100%;
    		height: 100%;
    		overflow: scroll;
    	}

    如果你不喜欢它的滚动方式,你也可以尝试添加 -webkit-overflow-scrolling:触摸。

    希望对您/任何来寻找此内容的人有所帮助!

    【讨论】:

    • 解决方案对我不起作用。另请注意,此解决方案可能会导致其他滚动效果出现问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多