Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap-select plugin: how to avoid flickering

The Bootsrap-select plugin is amazing (http://silviomoreto.github.io/bootstrap-select/). It provides an extremely easy way of creating gorgeous select menus in 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)
  2. The Bootstrap-select plugin JS runs
  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).

So, the user first sees the HTML select element and then sees it switch to the pretty Bootstrap-select item, thus the "flickering".

Has anyone found a good solution to the problem?

like image 988
hackingForward Avatar asked Apr 23 '14 17:04

hackingForward


People also ask

How do I use bootstrap select in jQuery?

Bootstrap-select requires jQuery v1.8.0+, Bootstrap’s dropdown.js component, and Bootstrap’s CSS. The plugin can be imported directly in your project from the CDN (links on the official site). You have to import a CSS file to style the select boxes and a Javascript file to make the entire thing work.

How to add a select picker in Bootstrap 4?

When having to add a select picker, the first option is the default Bootstrap 4 component. This will not style the dropdown menu where the options appear though. Another popular option is to create your own classes and add them to the <select> tag for restyling.

How to add custom styles to bootstrap select input fields?

1. Bootstrap-select by Silvio Moreto. The first plugin that can help us add custom styles and additional functionalities to our Bootstrap select input fields is called “Bootstrap-select”. It’s a jQuery plugin that uses Bootstrap’s dropdown.js library.

What is the boostrap select picker plugin?

The Boostrap Select Picker plugin is a library for supporting work with select inputs. The basic setup will show a simple select box and the styling for the dropdown will be the same as the one for the Bootstrap 4 dropdown. This is a plus, since your design will be consistent across your project.


2 Answers

The best and most simple answer:

Add the existing Bootstrap class invisible to your Select object on the markup page, whether it's an HTML Input element or HTML Select element: https://getbootstrap.com/docs/5.1/utilities/visibility/

e.g.

<input class="visible">...</div>
<input class="invisible">...</div>

This prevents the HTML version of the Select/DDL from being visible to the user, while reserving the space in the layout that the SELECT would take up (which is rendered in once the bootstrap-select JS is initialized).

like image 134
BassGod Avatar answered Sep 19 '22 21:09

BassGod


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!

like image 44
Alex Avatar answered Sep 20 '22 21:09

Alex