【问题标题】:sticky-kit flashes when sticky粘性时,sticky-kit 会闪烁
【发布时间】:2017-08-09 09:28:55
【问题描述】:

我在使用sticky-kit v1.1.2 的测试页面上显示了一些 AdSense 广告。

当我向下滚动页面时(我使用的是 Chrome 浏览器),当粘性工具包变得粘性时,广告会闪烁/重新加载。

我已阅读此trouble shooting guide,它似乎确实解决了我遇到的确切问题,但不适用于我的代码。

我的代码如下所示。我故意省略了以下两个 div,因为我不确定它们应该插入到哪里(我尝试了很多场景,但没有一个适合我):

<div class="sticky-parent">
  <div class="sticky-spacer">
  </div>
</div>

这是我的 HTML 代码:

<div id="id_side_advert_container" class="side_advert col-md-2">
    <div class="margin-bottom-20">

        <div id="id_side_advert_wrapper">
            {# google adsense code to display side advertiements #}
            <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
            <!-- zoodal-side-advertisement-responsive -->
            <ins class="adsbygoogle"
                 style="display:block"
                 data-ad-client="ca-pub-1234567890112987"
                 data-ad-slot="1234567890"
                 data-ad-format="auto"></ins>
            <script>
            (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
        </div>
    </div>
</div>

这是我的 JQ 代码:

// sticky-kit code for the side advertising sticky - with an offset of 15px when sticky.
$("#id_side_advert_container").stick_in_parent({
    offset_top: 15,
    parent: ".sticky-parent", // note: we must now manually provide the parent
    spacer: ".sticky-spacer",
});

// the following code is the workaround so that the sticky element does not disappear when bottom -
// taken from: https://github.com/leafo/sticky-kit/issues/31 danxshap
$('#id_side_advert_container')
.on('sticky_kit:bottom', function(e) {
    $(this).parent().css('position', 'static');
})
.on('sticky_kit:unbottom', function(e) {
    $(this).parent().css('position', 'relative');
})

【问题讨论】:

  • sticky_kit:unbottom事件回调期间,可能position设置为inherit而不是relative,以防父css没有'它的 position 之前设置为 relative
  • 我认为您想将class="sticky-parent" 添加到id_side_advert_container 并将带有class="margin-bottom-20" 的div 更改为class="margin-bottom-20 sticky-spacer"
  • 尝试将will-change: margin 添加到#id_side_advert_container 的CSS。这在过去为我解决了一些性能故障。
  • 我看不到它是否闪烁,尝试使用虚拟客户端 id ca-digitalinspiration 创建 jsfiddle

标签: javascript jquery sticky


【解决方案1】:

下面显示了您问题中的 solution 应该如何设置才能正常工作(这里我使用 iframe 来模拟您的广告)。

$("#sidebar").stick_in_parent({
  offset_top: 15,
  parent: ".content", // note: we must now manually provide the parent
  spacer: ".sticky-spacer",
});

// the following code is the workaround so that the sticky element does not disappear when bottom -
// taken from: https://github.com/leafo/sticky-kit/issues/31 danxshap
$('#id_side_advert_container')
  .on('sticky_kit:bottom', function(e) {
    $(this).parent().css('position', 'static');
  })
  .on('sticky_kit:unbottom', function(e) {
    $(this).parent().css('position', 'relative');
  })
.content {
  overflow: hidden;
}

.content .sidebar {
  width: 200px;
  margin: 10px;
  margin-right: 0;
  float: left;
  overflow: hidden;
  font-family: sans-serif;
}

.content .main {
  margin: 10px;
  margin-left: 222px;
  border: 1px solid blue;
  height: 2000px;
  overflow: hidden;
}

.footer {
  margin: 10px;
  text-align: center;
  font-size: 13px;
  border-top: 1px dashed #dadada;
  color: #666;
  padding-top: 10px;
  min-height: 233px;
}

.sub {
  color: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/leafo/sticky-kit/v1.1.2/jquery.sticky-kit.min.js"></script>

<h1>Example sticky flicker fix</h1>
<div class="content">
  <div class="sticky-spacer">
    <div id="sidebar" class="sidebar">
      <em>Ads go here. <span style="color:maroon">Demo'ing with an iFrame instead.</span></em>
      <iframe frameborder="0" scrolling="no" width="100%" height="100%" src="https://unsplash.it/200/200" name="imgbox" id="imgbox">
        <p>iframes are not supported by your browser.</p>
      </iframe>
    </div>
  </div>
  <div class="main">
    This is the main column
    <p class="sub">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus id leo et aliquam. Proin consectetur ligula vel neque cursus laoreet. Nullam dignissim, augue at consectetur pellentesque, metus ipsum interdum sapien, quis ornare quam enim vel ipsum.
    </p>
    <p class="sub">
      In congue nunc vitae magna tempor ultrices. Cras ultricies posuere elit. Nullam ultrices purus ante, at mattis leo placerat ac. Nunc faucibus ligula nec lorem sodales venenatis. Curabitur nec est condimentum, blandit tellus nec, semper arcu. Nullam in
      porta ipsum, non consectetur mi. Sed pharetra sapien nisl. Aliquam ac lectus sed elit vehicula scelerisque ut vel sem. Ut ut semper nisl.
    </p>
    <p class="sub">
      Curabitur rhoncus, arcu at placerat volutpat, felis elit sollicitudin ante, sed tempus justo nibh sed massa. Integer vestibulum non ante ornare eleifend. In vel mollis dolor.
    </p>
  </div>
</div>
<div class="footer">
  My very tall footer!
</div>

有了这个,我在 Firefox 或 Chrome 中看不到任何闪烁。

作为测试,我建议您尝试将广告放在此 JSFiddle 中(与上面的 sn-p 代码相同):https://jsfiddle.net/2jkf4qcr/1/

【讨论】:

  • @K Scandrett,非常感谢您的回答。上面的确切代码并不完全适用于我的场景,但您的代码绝对让我走上了理解和克服问题的正确道路。
【解决方案2】:

我在sticky-kit 库中遇到了类似的问题。解决方案是不使用垫片 (spacer: false):

<div class="sticky-parent">
</div>

$("#id_side_advert_container").stick_in_parent({
    offset_top: 15,
    parent: ".sticky-parent",
    spacer: false,
});

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2019-06-29
    相关资源
    最近更新 更多