如何强制 Safari 重新绘制位置:固定滚动上的元素



我在桌面和移动设备上遇到 Safari 问题,当用户滚动时,使用 position:fix 重新绘制元素的速度非常慢。

带有 position:fixed 的元素是 .portfolio-item .expanded-content 的 #intro 和页脚元素。滚动上的 #intro 不一定会重新绘制为正确的 z 索引(当用户滚动时,它应该位于其他元素的后面)。移动设备上的页脚元素不会停留在 iOS 野生动物园滚动内容上方的固定位置。在 iOS safari 上滚动是参差不齐的(然而,iOS chrome 是流畅的,一切都按预期工作)。

做了一个小提琴,我去掉了所有的图像、字体和 JS,你瞧,safari 在重新绘制位置:滚动上的固定元素方面没有问题。

由于这是一个投资组合网站,因此删除我的图像显然不是一种选择。我真的希望使它成为一个真正的单页网站,而不是使用 AJAX 或任何内容来按需加载内容。我是否要求太多的野生动物园拥有那么多元素并能够重新绘制带有位置:固定在滚动上的元素?Chrome和FF似乎没有问题;IE9、10、11 也没有。

我不完全确定这是一个重绘问题,但你可以在下面的视频中看到,如果我通过触发一个不滚动的事件(如鼠标悬停事件)来强制 Safari 重绘,它会重绘,并将该位置:固定元素放在我在样式表中指定的 z-index 中。所以这个事实,再加上小提琴工作得很好,这就是为什么我假设这是一个重新绘制的问题,而不是我的代码问题。

我希望找到一种方法来解决此问题,而无需诉诸更多的JS或动态加载的内容,保持相同的设计(不要仅仅因为一个浏览器出现问题而放弃使用position:fixed或流畅布局的想法)并尝试保持性能快速流畅。我想过每次用户滚动以强制 safari 重新绘制时使用 JS,但在我看来,这会对所有浏览器的性能产生负面影响。我真的可以用其他人的想法和观点来做这件事。

网站: http://sarahjean.co

小提琴:http://jsfiddle.net/sjc8611/n9z3W/

视频:https://dl.dropboxusercontent.com/u/24724104/position-fixed-repaint-lag-safari.mov

该网页:

    <nav data-scroll-header="" id="main-navigation">
    <ul>
        <li><a href="#work" data-scroll="">Work</a>
        </li>
        <li><a href="#about" data-scroll="">About</a>
        </li>
        <li><a href="#services" data-scroll="">Services</a>
        </li>
        <li><a href="#contact" data-scroll="">Contact</a>
        </li>
    </ul>
</nav>
<div class="header" id="header">Header Content</div>
<section id="intro" class="container">
    <article class="content">
            <h1>Introduction Text</h1>
        <p>Welcome to my super cool portfolio site. Check out how awesome I am. You should totally hire me.</p>
    </article>
</section>
<section id="work" class="container">
    <article class="content">
            <h1>Work</h1>
        <nav id="portfolio-navigation">
            <ul>
                <li><a href="#work1">See My Work 1</a>
                </li>
                <li><a href="#work2">See My Work 2</a>
                </li>
            </ul>
        </nav>
    </article>
    <article id="work1" class="portfolio-item">
        <div class="expanded-content">
                <h2 class="center">Here is some of my work!</h2>
            <p>Lorem ipsum dolor sit amet..</p>
            <footer><a href="#work">Close</a>
            </footer>
        </div>
    </article>
    <article id="work2" class="portfolio-item">
        <div class="expanded-content">
                <h2 class="center">More of my cool work!</h2>
                <h1>Proin Quis Tortor Orci. Etiam At Risus</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit..</p>
            <footer><a href="#work">Close</a>
            </footer>
        </div>
    </article>
</section>
<section id="contact" class="container">
    <article class="content">
            <h1>Contact</h1>
        <ul id="contact-list">
            <li>I would include a list of ways to contact me here</li>
            <li>Emails</li>
            <li>Telephones</li>
            <li>The postal services</li>
        </ul>
    </article>
</section>
<section id="services" class="container">
    <article class="content">
         <h1>Services</h1>
        <p>Lorem ipsum dolor sit amet..</p>
    </article>
</section>

CSS:

