PageSourceSearch

https://dreaminmag.ru/js/jquery.elastislide.js

js dreaminmag.ru collected 2026-10-08 20:41:09 UTC 12,834 bytes, 439 lines download raw bytes

1(function( window, $, undefined ) {
2	
3	// http://www.netcu.de/jquery-touchwipe-iphone-ipad-library
4	$.fn.touchwipe 				= function(settings) {
5		
6		var config = {
7			min_move_x: 20,
8			min_move_y: 20,
9			wipeLeft: function() { },
10			wipeRight: function() { },
11			wipeUp: function() { },
12			wipeDown: function() { },
13			preventDefaultEvents: true
14		};
15     
16		if (settings) $.extend(config, settings);
17 
18		this.each(function() {
19			var startX;
20			var startY;
21			var isMoving = false;
22
23			function cancelTouch() {
24				this.removeEventListener('touchmove', onTouchMove);
25				startX = null;
26				isMoving = false;
27			}	
28		 
29			function onTouchMove(e) {
30				if(config.preventDefaultEvents) {
31					e.preventDefault();
32				}
33				if(isMoving) {
34					var x = e.touches[0].pageX;
35					var y = e.touches[0].pageY;
36					var dx = startX - x;
37					var dy = startY - y;
38					if(Math.abs(dx) >= config.min_move_x) {
39						cancelTouch();
40						if(dx > 0) {
41							config.wipeLeft();
42						}
43						else {
44							config.wipeRight();
45						}
46					}
47					else if(Math.abs(dy) >= config.min_move_y) {
48						cancelTouch();
49						if(dy > 0) {
50							config.wipeDown();
51						}
52						else {
53							config.wipeUp();
54						}
55					}
56				}
57			}
58		 
59			function onTouchStart(e)
60			{
61				if (e.touches.length == 1) {
62					startX = e.touches[0].pageX;
63					startY = e.touches[0].pageY;
64					isMoving = true;
65					this.addEventListener('touchmove', onTouchMove, false);
66				}
67			}    	 
68			if ('ontouchstart' in document.documentElement) {
69				this.addEventListener('touchstart', onTouchStart, false);
70			}
71		});
72 
73		return this;
74	};
75	
76	$.elastislide 				= function( options, element ) {
77		this.$el	= $( element );
78		this._init( options );
79	};
80	
81	$.elastislide.defaults 		= {
82		speed		: 450,	// animation speed
83		easing		: '',	// animation easing effect
84		imageW		: 286,	// the images width
85		margin		: 0,	// image margin right
86		border		: 0,	// image border
87		minItems	: 3,	// the minimum number of items to show. 
88							// when we resize the window, this will make sure minItems are always shown 
89							// (unless of course minItems is higher than the total number of elements)
90		current		: 0,	// index of the current item
91							// when we resize the window, the carousel will make sure this item is visible 
92    };
93	
94	$.elastislide.prototype 	= {
95		_init 				: function( options ) {
96			
97			this.options 		= $.extend( true, {}, $.elastislide.defaults, options );
98			
99			// <ul>
100			this.$slider		= this.$el.find('ul');
101			
102			// <li>
103			this.$items			= this.$slider.children('li');
104			
105			// total number of elements / images
106			this.itemsCount		= this.$items.length;
107			
108			// cache the <ul>'s parent, since we will eventually need to recalculate its width on window resize
109			this.$esCarousel	= this.$slider.parent();
110			
111			// validate options
112			this._validateOptions();
113			
114			// set sizes and initialize some vars...
115			this._configure();
116			
117			// add navigation buttons
118			this._addControls();
119			
120			// initialize the events
121			this._initEvents();
122			
123			// show the <ul>
124			this.$slider.show();
125			
126			// slide to current's position
127			this._slideToCurrent( false );
128			
129		},
130		_validateOptions	: function() {
131		
132			if( this.options.speed < 0 )
133				this.options.speed = 450;
134			if( this.options.margin < 0 )
135				this.options.margin = 4;
136			if( this.options.border < 0 )
137				this.options.border = 1;
138			if( this.options.minItems < 1 || this.options.minItems > this.itemsCount )
139				this.options.minItems = 1;
140			if( this.options.current > this.itemsCount - 1 )
141				this.options.current = 0;
142				
143		},
144		_configure			: function() {
145			
146			// current item's index
147			this.current		= this.options.current;
148			
149			// the ul's parent's (div.es-carousel) width is the "visible" width
150			this.visibleWidth	= this.$esCarousel.width();
151			
152			// test to see if we need to initially resize the items
153			if( this.visibleWidth < this.options.minItems * ( this.options.imageW + 2 * this.options.border ) + ( this.options.minItems - 1 ) * this.options.margin ) {
154				this._setDim( ( this.visibleWidth - ( this.options.minItems - 1 ) * this.options.margin ) / this.options.minItems );
155				this._setCurrentValues();
156				// how many items fit with the current width
157				this.fitCount	= this.options.minItems;
158			}
159			else {
160				this._setDim();
161				this._setCurrentValues();
162			}
163			
164			// set the <ul> width
165			this.$slider.css({
166				width	: this.sliderW
167			});
168			
169		},
170		_setDim				: function( elW ) {
171			
172			// <li> style
173			this.$items.css({
174				marginRight	: this.options.margin,
175				width		: ( elW ) ? elW : this.options.imageW + 2 * this.options.border
176			}).children('a').css({ // <a> style
177				//borderWidth		: this.options.border
178			});
179			
180		},
181		_setCurrentValues	: function() {
182			
183			// the total space occupied by one item
184			this.itemW			= this.$items.outerWidth(true);
185			
186			// total width of the slider / <ul>
187			// this will eventually change on window resize
188			this.sliderW		= this.itemW * this.itemsCount;
189			
190			// the ul parent's (div.es-carousel) width is the "visible" width
191			this.visibleWidth	= this.$esCarousel.width();
192			
193			// how many items fit with the current width
194			this.fitCount		= Math.floor( this.visibleWidth / this.itemW );
195			
196		},
197		_addControls		: function() {
198			
199			this.$navNext	= $('<span class="es-nav-next">Next</span>');
200			this.$navPrev	= $('<span class="es-nav-prev">Previous</span>');
201			$('<div class="es-nav"/>')
202			.append( this.$navPrev )
203			.append( this.$navNext )
204			.appendTo( this.$el );
205			
206			//this._toggleControls();
207				
208		},
209		_toggleControls		: function( dir, status ) {
210			
211			// show / hide navigation buttons
212			if( dir && status ) {
213				if( status === 1 )
214					( dir === 'right' ) ? this.$navNext.show() : this.$navPrev.show();
215				else
216					( dir === 'right' ) ? this.$navNext.hide() : this.$navPrev.hide();
217			}
218			else if( this.current === this.itemsCount - 1 || this.fitCount >= this.itemsCount )
219					this.$navNext.hide();
220			
221		},
222		_initEvents			: function() {
223			
224			var instance	= this;
225			
226			// window resize
227			$(window).bind('resize.elastislide', function( event ) {
228				
229				// set values again
230				instance._setCurrentValues();
231				
232				// need to resize items
233				if( instance.visibleWidth < instance.options.minItems * ( instance.options.imageW + 2 * instance.options.border ) + ( instance.options.minItems - 1 ) * instance.options.margin ) {
234					instance._setDim( ( instance.visibleWidth - ( instance.options.minItems - 1 ) * instance.options.margin ) / instance.options.minItems );
235					instance._setCurrentValues();
236					instance.fitCount	= instance.options.minItems;
237				}	
238				else{
239					instance._setDim();
240					instance._setCurrentValues();
241				}
242				
243				instance.$slider.css({
244					width	: instance.sliderW + 10 // TODO: +10px seems to solve a firefox "bug" :S
245				});
246						
247				// slide to the current element
248				clearTimeout( instance.resetTimeout );
249				instance.resetTimeout	= setTimeout(function() {
250					instance._slideToCurrent();
251				}, 200);
252				
253			});
254			
255			// navigation buttons events
256			this.$navNext.bind('click.elastislide', function( event ) {
257				instance._slide('right');
258			});
259			
260			this.$navPrev.bind('click.elastislide', function( event ) {
261				instance._slide('left');
262			});
263			
264			// item click event
265			/*
266			this.$items.bind('click.elastislide', function( event ) {
267				instance.options.onClick( $(this) );
268				return false;
269			});
270			*/
271			
272			// touch events
273			instance.$slider.touchwipe({
274				wipeLeft			: function() {
275					instance._slide('right');
276				},
277				wipeRight			: function() {
278					instance._slide('left');
279				}
280			});
281			
282		},
283		_slide				: function( dir, val, anim, callback ) {
284			
285			// if animating return
286			if( this.$slider.is(':animated') )
287				return false;
288			
289			// current margin left
290			var ml		= parseFloat( this.$slider.css('margin-left') );
291			
292			// val is just passed when we want an exact value for the margin left (used in the _slideToCurrent function)
293			if( val === undefined ) {
294			
295				// how much to slide?
296				var amount	= this.fitCount * this.itemW, val;
297				
298				if( amount < 0 ) return false;
299				
300				// make sure not to leave a space between the last item / first item and the end / beggining of the slider available width
301				if( dir === 'right' && this.sliderW - ( Math.abs( ml ) + amount ) < this.visibleWidth ) {
302					amount	= this.sliderW - ( Math.abs( ml ) + this.visibleWidth ) - this.options.margin; // decrease the margin left
303					// show / hide navigation buttons
304					this._toggleControls( 'right', -1 );
305					this._toggleControls( 'left', 1 );
306				}
307				else if( dir === 'left' && Math.abs( ml ) - amount < 0 ) {				
308					amount	= Math.abs( ml );
309					// show / hide navigation buttons
310					this._toggleControls( 'left', -1 );
311					this._toggleControls( 'right', 1 );
312				}
313				else {
314					var fml; // future margin left
315					( dir === 'right' ) 
316						? fml = Math.abs( ml ) + this.options.margin + Math.abs( amount ) 
317						: fml = Math.abs( ml ) - this.options.margin - Math.abs( amount );
318					
319					// show / hide navigation buttons
320					if( fml > 0 )
321						this._toggleControls( 'left', 1 );
322					else	
323						this._toggleControls( 'left', -1 );
324					
325					if( fml < this.sliderW - this.visibleWidth )
326						this._toggleControls( 'right', 1 );
327					else	
328						this._toggleControls( 'right', -1 );
329						
330				}
331				
332				( dir === 'right' ) ? val = '-=' + amount : val = '+=' + amount
333				
334			}
335			else {
336				var fml		= Math.abs( val ); // future margin left
337				
338				if( Math.max( this.sliderW, this.visibleWidth ) - fml < this.visibleWidth ) {
339					val	= - ( Math.max( this.sliderW, this.visibleWidth ) - this.visibleWidth );
340					if( val !== 0 )
341						val += this.options.margin;	// decrease the margin left if not on the first position
342						
343					// show / hide navigation buttons
344					this._toggleControls( 'right', -1 );
345					fml	= Math.abs( val );
346				}
347				
348				// show / hide navigation buttons
349				if( fml > 0 )
350					this._toggleControls( 'left', 1 );
351				else
352					this._toggleControls( 'left', -1 );
353				
354				if( Math.max( this.sliderW, this.visibleWidth ) - this.visibleWidth > fml + this.options.margin )	
355					this._toggleControls( 'right', 1 );
356				else
357					this._toggleControls( 'right', -1 );
358					
359			}
360			
361			$.fn.applyStyle = ( anim === undefined ) ? $.fn.animate : $.fn.css;
362			
363			var sliderCSS	= { marginLeft : val };
364			
365			var instance	= this;
366			
367			this.$slider.applyStyle( sliderCSS, $.extend( true, [], { duration : this.options.speed, easing : this.options.easing, complete : function() {
368				if( callback ) callback.call();
369			} } ) );
370			
371		},
372		_slideToCurrent		: function( anim ) {
373			
374			// how much to slide?
375			var amount	= this.current * this.itemW;
376			this._slide('', -amount, anim );
377			
378		},
379		add					: function( $newelems, callback ) {
380			
381			// adds new items to the carousel
382			this.$items 		= this.$items.add( $newelems );
383			this.itemsCount		= this.$items.length;
384			this._setDim();
385			this._setCurrentValues();
386			this.$slider.css({
387				width	: this.sliderW
388			});
389			this._slideToCurrent();
390			
391			if ( callback ) callback.call( $newelems );
392			
393		},
394		destroy				: function( callback ) {
395			this._destroy( callback );
396		},
397		_destroy 			: function( callback ) {
398			this.$el.unbind('.elastislide').removeData('elastislide');
399			$(window).unbind('.elastislide');
400			if ( callback ) callback.call();
401		}
402	};
403	
404	var logError 				= function( message ) {
405		if ( this.console ) {
406			console.error( message );
407		}
408	};
409	
410	$.fn.elastislide 				= function( options ) {
411		if ( typeof options === 'string' ) {
412			var args = Array.prototype.slice.call( arguments, 1 );
413
414			this.each(function() {
415				var instance = $.data( this, 'elastislide' );
416				if ( !instance ) {
417					logError( "cannot call methods on elastislide prior to initialization; " +
418					"attempted to call method '" + options + "'" );
419					return;
420				}
421				if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
422					logError( "no such method '" + options + "' for elastislide instance" );
423					return;
424				}
425				instance[ options ].apply( instance, args );
426			});
427		} 
428		else {
429			this.each(function() {
430				var instance = $.data( this, 'elastislide' );
431				if ( !instance ) {
432					$.data( this, 'elastislide', new $.elastislide( options, this ) );
433				}
434			});
435		}
436		return this;
437	};
438	
439})( window, jQuery );

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.