dropdown.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (function ( $ ) {
  2. $.fn.dropdown = function(options){
  3. var element = this;
  4. var options = $(this).children('option');
  5. build(element);
  6. $('body').on('click', '.dropdown', function(){
  7. animate($(this));
  8. })
  9. $(document).mouseup(function (e)
  10. {
  11. var dropdown = $(".dropdown");
  12. if (!dropdown.is(e.target) // if the target of the click isn't the container...
  13. && dropdown.has(e.target).length === 0) // ... nor a descendant of the container
  14. {
  15. deanimate()
  16. }
  17. });
  18. return {
  19. blue: function(){
  20. element.css('color', 'blue')
  21. }
  22. }
  23. };
  24. function build(element){
  25. var placeholder = element.attr('data-placeholder')
  26. var options = element.children('option')
  27. var selected = false;
  28. var classes = "dropdown";
  29. var options_html = '';
  30. options.each(function(){
  31. if ( $(this).attr('selected') == "selected") {
  32. selected = true
  33. }
  34. options_html += '<li><a>'+ $(this).text() +'</a></li>';
  35. })
  36. if ( selected == true ) {
  37. classes += ' hasValue';
  38. }
  39. var dropdown_html = '<div class = "dropdown"><div class = "bg"></div>';
  40. dropdown_html += '<div class = "front_face">'
  41. dropdown_html += '<span class = "placeholder=">'+ placeholder +'</span>'
  42. dropdown_html += '</div>';
  43. dropdown_html += '<div class = "back_face"><ul>'
  44. dropdown_html += options_html
  45. dropdown_html += '</ul></div>';
  46. dropdown_html += '</div>';
  47. $(dropdown_html).insertAfter(element)
  48. element.hide()
  49. }
  50. function animate(element){
  51. element.addClass('placeholder_animate')
  52. setTimeout(function(){
  53. element.addClass('placeholder_animate2')
  54. element.addClass('animate')
  55. element.addClass('animate2')
  56. }, 100)
  57. }
  58. function deanimate(){
  59. $('.dropdown').removeClass('animate2')
  60. setTimeout(function(){
  61. $('.dropdown').removeClass('animate')
  62. $('.dropdown').removeClass('placeholder_animate2')
  63. setTimeout(function(){
  64. $('.dropdown').removeClass('placeholder_animate')
  65. },100)
  66. }, 200)
  67. }
  68. function change(elem){
  69. elem.css('color', 'green')
  70. }
  71. }( jQuery ));