body {
    background: #fff8ec;
    margin: 0 auto;
    height: 100%;
}
html {
    font-family: Arial, sans-serif;
    font-size: 14px;
    line-height: 135%;
    color: #4b3d2f;
    height: 100%;
}
h1, h2, h3, h4, h5 {
    font-family: Arial, sans-serif;
}
h1 {
    color: #aba499;
    text-align: center;
    font-size: 2em;
}
.portfolio-item h2 {
    font-size: 1.8em;
}
a, a:link, a:visited {
    color: #c85128;
    text-decoration: none;
}
a:hover {
    color: #4b3d2f;
}
p {
    margin: 1em 0;
    line-height: 135%;
}
img {
    max-width: 100%;
    height: auto;
}
.container {
    width: 100%;
    position: relative;
    background-color: #e5e2de;
    padding: 100px 0;
}
.container > .content {
    width: 80%;
    margin: 0 auto;
    max-width: 800px;
    background-color: transparent;
}
#header {
    background-color: #c85128;
    height: 95%;
    position: relative;
    z-index: 3;
    display: table;
    width: 100%;
}
#intro {
    background-color: transparent;
    position: fixed;
    top: 5%;
    left: 0px;
    height: 25%;
    padding: 5% 0;
    z-index: 0;
}
#intro > .content {
    background-color: #fff8ec;
    width: 70%;
    padding: 5%;
    border-radius: 20px;
    box-shadow: 0px 1px 3px #e5e2de;
}
#work {
    margin-top: 55%;
    background-color: #dedad5;
}
#contact {
    background-color: #d8d3cd;
}
#services {
    background-color: #d1cbc4;
}
#about {
    background-color: #cac4bc;
}
section h1 {
    padding: 50px 0;
}
article .expanded-content h2, article .expanded-content p {
    margin: 50px auto;
}
#main-navigation {
    display: table;
    width: 100%;
    background-color: #aba499;
    position: fixed;
    top: 0;
    left: 0;
    height: 3em;
    overflow: visible;
    z-index: 2;
}
#main-navigation a {
    color: #fff8ec;
    font-family:'NovecentowideBookRegular', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    display: block;
}
#main-navigation a:hover {
    color: #4b3d2f;
    text-shadow: 0em -0.05em 0em #e5e2de;
}
#main-navigation ul {
    display: table-row;
    height: 3em;
    overflow: visible;
}
#main-navigation ul li {
    display: table-cell;
    width: 20%;
    padding: .8em;
    text-align: center;
    vertical-align: middle;
}
.portfolio-item {
    max-height: 0px;
    height: 0px;
    overflow: hidden;
    position: fixed;
    top: 3em;
    left: 0%;
    -webkit-transition: height 700ms ease;
    -moz-transition: height 700ms ease;
    -ms-transition: height 700ms ease;
    -o-transition: height 700ms ease;
    transition: height 700ms ease;
}
#work1:target, #work2:target {
    max-height: 1000px;
    opacity: 1;
    width: 80%;
    height: 70%;
    padding: 5%;
    top: 5%;
    left: 5%;
    background-color: #fff;
    box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.5);
    z-index: 10;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
}
#work1:target .expanded-content, #work2:target .expanded-content {
    max-width: 900px;
    margin: 0 auto;
}
#work1:target .expanded-content footer, #work2:target .expanded-content footer {
    display: block;
    width: 90%;
    text-align: right;
    background-color: #c85128;
    position: fixed;
    top: 5%;
    left: 5%;
    z-index: 11;
}
#work1:target .expanded-content footer a, #work2:target .expanded-content footer a {
    display: block;
    padding: 1em;
    color: #e5e2de;
    height: 1em;
}

> 你没有疯。我也遇到了position: fixed元素不重新绘制的问题。还没有找到解决方案。

编辑 找到了解决方案。您可以通过触发其大小的CSS动画来重新绘制几乎任何内容。这是我正在使用的代码片段:

.foo
  position: fixed
  &.active
    animation: repaint 1ms
@keyframes repaint
  from
    width: 99.999%
  to
    width: 100%

我在Safari 9.1中遇到了完全相同的问题。

在大多数情况下,延长动画执行时间对我有用。

@keyframes repaint {
  from {
    width: 99.999%
  }
  to {
    width: 100%
  }
}
.repaint {
  animation: repaint 5000ms;
}

但是,如果固定位置的 DOM 元素位于高度发生变化的父元素内部(例如,动态添加新的 DOM 元素时父元素的高度可能会改变),那么即使将动画时间延长到不合理的值,它对我也不起作用。

我的最终解决方案是放弃animation黑客并使用 JS 强制重绘

$('.repaint').hide().show(0);

如在Chrome/Mac上强制DOM重绘/刷新中所述

我使用AngularJS,为了让这个hack在所有情况下都起作用,我必须在每个摘要循环上调用这个.hide().show(0)

以AngularJS指令的形式破解:

function ForceRepaintDirective() {
  return {
    restrict: 'EA',
    link: function(scope, $element) {
      scope.$watch(function() {
        $element.hide().show(0);
      });
    }
  };
};
如果您

不知道什么是手写笔样式,因此无法使用@CorySimmons的解决方案,这里是上面代码的css版本。此外,我还必须更改一些值,显然高于iOS 8中的值

@-webkit-keyframes repaint {
    from {
        width: 99.9%;
    }
    to {
        width: 100%;
    }
}
@-moz-keyframes repaint {
    from {
        width: 99.9%;
    }
    to {
        width: 100%;
    }
}
@keyframes repaint {
    from {
        width: 99.9%;
    }
    to {
        width: 100%;
    }
}
.repaint {
    -webkit-animation: repaint 100ms;
       -moz-animation: repaint 100ms;
        -ms-animation: repaint 100ms;
            animation: repaint 100ms;
}

您需要做的就是在需要重绘时为固定元素提供一类 .repaint。就我而言,这是一个使用 jQuery 的 scrollTop() 从我的刊头中添加和删除类的粘性导航,所以在需要时,jquery 函数还会将 .repaint 类添加到我的刊头,它为我解决了这个问题。

最新更新