简体   繁体   English

固定位置元件的性能较差

[英]poor performance of fixed position elements

I have an issue with a fixed element on my website when scrolling when browsing with Chrome. 使用Chrome浏览时滚动时,我的网站上的固定元素存在问题。

When scrolling up and down the page the element flickers and duplicates itself. 上下滚动页面时,该元素会闪烁并自我复制。

Usually when faced with this issue I have normally been able to resolve the issue with a simple z-index but this is not making any difference for this particular issue. 通常,当遇到此问题时,我通常可以使用简单的z-index解决此问题,但这对于此特定问题没有任何影响。

Website Code Fiddle 网站代码小提琴

Actual Website The element in question is the black scroll element on the left hand side. 实际网站所涉及的元素是左侧的黑色滚动元素。

Simplified Fiddle to isolate the issue 简化小提琴以隔离问题

Here is the code from the simplified fiddle to replicate the issue: 这是简化的小提琴中用来复制问题的代码:

// HTML

 <ul id="et-float-menu">
     <li class="et-float-menu-item1">
        <a id="scrollUp">
            <span><img /></span>
        </a>
    </li>
    <li class="et-float-menu-item2">
        <a id="scrollDown">
            <span><img /></span>
        </a>
    </li>
</ul>

<div class="jumptosection selected" id="section1">
    <h2>Section 1</h2>
</div>    
<div class="jumptosection" id="section2">
    <h2>Section 2</h2>
</div>    
<div class="jumptosection" id="section3">
    <h2>Section 3</h2>
</div>    
<div class="jumptosection" id="section4">
    <h2>Section 4</h2>
</div>

// JS
function changeSelection(sectionFrom, sectionTo) {
    if(sectionTo.length > 0) {
        sectionFrom.removeClass("selected");
        sectionTo.addClass("selected");
        jQuery("body").animate({scrollTop: sectionTo.offset().top});
    }
}

jQuery(document).on("click", "#scrollDown", function(){
    var currentSection = jQuery(".selected");
    var nextSection = currentSection.next(".jumptosection");

    changeSelection(currentSection, nextSection);

    return false;
});

jQuery(document).on("click", "#scrollUp", function(){
    var currentSection = jQuery(".selected");
    var prevSection = currentSection.prev(".jumptosection");

    changeSelection(currentSection, prevSection);

    return false;
});

// CSS

.jumptosection {
    height: 200px;
    background-color:#e8e8e8;
}

#et-float-menu {
position: fixed;
z-index: 11;
left: 0;
top: 45%;
background-color: #000;
padding: 20px 10px 10px 15px;
margin: 0;
-webkit-border-top-right-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
-moz-border-radius-topright: 8px;
-moz-border-radius-bottomright: 8px;
border-top-right-radius: 8px;
border-bottom-right-radius: 8px;
}

#et-float-menu a {
padding: 0;
clear: both;
float: left;
margin-bottom: 10px;
color: #fff;
}

#et-float-menu a:hover { color: #b2b2b2; transition: color 300ms ease 0s; }

#et-float-menu li {
display: block;
margin-left: 0;
}


.et-float-menu-item a { display: inline-block; font-size: 24px; position: relative; text-align: center; transition: color 300ms ease 0s; color: #fff; text-decoration: none; }
.et-float-menu-item a:hover { color: #a0a0a0; }
    .et-social-icon span { display: none; }
.et-float-menu-item1 a:before { content: '↑';font-size:22px; }
.et-float-menu-item2 a:before { content: '↓';font-size:22px; }

Is anyone aware of the cause of this issue and a possible resolution? 是否有人知道此问题的原因以及可能的解决方法?

I managed to replicate the issue in Chrome and using this Link I added -webkit-backface-visibility: hidden; 我设法在Chrome中复制了该问题,并使用此链接添加了-webkit-backface-visibility: hidden; to the UL element and it seemed to fix the issue.. for me at least. UL元素,它似乎可以解决此问题..至少对我来说。 Could you give it a try. 你可以试试看。

A good explanation of the issue can be found on the above link and a more in-depth review of the problem can be found Here 这个问题的一个很好的解释可在上面的链接上发现问题的更深入的审查,可以发现这里

Excerpt from above link http://benfrain.com/improving-css-performance-fixed-position-elements/ : 摘录自上述链接http://benfrain.com/improving-css-performance-fixed-position-elements/

...adding backface-visibility: hidden; ...增加背面可见度:隐藏; to the fixed position elements. 到固定位置元素。 That was stopping the paint happening on scroll. 那阻止了绘画的滚动。 So, I had a nice simple solution for my own site but I was annoyed I didn't understand WHY that worked: I had my suspicions but no actual proof. 因此,我为自己的网站提供了一个很好的简单解决方案,但我很生气,我不明白为什么会这样:我有怀疑,但没有实际证据。 In these situations I always do the same thing; 在这种情况下,我总是做同样的事情。 ask someone way smarter. 问一个更聪明的人。

… when elements repaint, the dirty rectangle calculation is done per layer. …当元素重新绘制时,脏矩形的计算是按层进行的。 So if an element is on its own layer then it won't affect anything else. 因此,如果元素在其自己的层上,则不会影响其他任何元素。 If you promote a fixed header – say – then when content appears at the bottom of the page there is only the new content that needs to be painted. 如果您提倡使用固定的标题(例如),则当内容显示在页面底部时,仅需要绘制新内容。 Without the promotion the header needs to be repainted at the top of the page. 如果没有促销,则标题需要在页面顶部重新绘制。 You might wonder why we don't automatically promote fixed position elements. 您可能想知道为什么我们不自动提升固定位置元素。 The answer is: we do for high DPI screens, but we don't for low DPI because we lose sub-pixel antialiasing on text, and that's not something we want to by default. 答案是:对于高DPI屏幕,我们这样做,但对于低DPI,我们不这样做,因为我们在文本上失去了亚像素抗锯齿功能,而这在默认情况下不是我们想要的。 On high DPI screens you can't tell, so it's safe there. 在高DPI屏幕上您无法分辨,因此在那里很安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM