dropdown.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. dropdown_count = 0;
  2. (function ( $ ) {
  3. $(document).mouseup(function (e)
  4. {
  5. var dropdown = $(".dropdown");
  6. if (!dropdown.is(e.target) // if the target of the click isn't the container...
  7. && dropdown.has(e.target).length === 0) // ... nor a descendant of the container
  8. {
  9. deanimate()
  10. }
  11. });
  12. $.fn.dropdown = function(options){
  13. var element = $(this);
  14. var opts = $.extend({}, $.fn.dropdown.defaults, options)
  15. element.each(function(){
  16. build($(this), opts);
  17. })
  18. $('body').on('click', '.dropdown', function(){
  19. //alert(3)
  20. if ( $(this).hasClass('animate') == false ) {
  21. animate($(this))
  22. }
  23. })
  24. $('body').on('click', '.dropdown ul li a', function(){
  25. setValue($(this).parents('.dropdown'), $(this).parent('li').index())
  26. })
  27. return {
  28. blue: function(){
  29. element.css('color', 'blue')
  30. }
  31. }
  32. };
  33. $.fn.dropdown.defaults = {
  34. background: "#e5e5e5",
  35. active_background: "#fff",
  36. placeholder_color: "#000",
  37. placeholder_active_color: "#000",
  38. link_color: "#000"
  39. }
  40. function build(element, opts){
  41. var placeholder = element.attr('data-placeholder')
  42. var id = element.attr('id')
  43. var options = element.children('option')
  44. var selected = false;
  45. var classes = "dropdown";
  46. var options_html = '';
  47. var background = opts["background"]
  48. var active_background = opts["active_background"]
  49. var placeholder_color = opts["placeholder_color"]
  50. var placeholder_active_color = opts["placeholder_active_color"]
  51. var link_color = opts["link_color"]
  52. options.each(function(){
  53. if ( $(this).attr('selected') == "selected") {
  54. selected = $(this).text()
  55. }
  56. options_html += '<li><a>'+ $(this).text() +'</a></li>';
  57. })
  58. if ( selected !== false ) {
  59. classes += ' hasValue';
  60. }
  61. if (typeof id !== typeof undefined && id !== false) {
  62. id_html = id
  63. } else {
  64. id_html = 'dropdown_' + dropdown_count;
  65. $(element).attr('id', id_html)
  66. }
  67. var dropdown_html = '<div id="euler_'+ id_html +'" data-select="'+ id_html +'" class = "'+ classes +'"><div style="background:'+ active_background +'" class = "bg"></div>';
  68. dropdown_html += '<div class = "front_face">'
  69. dropdown_html += '<div style="background:'+ background +'" class = "bg"></div>'
  70. dropdown_html += '<div data-inactive-color="'+ placeholder_active_color +'" style="color:'+ placeholder_color +'" class = "content">'
  71. if ( selected !== false ) {
  72. dropdown_html += '<span class="current_value">'+ selected +'</span>';
  73. }
  74. dropdown_html += '<span class = "placeholder">'+ placeholder +'</span>'
  75. dropdown_html += '<i class = "icon">'+ icon(placeholder_color) +'</i>'
  76. dropdown_html += '</div>'
  77. dropdown_html += '</div>';
  78. dropdown_html += '<div class = "back_face"><ul style="color:'+ link_color +'">'
  79. dropdown_html += options_html
  80. dropdown_html += '</ul></div>';
  81. dropdown_html += '</div>';
  82. $(dropdown_html).insertAfter(element)
  83. //$('#euler_' + id_html).css("height", $('#euler_' + id_html).outerHeight() - $('#euler_' + id_html).find('.back_face').outerHeight() )
  84. element.hide()
  85. }
  86. function animate(element){
  87. if ( $('.dropdown.animate').length > 0 ) {
  88. deanimate($('.dropdown').not(element))
  89. var timeout = 600
  90. } else {
  91. var timeout = 100
  92. }
  93. setTimeout(function(){
  94. var back_face = element.find('.back_face')
  95. back_face.show()
  96. var bg = element.find('> .bg')
  97. bg.css({
  98. "height" : element.outerHeight() + back_face.outerHeight()
  99. })
  100. back_face.css({
  101. "margin-top" : $(element).outerHeight()
  102. })
  103. switchPlaceholderColor(element)
  104. element.addClass('placeholder_animate')
  105. setTimeout(function(){
  106. if ( back_face.outerHeight() == 200 ) {
  107. back_face.addClass('overflow')
  108. }
  109. element.addClass('placeholder_animate2')
  110. element.addClass('animate')
  111. element.addClass('animate2')
  112. }, 100)
  113. }, timeout)
  114. }
  115. function deanimate(dropdowns){
  116. if (dropdowns == null ) {
  117. var dropdown = $('.dropdown')
  118. } else {
  119. var dropdown = dropdowns
  120. }
  121. $(dropdown).each(function(){
  122. var element = $(this);
  123. if ( element.hasClass('animate2') ) {
  124. setTimeout(function(){
  125. element.removeClass('animate2')
  126. setTimeout(function(){
  127. element.find('.back_face').hide()
  128. element.removeClass('animate')
  129. switchPlaceholderColor(element)
  130. element.children(".bg").css({
  131. "height" : 0
  132. })
  133. element.removeClass('placeholder_animate2')
  134. setTimeout(function(){
  135. element.removeClass('placeholder_animate')
  136. },100)
  137. }, 200)
  138. }, 400)
  139. }
  140. })
  141. }
  142. function switchPlaceholderColor(element) {
  143. var placeholder_inactive_color = element.find('.front_face .content').attr('data-inactive-color')
  144. var placeholder_normal_color = element.find('.front_face .content').css('color')
  145. element.find('.front_face .content').attr('data-inactive-color', placeholder_normal_color)
  146. element.find('.front_face .content').css('color', placeholder_inactive_color)
  147. element.find('.front_face .icon svg').css('fill', placeholder_inactive_color)
  148. }
  149. function setValue(euler_dropdown, value_index){
  150. var id = euler_dropdown.attr('data-select')
  151. var select = document.getElementById(id);
  152. var option_value = $(select).children('option').eq(value_index)
  153. var callback = $(select).attr('data-callback')
  154. $(select).val(option_value.val())
  155. $(euler_dropdown).find('.current_value').remove()
  156. $(euler_dropdown).find('.front_face .content').prepend('<span class = "current_value">'+ option_value.text() + '</span>')
  157. $(euler_dropdown).addClass('hasValue')
  158. if (typeof callback !== typeof undefined && callback !== false) {
  159. window[callback](option_value.val())
  160. }
  161. setTimeout(function(){
  162. deanimate()
  163. }, 100)
  164. }
  165. function icon(color){
  166. return '<svg style="fill:'+ color +'" version="1.1" id="Chevron_thin_down" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 20 20" enable-background="new 0 0 20 20" xml:space="preserve"><path d="M17.418,6.109c0.272-0.268,0.709-0.268,0.979,0c0.27,0.268,0.271,0.701,0,0.969l-7.908,7.83c-0.27,0.268-0.707,0.268-0.979,0l-7.908-7.83c-0.27-0.268-0.27-0.701,0-0.969c0.271-0.268,0.709-0.268,0.979,0L10,13.25L17.418,6.109z"/></svg>';
  167. }
  168. function change(elem){
  169. elem.css('color', 'green')
  170. }
  171. }( jQuery ));
  172. function hello(value){
  173. console.log("hello world! the selected value is " + value)
  174. }