简体   繁体   English

Bootstrap Popover在页面滚动期间闪烁

[英]Bootstrap Popover flickers during page scroll

I am using Twitter Bootstrap to indicate the part of the article currently shown on screen. 我正在使用Twitter Bootstrap指示当前在屏幕上显示的文章部分。 The popover messages are always shown, irrespective of the click being made. 总是显示弹出消息,而与单击无关。 The problem I am facing is that the popover flickers during scroll. 我面临的问题是滚动过程中弹出窗口闪烁。 I want the popover to be stable and to show messages without flicker. 我希望弹出窗口稳定并显示消息而不会闪烁。 How do I do it ? 我该怎么做 ?

Bootstrap Popover : Bootstrap Popover:

<a href="#" id="example" class="btn btn-primary" rel="popover" data-content="This is the body of Popover"  data-placement="left"

jQuery code : jQuery代码:

$(function () {
    $('#example').popover();
});

$(window).scroll(function() {  
    var current = $('[name = "scroll"]');
    if($(window).scrollTop() > 0 ) {
        current.attr("data-content", "Introduction");
        current.popover("show");
    }
    if($(window).scrollTop() > 620 ) {
        current.attr("data-content", "Main Passage");
        current.popover("show");
    }
});

Here's the working demo : http://jsfiddle.net/abyz19ja/ 这是工作示例: http : //jsfiddle.net/abyz19ja/

It flickers because you are constantly calling the popover's show function on every scroll action. 之所以闪烁,是因为您在每次滚动操作时都不断调用弹出框的show函数。 What you need to do is use a flag or a counter and only tell the popover to show when it's content has been updated. 您需要做的是使用一个标志或一个计数器,并且仅告诉弹出框显示其内容已更新的时间。

something like this: 像这样的东西:

$(function () {
    $('#example').popover();
});
var counter = 0;
$(window).scroll(function() {
    var pos = $(window).scrollTop(); 
    var current = $('[name = "scroll"]');
    if(pos >= 0 && pos < 619) {
      if (current.attr("data-content") != "Introduction") {
        current.attr("data-content", "Introduction");
        current.popover("show");
        counter == 0;
      }
    } else {
      if (current.attr("data-content") != "Main Passage") {
        current.attr("data-content", "Main Passage");
        current.popover("show");
        counter == 0;
      }
    }

    if (counter === 0){
    current.popover("show");
    counter = counter+1;
    }

});

You also need to position the popover as fixed so that it stays in the same place as it is not recalculating on every scroll position change anymore. 您还需要将弹出框固定放置,以使其不再位于每次滚动位置更改时都不会重新计算的位置。

.popover {
  position:fixed
}

Now if you can, try to make this more explicit by adding a parent container, eg: 现在,如果可以的话,请尝试通过添加父容器来使其更加明确,例如:

#container .popover {
 position:fixed;
}

Otherwise it will apply to all the popovers on your page/site, depending on how you implement your css and whether you have any other popovers or not. 否则,它将应用于您的页面/站点上的所有弹出窗口,具体取决于您实现CSS的方式以及是否有其他弹出窗口。

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

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