简体   繁体   English

Bootstrap-select 插件:如何避免闪烁

[英]Bootstrap-select plugin: how to avoid flickering

The Bootsrap-select plugin is amazing ( http://silviomoreto.github.io/bootstrap-select/ ). Bootsrap-select 插件很棒( http://silviomoreto.github.io/bootstrap-select/ )。 It provides an extremely easy way of creating gorgeous select menus in Bootstrap.它提供了一种在 Bootstrap 中创建华丽选择菜单的极其简单的方法。 The one problem I've encountered with it, however, is "flickering" on page load.然而,我遇到的一个问题是页面加载时“闪烁”。 What I mean by this is fairly straight forward:我的意思很简单:

  1. The page loads with original HTML select element (which of course looks like crap)页面加载了原始的 HTML 选择元素(当然看起来像废话)
  2. The Bootstrap-select plugin JS runs Bootstrap-select 插件 JS 运行
  3. At some noticeable time after the page loads the original HTML select element is converted to a nice Bootstrap-select element by JS in Step (2).在页面加载后的某个明显时间,原始 HTML 选择元素在步骤 (2) 中被 JS 转换为一个不错的 Bootstrap-select 元素。

So, the user first sees the HTML select element and then sees it switch to the pretty Bootstrap-select item, thus the "flickering".因此,用户首先看到 HTML select 元素,然后看到它切换到漂亮的 Bootstrap-select 项,因此“闪烁”。

Has anyone found a good solution to the problem?有没有人找到解决问题的好方法?

To show a blank until bootstrap-select is loaded, add this to your css. 要在加载bootstrap-select之前显示空白,请将其添加到css中。

select {
   visibility: hidden;
}

There's not a great way to prevent the flickering all together, but you can minimize it. 没有一种很好的方法可以防止闪烁在一起,但你可以最小化它。

The problem is that in addition to whatever other bottlenecks you have, you have to download and parse a lot of javascript files first. 问题是,除了你遇到的任何其他瓶颈外,你必须先下载并解析很多javascript文件。 Bootstrap-Select depends on Bootstrap which itself depends on jQuery . Bootstrap-Select取决于Bootstrap ,它本身依赖于jQuery By the time all that javascript is loaded, the page is probably already rendered. 加载所有javascript时,页面可能已经呈现。

One thing you can do to minimize the flickering is to style the select elements as similar to the final product as possible so they take the same amount of screen space. 您可以采取的最小化闪烁的方法是将select元素设置为与最终产品类似,以便它们占用相同数量的屏幕空间。 When the plugin loads, it will hide this control anyway and replace it with it's own implementation. 当插件加载时,它将隐藏此控件并将其替换为它自己的实现。 Use the selector that you were going to pass into the selectpicker() function and style like this: 使用您要传递到selectpicker()函数的选择器和样式,如下所示:

.selectpicker {
    width: 220px;
    height:34px;
    margin-bottom: 10px;
}

Working Demo in fiddle 工作演示小提琴

截图

Note: I've intentionally delayed the load time by using setTimeout so the difference is more noticeable. 注意:我故意通过使用setTimeout延迟加载时间,因此差异更明显。

setTimeout(function() { $(".selectpicker").selectpicker(); }, 500);

If that's really still not acceptable, you can display a loading graphic until the page is fully loaded. 如果这仍然是不可接受的,您可以显示加载图形,直到页面完全加载。
Adapted from Loading screen example : 改编自加载屏幕示例

Add this to your HTML: 将其添加到您的HTML:

<div id="loader"></div> 

Style it like this in CSS: 在CSS中将它设置为样式:

#loader {
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background: url('http://i.imgur.com/ntIrgYXs.gif') 50% 50% no-repeat grey;
}

And remove it with the following JavaScript after you've loaded your page: 在加载页面后,使用以下JavaScript删除它:

$("#loader").fadeOut("slow");

Working Demo Of Loading Screen In Fiddle 加载屏幕的工作演示在小提琴

Use size to limit the height of the select , before it is being replaced: 在更换之前,使用size来限制select的高度:

<select size="1">
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
</select>

The spec around this attribute says: 该属性的规范说:

'size' Defines the number of visible options in a drop-down list 'size'定义下拉列表中的可见选项数

This solution works for me: 这个解决方案对我有用:

<select class="selectpicker">
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
</select>

<script>
    $('.selectpicker').selectpicker({  
        width: '100%'
    });
</script>

I just place the script right after the creation of the select drop-down, instead of waiting for the document to be ready! 我只是在创建选择下拉列表后立即放置脚本,而不是等待文档准备好!

(This answer should also work for this plugin, it does for Select2 ) (这个答案也适用于这个插件,它适用于Select2

Simply use the existing Bootstrap invisible class as described here: https://stackoverflow.com/a/70250132/15596537只需使用现有的 Bootstrap invisible类,如下所述: https : //stackoverflow.com/a/70250132/15596537

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

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