背景附件的替代方案:已修复

时间:2014-01-20 17:30:55

标签: html css background-attachment

由于后台附件:已修复在某些移动浏览器上无效(例如在iOS上),因此有哪些替代方案,特别是在尝试实施以下演示时:http://cppforums.ludost.net/test/test.html

CSS:

.sec1 { 
  min-height: 1000px;
  background: #222 url('wallpaper-2959361.jpg') no-repeat center top fixed;
}

.sec2 { 
  min-height: 1000px;
  background: #222 url('wallpaper-1829278.jpg') no-repeat center top fixed;
}

HTML:

<div class="sec1">text</div>
<h1>Title 1</h1>
<div class="sec2">more text</div>
<h1>Title 2</h1>
<div class="sec1">even more text</div>

有关如何以适用于所有浏览器的方式实现演示的任何建议?最好是仅限CSS的解决方案。

2 个答案:

答案 0 :(得分:0)

替代background-attachment:fixed;的jQuery(确保正确安装了jQuery):

$(window).scroll(function() {
     var scrolledY = $(window).scrollTop();
     $('.sec1').css('background-position', 'center ' + ((scrolledY)) + 'px');
});

重要的是要注意你的背景图像不得高于div的高度,否则会被抛弃。

答案 1 :(得分:0)

    .bg 
    {
      background-image: url(bg.jpg);
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      position: fixed;
      top: 0px;
      bottom: 0px;
      left: 0px;
      right: 0px;
      z-index: -1;
      -webkit-transform: translateZ(0);
      pointer-events: none;
    }

请参阅此示例:https://rawgit.com/arnaudbreton/background-fixed-chrome-rendering-issue/master/index-without-rendering-issue.html

取自: http://blog.mention.com/building-a-beautiful-homepage-how-we-nailed-down-chrome-performance-rendering-issues/