PageSourceSearch

https://rnlaigner.github.io/js/jquery.dropdownit.js

js rnlaigner.github.io collected 2026-10-03 09:51:21 UTC 6,535 bytes, 213 lines download raw bytes

1/**
2 * jquery.dropdown.js v1.0.0
3 * http://www.codrops.com
4 *
5 * Licensed under the MIT license.
6 * http://www.opensource.org/licenses/mit-license.php
7 *
8 * Copyright 2012, Codrops
9 * http://www.codrops.com
10 */
11;( function( $, window, undefined ) {
12
13	'use strict';
14
15	$.DropDown = function( options, element ) {
16		this.$el = $( element );
17		this._init( options );
18	};
19
20	// the options
21	$.DropDown.defaults = {
22		speed : 300,
23		easing : 'ease',
24		gutter : 0,
25		// initial stack effect
26		stack : true,
27		// delay between each option animation
28		delay : 0,
29		// random angle and positions for the options
30		random : false,
31		// rotated [right||left||false] : the options will be rotated to thr right side or left side.
32		// make sure to tune the transform origin in the stylesheet
33		rotated : false,
34		// effect to slide in the options. value is the margin to start with
35		slidingIn : false,
36		onOptionSelect : function(opt) { return false; }
37	};
38
39	$.DropDown.prototype = {
40
41		_init : function( options ) {
42
43			// options
44			this.options = $.extend( true, {}, $.DropDown.defaults, options );
45			this._layout();
46			this._initEvents();
47
48		},
49		_layout : function() {
50
51			var self = this;
52			this.minZIndex = 1000;
53			var value = this._transformSelect();
54			this.opts = this.listopts.children( 'li' );
55			this.optsCount = this.opts.length;
56			this.size = { width : this.dd.width(), height : this.dd.height() };
57			
58			var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ),
59				inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime();
60
61			this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel );
62			
63			this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount );
64			this._positionOpts();
65			if( Modernizr.csstransitions ) {
66				setTimeout( function() { self.opts.css( 'transition', 'all ' + self.options.speed + 'ms ' + self.options.easing ); }, 25 );
67			}
68
69		},
70		_transformSelect : function() {
71
72			var optshtml = '', selectlabel = '', value = -1;
73			this.$el.children( 'option' ).each( function() {
74
75				var $this = $( this ),
76					val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) ,
77					classes = $this.attr( 'class' ),
78					selected = $this.attr( 'selected' ),
79					label = $this.text();
80
81				if( val !== -1 ) {
82					optshtml += 
83						classes !== undefined ? 
84							'<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
85							'<li data-value="' + val + '"><span>' + label + '</span></li>';
86				}
87
88				if( selected ) {
89					selectlabel = label;
90					value = val;
91				}
92
93			} );
94
95			this.listopts = $( '<ul/>' ).append( optshtml );
96			this.selectlabel = $( '<span/>' ).append( selectlabel );
97			this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el );
98			this.$el.remove();
99
100			return value;
101
102		},
103		_positionOpts : function( anim ) {
104
105			var self = this;
106
107			this.listopts.css( 'height', 'auto' );
108			this.opts
109				.each( function( i ) {
110					$( this ).css( {
111						zIndex : self.minZIndex + self.optsCount - 1 - i,
112						top : self.options.slidingIn ? ( i + 1 ) * ( self.size.height + self.options.gutter ) : 0,
113						left : 0,
114						marginLeft : self.options.slidingIn ? i % 2 === 0 ? self.options.slidingIn : - self.options.slidingIn : 0,
115						opacity : self.options.slidingIn ? 0 : 1,
116						transform : 'none'
117					} );
118				} );
119
120			if( !this.options.slidingIn ) {
121				this.opts
122					.eq( this.optsCount - 1 )
123					.css( { top : this.options.stack ? 9 : 0, left : this.options.stack ? 4 : 0, width : this.options.stack ? this.size.width - 8 : this.size.width, transform : 'none' } )
124					.end()
125					.eq( this.optsCount - 2 )
126					.css( { top : this.options.stack ? 6 : 0, left : this.options.stack ? 2 : 0, width : this.options.stack ? this.size.width - 4 : this.size.width, transform : 'none' } )
127					.end()
128					.eq( this.optsCount - 3 )
129					.css( { top : this.options.stack ? 3 : 0, left : 0, transform : 'none' } );
130			}
131
132		},
133		_initEvents : function() {
134			
135			var self = this;
136			
137			this.selectlabel.on( 'mousedown.dropdown', function( event ) {
138				self.opened ? self.close() : self.open();
139				return false;
140
141			} );
142
143			this.opts.on( 'click.dropdown', function() {
144				if( self.opened ) {
145					var opt = $( this );
146					self.options.onOptionSelect( opt );
147					self.inputEl.val( opt.data( 'value' ) ).trigger('change');
148
149					self.selectlabel.html( opt.html() );
150					self.close();
151				}
152			} );
153
154		},
155		open : function() {
156			var self = this;
157			this.dd.toggleClass( 'cd-active' );
158			this.listopts.css( 'height', ( this.optsCount + 1 ) * ( this.size.height + this.options.gutter ) );
159			this.opts.each( function( i ) {
160
161				$( this ).css( {
162					opacity : 1,
163					top : self.options.rotated ? self.size.height + self.options.gutter : ( i + 1 ) * ( self.size.height + self.options.gutter ),
164					left : self.options.random ? Math.floor( Math.random() * 11 - 5 ) : 0,
165					width : self.size.width,
166					marginLeft : 0,
167					transform : self.options.random ?
168						'rotate(' + Math.floor( Math.random() * 11 - 5 ) + 'deg)' :
169						self.options.rotated ?
170							self.options.rotated === 'right' ?
171								'rotate(-' + ( i * 5 ) + 'deg)' :
172								'rotate(' + ( i * 5 ) + 'deg)'
173							: 'none',
174					transitionDelay : self.options.delay && Modernizr.csstransitions ? self.options.slidingIn ? ( i * self.options.delay ) + 'ms' : ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : 0
175				} );
176
177			} );
178			this.opened = true;
179
180		},
181		close : function() {
182
183			var self = this;
184			this.dd.toggleClass( 'cd-active' );
185			if( this.options.delay && Modernizr.csstransitions ) {
186				this.opts.each( function( i ) {
187					$( this ).css( { 'transition-delay' : self.options.slidingIn ? ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : ( i * self.options.delay ) + 'ms' } );
188				} );
189			}
190			this._positionOpts( true );
191			this.opened = false;
192
193		}
194
195	}
196
197	$.fn.dropdownit = function( options ) {
198		var instance = $.data( this, 'dropdown' );
199		if ( typeof options === 'string' ) {
200			var args = Array.prototype.slice.call( arguments, 1 );
201			this.each(function() {
202				instance[ options ].apply( instance, args );
203			});
204		}
205		else {
206			this.each(function() {
207				instance ? instance._init() : instance = $.data( this, 'dropdown', new $.DropDown( options, this ) );
208			});
209		}
210		return instance;
211	};
212
213} )( jQuery, window );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.