dropdown.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. dropdown_count = 0;
  2. (function ( $ ) {
  3. $.fn.dropdown = function(options){
  4. var element = $(this);
  5. var opts = $.extend({}, $.fn.dropdown.defaults, options)
  6. element.each(function(){
  7. build($(this), opts);
  8. })
  9. $('body').on('click', '.dropdown', function(){
  10. //alert(3)
  11. if ( $(this).hasClass('animate') == false ) {
  12. animate($(this))
  13. }
  14. })
  15. $('body').on('click', '.dropdown ul li a', function(){
  16. setValue($(this).parents('.dropdown'), $(this).parent('li').index())
  17. })
  18. return {
  19. blue: function(){
  20. element.css('color', 'blue')
  21. }
  22. }
  23. };
  24. $.fn.dropdown.defaults = {
  25. background: "#e5e5e5",
  26. onOpen_background: "#fff",
  27. placeholder_color: "#000",
  28. link_color: "#000"
  29. }
  30. function build(element, opts){
  31. var placeholder = element.attr('data-placeholder')
  32. var id = element.attr('id')
  33. var options = element.children('option')
  34. var selected = false;
  35. var classes = "dropdown";
  36. var options_html = '';
  37. var background = opts["background"]
  38. var onOpen_background = opts["onOpen_background"]
  39. var placeholder_color = opts["placeholder_color"]
  40. var link_color = opts["link_color"]
  41. options.each(function(){
  42. if ( $(this).attr('selected') == "selected") {
  43. selected = $(this).text()
  44. }
  45. options_html += '<li><a>'+ $(this).text() +'</a></li>';
  46. })
  47. if ( selected !== false ) {
  48. classes += ' hasValue';
  49. }
  50. if (typeof id !== typeof undefined && id !== false) {
  51. id_html = id
  52. } else {
  53. id_html = 'dropdown_' + dropdown_count;
  54. $(element).attr('id', id_html)
  55. }
  56. var dropdown_html = '<div id="euler_'+ id_html +'" data-select="'+ id_html +'" class = "'+ classes +'"><div class = "bg"></div>';
  57. dropdown_html += '<div class = "front_face">'
  58. dropdown_html += '<div class = "bg"></div>'
  59. dropdown_html += '<div class = "content">'
  60. if ( selected !== false ) {
  61. dropdown_html += '<span class="current_value">'+ selected +'</span>';
  62. }
  63. dropdown_html += '<span class = "placeholder">'+ placeholder +'</span>'
  64. dropdown_html += '<i class = "icon">'+ icon() +'</i>'
  65. dropdown_html += '</div>'
  66. dropdown_html += '</div>';
  67. dropdown_html += '<div class = "back_face"><ul>'
  68. dropdown_html += options_html
  69. dropdown_html += '</ul></div>';
  70. dropdown_html += '</div>';
  71. $(dropdown_html).insertAfter(element)
  72. //$('#euler_' + id_html).css("height", $('#euler_' + id_html).outerHeight() - $('#euler_' + id_html).find('.back_face').outerHeight() )
  73. element.hide()
  74. }
  75. function animate(element){
  76. if ( $('.dropdown.animate').length > 0 ) {
  77. deanimate($('.dropdown').not(element))
  78. var timeout = 600
  79. } else {
  80. var timeout = 10
  81. }
  82. setTimeout(function(){
  83. setTimeout(function(){
  84. var back_face = element.find('.back_face')
  85. var bg = element.find('> .bg')
  86. bg.css({
  87. "height" : element.outerHeight() + back_face.outerHeight()
  88. })
  89. back_face.css({
  90. "margin-top" : $(element).outerHeight()
  91. })
  92. element.addClass('placeholder_animate')
  93. setTimeout(function(){
  94. element.addClass('placeholder_animate2')
  95. element.addClass('animate')
  96. element.addClass('animate2')
  97. }, 100)
  98. }, timeout)
  99. }, 200)
  100. }
  101. function deanimate(dropdowns){
  102. if (dropdowns == null ) {
  103. var dropdown = $('.dropdown')
  104. } else {
  105. var dropdown = dropdowns
  106. }
  107. setTimeout(function(){
  108. dropdown.removeClass('animate2')
  109. setTimeout(function(){
  110. dropdown.removeClass('animate')
  111. dropdown.children(".bg").css({
  112. "height" : 0
  113. })
  114. dropdown.removeClass('placeholder_animate2')
  115. setTimeout(function(){
  116. dropdown.removeClass('placeholder_animate')
  117. },100)
  118. }, 200)
  119. }, 400)
  120. }
  121. function setValue(euler_dropdown, value_index){
  122. var id = euler_dropdown.attr('data-select')
  123. var select = document.getElementById(id);
  124. var option_value = $(select).children('option').eq(value_index)
  125. var callback = $(select).attr('data-callback')
  126. $(select).val(option_value.val())
  127. $(euler_dropdown).find('.current_value').remove()
  128. $(euler_dropdown).find('.front_face .content').prepend('<span class = "current_value">'+ option_value.text() + '</span>')
  129. $(euler_dropdown).addClass('hasValue')
  130. if (typeof callback !== typeof undefined && callback !== false) {
  131. window[callback](option_value.val())
  132. }
  133. setTimeout(function(){
  134. deanimate()
  135. }, 100)
  136. }
  137. function icon(){
  138. return '<svg 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>';
  139. }
  140. function change(elem){
  141. elem.css('color', 'green')
  142. }
  143. }( jQuery ));
  144. function hello(value){
  145. console.log("hello world! the selected value is " + value)
  146. }