代码之家  ›  专栏  ›  技术社区  ›  user5762586

仅当定位点滚动时,Safari桌面上的粘性标题才会闪烁

  •  5
  • user5762586  · 技术社区  · 7 年前

    我已经在Adobe Muse中建立了一个网站,当滚动经过徽标时,会出现一个粘性标题。这在Chrome和Firefox上运行得很好,甚至在iPad Safari上也是如此。然而,它在桌面Safari上运行得并不好,当点击一个平稳滚动到锚点的锚点链接时,它的闪烁效果很差。

    请参见以下示例网站:

    http://mattstest03.businesscatalyst.com/index.html

    在Firefox/Chrome上点击“联系我们”时,整个平滑滚动页面上的粘性标题看起来都很棒。在Safari上,它在滚动过程中闪烁开/关。下面是闪烁效果的GIF:

    enter image description here

    这是我的代码:

    CSS

    #sticky-bar {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 53px;
        background: transparent url("assets/photoshop-eclipse.jpg") repeat left top;
    }
    /* Circle Logo */
    #u73837 {
        width: 57px;
        transition: width 0.4s, height 0.4s;
        -moz-transition: width 0.4s, height 0.4s;
        -webkit-transition: width 0.4s, height 0.4s;
    }
    /* Text Logo */
    #u56099 {
        width: 229px;
        transition: all 0.4s !important;
        -moz-transition: all 0.4s !important;
        -webkit-transition: all 0.4s !important;
    }
    /* Sticky Contact Us */
    .sticky-contact {
        position: fixed !important;
        top: 9px !important;
        left: -160px !important;
        padding-bottom: 4px !important;
        margin-top: 0 !important;
        transition: all 0.4s;
        -moz-transition: all 0.4s;
        -webkit-transition: all 0.4s;
    }
    .sticky-contact:before {
        content: "We'd love to talk";
        position: absolute;
        left: calc(-100% - 30px);
        top: 8px;
        color: #eee;
        font-family: 'Raleway', 'Open Sans';
        font-size: 17px;
    }
    .contact-inner {
        margin-top: 4px !important;
        font-size: 17px !important;
        transition: all 0.4s;
        -moz-transition: all 0.4s;
        -webkit-transition: all 0.4s;
    }
    /* Circle Logo Transition */
    .smaller-logo {
        position: fixed !important;
        top: 7px !important;
        width: 40px !important;
        height: 40px !important;
    }
    /* Normal Circle Logo */
    .normal-logo {
        width: 57px;
        height: 57px;
    }
    /* Smaller Text */
    .smaller-text {
        width: 0 !important;
    }
    

    var width = window.innerWidth;
    
    if (width > 1000) {
        if (jQuery(window).scrollTop() > (jQuery('#u106240').offset().top - 15)) {
            // Fade in sticky bar
            jQuery('#sticky-bar').css('display', 'block');
            // Re-position 'Contact Us'
            jQuery('#buttonu206').addClass('sticky-contact');
            jQuery('#u200-4').addClass('contact-inner');
            // Hide logo text
            jQuery('#u56099').css('display', 'none');
            // Animate circle logo (CSS)
            jQuery('#u73837').removeClass('normal-logo');
            jQuery('#u73837').addClass('smaller-logo');
        } else {
            // Fade out sticky bar
            jQuery('#sticky-bar').css('display', 'none');
            // Re-position 'Contact Us'
            jQuery('#buttonu206').removeClass('sticky-contact');
            jQuery('#u200-4').removeClass('contact-inner');
            // Show logo text
            jQuery('#u56099').css('display', 'initial');
            // Animate circle logo (CSS)
            jQuery('#u73837').removeClass('smaller-logo');
            jQuery('#u73837').addClass('normal-logo');
        }
    }
    

    请注意,这与JavaScript代码的滚动部分(第4行)没有任何关系,因为我在测试时删除了它,问题仍然存在。

    我已经尝试了几个CSS“修复”的 sticky-bar -webkit-transform: translate3d(0,0,0) -webkit-translateZ(0) 但我一点运气都没有。有谁能提供见解吗?非常感谢。

    1 回复  |  直到 7 年前
        1
  •  4
  •   Duannx    7 年前

    position: fixed know issue translate3d(0,0,0) 因为元素是一种漫游,但它并不完美。当你滚动时,它仍然很奇怪。所以我的建议是使用 position: absolute 相反只要把你的酒吧移出你的内容容器,然后给它 位置:绝对

    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }
    
    .fixed-bar {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 50px;
      background-color: #123;
      color: #FFF;
      text-align: center;
      line-height: 50px;
      z-index: 1;
    }
    
    .content {
      background-color: #ddd;
      color: #333;
      width: 100%;
      padding-top: 50px;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      overflow: auto;
    }
    
    .item {
      width: 100%;
      text-align: center;
      height: 200px;
      height: 30vh;
      padding-top: 10vh;
    }
    <div class="fixed-bar">
      I am a fixed bar
    </div>
    <div class="content">
      <div class="item">
        Your content goes here
      </div>
      <div class="item">
        Your content goes here
      </div>
      <div class="item">
        Your content goes here
      </div>
      <div class="item">
        Your content goes here
      </div>
      <div class="item">
        Your content goes here
      </div>
    </div>