jquery.customSelect.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. * jquery.customSelect() - v0.3.3
  3. * http://adam.co/lab/jquery/customselect/
  4. * 2013-03-04
  5. *
  6. * Copyright 2013 Adam Coulombe
  7. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  8. * @license http://www.gnu.org/licenses/gpl.html GPL2 License
  9. */
  10. (function ($) {
  11. 'use strict';
  12. $.fn.extend({
  13. customSelect: function (options) {
  14. var defaults = {
  15. customClass: null,
  16. mapClass: true,
  17. mapStyle: true
  18. },
  19. changed = function ($select,customSelectSpan) {
  20. var currentSelected = $select.find(':selected'),
  21. // var currentSelected = $select.find('option').filter("[selected]"),
  22. customSelectSpanInner = customSelectSpan.children(':first'),
  23. html = currentSelected.html() || ' ';
  24. //$select.find(':selected').each(function () { console.log("found"); });
  25. //console.log("SelectHTML=" + escape(currentSelected.text()));
  26. customSelectSpanInner.html(html);
  27. setTimeout(function () {
  28. customSelectSpan.removeClass('customSelectOpen');
  29. }, 60);
  30. };
  31. if (typeof document.body.style.maxHeight === 'undefined') {
  32. /* filter out <= IE6 */
  33. return this;
  34. }
  35. options = $.extend(defaults, options);
  36. return this.each(function () {
  37. var $select = $(this),
  38. customSelectInnerSpan = $('<span class="customSelectInner" />'),
  39. customSelectSpan = $('<span class="customSelect" />');
  40. customSelectSpan.append(customSelectInnerSpan);
  41. $select.after(customSelectSpan);
  42. if (options.customClass) {
  43. customSelectSpan.addClass(options.customClass);
  44. }
  45. if (options.mapClass) {
  46. customSelectSpan.addClass($select.attr('class'));
  47. }
  48. if (options.mapStyle) {
  49. customSelectSpan.attr('style', $select.attr('style'));
  50. }
  51. $select
  52. .addClass('hasCustomSelect')
  53. .on('update', function () {
  54. changed($select,customSelectSpan);
  55. var selectBoxWidth = parseInt($select.outerWidth(), 10) -
  56. (parseInt(customSelectSpan.outerWidth(), 10) -
  57. parseInt(customSelectSpan.width(), 10));
  58. // Set to inline-block before calculating outerHeight
  59. customSelectSpan.css({
  60. display: 'inline-block'
  61. });
  62. var selectBoxHeight = customSelectSpan.outerHeight();
  63. if ($select.attr('disabled')) {
  64. customSelectSpan.addClass('customSelectDisabled');
  65. } else {
  66. customSelectSpan.removeClass('customSelectDisabled');
  67. }
  68. customSelectInnerSpan.css({
  69. width: selectBoxWidth,
  70. display: 'inline-block'
  71. });
  72. $select.css({
  73. '-webkit-appearance': 'menulist-button',
  74. width: customSelectSpan.outerWidth(),
  75. position: 'absolute',
  76. opacity: 0,
  77. height: selectBoxHeight,
  78. fontSize: customSelectSpan.css('font-size')
  79. });
  80. })
  81. .on('change', function () {
  82. customSelectSpan.addClass('customSelectChanged');
  83. changed($select,customSelectSpan);
  84. })
  85. .on('keyup', function () {
  86. $select.blur();
  87. $select.focus();
  88. })
  89. .on('mousedown', function () {
  90. customSelectSpan.removeClass('customSelectChanged').addClass('customSelectOpen');
  91. })
  92. .on('mouseleave', function () {
  93. customSelectSpan.removeClass('customSelectChanged').removeClass('customSelectOpen');
  94. })
  95. .focus(function () {
  96. customSelectSpan.removeClass('customSelectChanged').addClass('customSelectFocus');
  97. })
  98. .blur(function () {
  99. customSelectSpan.removeClass('customSelectFocus customSelectOpen');
  100. })
  101. .hover(function () {
  102. customSelectSpan.addClass('customSelectHover');
  103. }, function () {
  104. customSelectSpan.removeClass('customSelectHover');
  105. })
  106. .trigger('update');
  107. });
  108. }
  109. });
  110. })(jQuery);