Skip to content

Instantly share code, notes, and snippets.

@NetOpWibby
Created August 28, 2017 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetOpWibby/f27d5d08352c4fe99f4cfbbf20d1df20 to your computer and use it in GitHub Desktop.
Save NetOpWibby/f27d5d08352c4fe99f4cfbbf20d1df20 to your computer and use it in GitHub Desktop.
//
// C E N T E R
// S E L E C T S
$.each($("select"), function () {
centerSelect($(this));
});
$("select").on("change", function () {
centerSelect($(this));
});
function getTextWidth(txt) {
const
element = $("<span style='display: inline-block;'>" + txt + "</span>").prependTo("main"),
elementWidth = element.width();
element.remove();
return elementWidth;
}
function centerSelect(element) {
const options = element.children();
let
optionWidth,
emptySpace;
for (var i = 0; i < options.length; i++) {
const oo = options[i];
if (oo.selected === true) {
optionWidth = getTextWidth(oo.text);
emptySpace = element.width() - optionWidth;
}
}
element.css("text-indent", (emptySpace / 2) - 30);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment