dropdown.js 4.4 KB

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