var dropdown_count = 0; (function ( $ ) { $.fn.dropdown = function(options){ var element = $(this); var opts = $.extend({}, $.fn.dropdown.defaults, options) build(element, opts); $('body').on('click', '.dropdown', function(){ animate($(this)); }) $('body').on('click', '.dropdown ul li a', function(){ setValue($(this).parents('.dropdown'), $(this).parent('li').index()) }) $(document).mouseup(function (e) { var dropdown = $(".dropdown"); if (!dropdown.is(e.target) // if the target of the click isn't the container... && dropdown.has(e.target).length === 0) // ... nor a descendant of the container { deanimate() } }); return { blue: function(){ element.css('color', 'blue') } } }; $.fn.dropdown.defaults = { background: "#e5e5e5", onOpen_background: "#fff", placeholder_color: "#000", link_color: "#000" } function build(element, opts){ dropdown_count += 1 var placeholder = element.attr('data-placeholder') var id = element.attr('id') var options = element.children('option') var selected = false; var classes = "dropdown"; var options_html = ''; var background = opts["background"] var onOpen_background = opts["onOpen_background"] var placeholder_color = opts["placeholder_color"] var link_color = opts["link_color"] options.each(function(){ if ( $(this).attr('selected') == "selected") { selected = $(this).text() } options_html += '
  • '+ $(this).text() +'
  • '; }) if ( selected !== false ) { classes += ' hasValue'; } if (typeof id !== typeof undefined && id !== false) { id_html = id } else { id_html = 'dropdown_' + dropdown_count; $(element).attr('id', id_html) } var dropdown_html = '
    '; dropdown_html += '
    ' dropdown_html += '
    ' dropdown_html += '
    ' if ( selected !== false ) { dropdown_html += ''+ selected +''; } dropdown_html += ''+ placeholder +'' dropdown_html += ''+ icon() +'' dropdown_html += '
    ' dropdown_html += '
    '; dropdown_html += '
    '; dropdown_html += '
    '; $(dropdown_html).insertAfter(element) $('#euler_' + id_html).css("height", $('#euler_' + id_html).outerHeight() - $('#euler_' + id_html).find('.back_face').outerHeight() ) element.hide() } function animate(element){ element.addClass('placeholder_animate') setTimeout(function(){ element.addClass('placeholder_animate2') element.addClass('animate') element.addClass('animate2') }, 100) } function deanimate(){ $('.dropdown').removeClass('animate2') setTimeout(function(){ $('.dropdown').removeClass('animate') $('.dropdown').removeClass('placeholder_animate2') setTimeout(function(){ $('.dropdown').removeClass('placeholder_animate') },100) }, 200) } function setValue(euler_dropdown, value_index){ var id = euler_dropdown.attr('data-select') var select = document.getElementById(id); var option_value = $(select).children('option').eq(value_index) var callback = $(select).attr('data-callback') $(select).val(option_value.val()) $(euler_dropdown).find('.current_value').remove() $(euler_dropdown).find('.front_face .content').prepend(''+ option_value.text() + '') $(euler_dropdown).addClass('hasValue') if (typeof callback !== typeof undefined && callback !== false) { window[callback](option_value.val()) } setTimeout(function(){ deanimate() }, 200) } function icon(){ return ''; } function change(elem){ elem.css('color', 'green') } }( jQuery )); function hello(value){ console.log("hello world! the selected value is " + value) }