Created
August 28, 2017 04:07
-
-
Save NetOpWibby/f27d5d08352c4fe99f4cfbbf20d1df20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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