How to use Base method in Mocha

Best JavaScript code snippet using mocha

Run Mocha automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

owl.carousel.js

Source: owl.carousel.js Github

copy
1/*
2 *	jQuery OwlCarousel v1.31
3 *
4 *	Copyright (c) 2013 Bartosz Wojciechowski
5 *	http://www.owlgraphic.com/owlcarousel/
6 *
7 *	Licensed under MIT
8 *
9 */
10
11if ( typeof Object.create !== "function" ) {
12	Object.create = function( obj ) {
13		function F() {};
14		F.prototype = obj;
15		return new F();
16	};
17}
18(function( $, window, document, undefined ) {
19
20	var Carousel = {
21		init :function(options, el){
22			var base = this;
23
24			base.$elem = $(el);
25
26			// options passed via js override options passed via data attributes
27			base.options = $.extend({}, $.fn.owlCarousel.options, base.$elem.data(), options);
28
29			base.userOptions = options;
30			base.loadContent();
31		},
32
33		loadContent : function(){
34			var base = this;
35
36			if (typeof base.options.beforeInit === "function") {
37				base.options.beforeInit.apply(this,[base.$elem]);
38			}
39
40			if (typeof base.options.jsonPath === "string") {
41				var url = base.options.jsonPath;
42
43				function getData(data) {
44					if (typeof base.options.jsonSuccess === "function") {
45						base.options.jsonSuccess.apply(this,[data]);
46					} else {
47						var content = "";
48						for(var i in data["owl"]){
49							content += data["owl"][i]["item"];
50						}
51						base.$elem.html(content);
52					}
53					base.logIn();
54				}
55				$.getJSON(url,getData);
56			} else {
57				base.logIn();
58			}
59		},
60
61		logIn : function(action){
62			var base = this;
63
64			base.$elem.data("owl-originalStyles", base.$elem.attr("style"))
65					  .data("owl-originalClasses", base.$elem.attr("class"));
66
67			base.$elem.css({opacity: 0});
68			base.orignalItems = base.options.items;
69			base.checkBrowser();
70			base.wrapperWidth = 0;
71			base.checkVisible;
72			base.setVars();
73		},
74
75		setVars : function(){
76			var base = this;
77			if(base.$elem.children().length === 0){return false}
78			base.baseClass();
79			base.eventTypes();
80			base.$userItems = base.$elem.children();
81			base.itemsAmount = base.$userItems.length;
82			base.wrapItems();
83			base.$owlItems = base.$elem.find(".owl-item");
84			base.$owlWrapper = base.$elem.find(".owl-wrapper");
85			base.playDirection = "next";
86			base.prevItem = 0;
87			base.prevArr = [0];
88			base.currentItem = 0;
89			base.customEvents();
90			base.onStartup();
91		},
92
93		onStartup : function(){
94			var base = this;
95			base.updateItems();
96			base.calculateAll();
97			base.buildControls();
98			base.updateControls();
99			base.response();
100			base.moveEvents();
101			base.stopOnHover();
102			base.owlStatus();
103
104			if(base.options.transitionStyle !== false){
105				base.transitionTypes(base.options.transitionStyle);
106			}
107			if(base.options.autoPlay === true){
108				base.options.autoPlay = 5000;
109			}
110			base.play();
111
112			base.$elem.find(".owl-wrapper").css("display","block")
113
114			if(!base.$elem.is(":visible")){
115				base.watchVisibility();
116			} else {
117				base.$elem.css("opacity",1);
118			}
119			base.onstartup = false;
120			base.eachMoveUpdate();
121			if (typeof base.options.afterInit === "function") {
122				base.options.afterInit.apply(this,[base.$elem]);
123			}
124		},
125
126		eachMoveUpdate : function(){
127			var base = this;
128
129			if(base.options.lazyLoad === true){
130				base.lazyLoad();
131			}
132			if(base.options.autoHeight === true){
133				base.autoHeight();
134			}
135			base.onVisibleItems();
136
137			if (typeof base.options.afterAction === "function") {
138				base.options.afterAction.apply(this,[base.$elem]);
139			}
140		},
141
142		updateVars : function(){
143			var base = this;
144			if(typeof base.options.beforeUpdate === "function") {
145				base.options.beforeUpdate.apply(this,[base.$elem]);
146			}
147			base.watchVisibility();
148			base.updateItems();
149			base.calculateAll();
150			base.updatePosition();
151			base.updateControls();
152			base.eachMoveUpdate();
153			if(typeof base.options.afterUpdate === "function") {
154				base.options.afterUpdate.apply(this,[base.$elem]);
155			}
156		},
157
158		reload : function(elements){
159			var base = this;
160			setTimeout(function(){
161				base.updateVars();
162			},0)
163		},
164
165		watchVisibility : function(){
166			var base = this;
167
168			if(base.$elem.is(":visible") === false){
169				base.$elem.css({opacity: 0});
170				clearInterval(base.autoPlayInterval);
171				clearInterval(base.checkVisible);
172			} else {
173				return false;
174			}
175			base.checkVisible = setInterval(function(){
176				if (base.$elem.is(":visible")) {
177					base.reload();
178					base.$elem.animate({opacity: 1},200);
179					clearInterval(base.checkVisible);
180				}
181			}, 500);
182		},
183
184		wrapItems : function(){
185			var base = this;
186			base.$userItems.wrapAll("<div class=\"owl-wrapper\">").wrap("<div class=\"owl-item\"></div>");
187			base.$elem.find(".owl-wrapper").wrap("<div class=\"owl-wrapper-outer\">");
188			base.wrapperOuter = base.$elem.find(".owl-wrapper-outer");
189			base.$elem.css("display","block");
190		},
191
192		baseClass : function(){
193			var base = this;
194			var hasBaseClass = base.$elem.hasClass(base.options.baseClass);
195			var hasThemeClass = base.$elem.hasClass(base.options.theme);
196
197			if(!hasBaseClass){
198				base.$elem.addClass(base.options.baseClass);
199			}
200
201			if(!hasThemeClass){
202				base.$elem.addClass(base.options.theme);
203			}
204		},
205
206		updateItems : function(){
207			var base = this;
208
209			if(base.options.responsive === false){
210				return false;
211			}
212			if(base.options.singleItem === true){
213				base.options.items = base.orignalItems = 1;
214				base.options.itemsCustom = false;
215				base.options.itemsDesktop = false;
216				base.options.itemsDesktopSmall = false;
217				base.options.itemsTablet = false;
218				base.options.itemsTabletSmall = false;
219				base.options.itemsMobile = false;
220				return false;
221			}
222
223			var width = $(base.options.responsiveBaseWidth).width();
224
225			if(width > (base.options.itemsDesktop[0] || base.orignalItems) ){
226				base.options.items = base.orignalItems;
227			}
228
229			if(typeof(base.options.itemsCustom) !== 'undefined' && base.options.itemsCustom !== false){
230				//Reorder array by screen size
231				base.options.itemsCustom.sort(function(a,b){return a[0]-b[0];});
232				for(var i in base.options.itemsCustom){
233					if(typeof(base.options.itemsCustom[i]) !== 'undefined' && base.options.itemsCustom[i][0] <= width){
234						base.options.items = base.options.itemsCustom[i][1];
235					}
236				}
237			} else {
238
239				if(width <= base.options.itemsDesktop[0] && base.options.itemsDesktop !== false){
240					base.options.items = base.options.itemsDesktop[1];
241				}
242
243				if(width <= base.options.itemsDesktopSmall[0] && base.options.itemsDesktopSmall !== false){
244					base.options.items = base.options.itemsDesktopSmall[1];
245				}
246
247				if(width <= base.options.itemsTablet[0]  && base.options.itemsTablet !== false){
248					base.options.items = base.options.itemsTablet[1];
249				}
250
251				if(width <= base.options.itemsTabletSmall[0]  && base.options.itemsTabletSmall !== false){
252					base.options.items = base.options.itemsTabletSmall[1];
253				}
254
255				if(width <= base.options.itemsMobile[0] && base.options.itemsMobile !== false){
256					base.options.items = base.options.itemsMobile[1];
257				}
258			}
259
260			//if number of items is less than declared
261			if(base.options.items > base.itemsAmount && base.options.itemsScaleUp === true){
262				base.options.items = base.itemsAmount;
263			}
264		},
265
266		response : function(){
267			var base = this,
268				smallDelay;
269			if(base.options.responsive !== true){
270				return false
271			}
272			var lastWindowWidth = $(window).width();
273
274			base.resizer = function(){
275				if($(window).width() !== lastWindowWidth){
276					if(base.options.autoPlay !== false){
277						clearInterval(base.autoPlayInterval);
278					}
279					clearTimeout(smallDelay);
280					smallDelay = setTimeout(function(){
281						lastWindowWidth = $(window).width();
282						base.updateVars();
283					},base.options.responsiveRefreshRate);
284				}
285			}
286			$(window).resize(base.resizer)
287		},
288
289		updatePosition : function(){
290			var base = this;
291			base.jumpTo(base.currentItem);
292			if(base.options.autoPlay !== false){
293				base.checkAp();
294			}
295		},
296
297		appendItemsSizes : function(){
298			var base = this;
299
300			var roundPages = 0;
301			var lastItem = base.itemsAmount - base.options.items;
302
303			base.$owlItems.each(function(index){
304				var $this = $(this);
305				$this
306					.css({"width": base.itemWidth})
307					.data("owl-item",Number(index));
308
309				if(index % base.options.items === 0 || index === lastItem){
310					if(!(index > lastItem)){
311						roundPages +=1;
312					}
313				}
314				$this.data("owl-roundPages",roundPages)
315			});
316		},
317
318		appendWrapperSizes : function(){
319			var base = this;
320			var width = 0;
321
322			var width = base.$owlItems.length * base.itemWidth;
323
324			base.$owlWrapper.css({
325				"width": width*2,
326				"left": 0
327			});
328			base.appendItemsSizes();
329		},
330
331		calculateAll : function(){
332			var base = this;
333			base.calculateWidth();
334			base.appendWrapperSizes();
335			base.loops();
336			base.max();
337		},
338
339		calculateWidth : function(){
340			var base = this;
341			base.itemWidth = Math.round(base.$elem.width()/base.options.items)
342		},
343
344		max : function(){
345			var base = this;
346			var maximum = ((base.itemsAmount * base.itemWidth) - base.options.items * base.itemWidth) * -1;
347			if(base.options.items > base.itemsAmount){
348				base.maximumItem = 0;
349				maximum = 0
350				base.maximumPixels = 0;
351			} else {
352				base.maximumItem = base.itemsAmount - base.options.items;
353				base.maximumPixels = maximum;
354			}
355			return maximum;
356		},
357
358		min : function(){
359			return 0;
360		},
361
362		loops : function(){
363			var base = this;
364
365			base.positionsInArray = [0];
366			base.pagesInArray = [];
367			var prev = 0;
368			var elWidth = 0;
369
370			for(var i = 0; i<base.itemsAmount; i++){
371				elWidth += base.itemWidth;
372				base.positionsInArray.push(-elWidth);
373
374				if(base.options.scrollPerPage === true){
375					var item = $(base.$owlItems[i]);
376					var roundPageNum = item.data("owl-roundPages");
377					if(roundPageNum !== prev){
378						base.pagesInArray[prev] = base.positionsInArray[i];
379						prev = roundPageNum;
380					}
381				}
382			}
383		},
384
385		buildControls : function(){
386			var base = this;
387			if(base.options.navigation === true || base.options.pagination === true){
388				base.owlControls = $("<div class=\"owl-controls\"/>").toggleClass("clickable", !base.browser.isTouch).appendTo(base.$elem);
389			}
390			if(base.options.pagination === true){
391				base.buildPagination();
392			}
393			if(base.options.navigation === true){
394				base.buildButtons();
395			}
396		},
397
398		buildButtons : function(){
399			var base = this;
400			var buttonsWrapper = $("<div class=\"owl-buttons\"/>")
401			base.owlControls.append(buttonsWrapper);
402
403			base.buttonPrev = $("<div/>",{
404				"class" : "owl-prev",
405				"html" : base.options.navigationText[0] || ""
406				});
407
408			base.buttonNext = $("<div/>",{
409				"class" : "owl-next",
410				"html" : base.options.navigationText[1] || ""
411				});
412
413			buttonsWrapper
414			.append(base.buttonPrev)
415			.append(base.buttonNext);
416
417			buttonsWrapper.on("touchstart.owlControls mousedown.owlControls", "div[class^=\"owl\"]", function(event){
418				event.preventDefault();
419			})
420
421			buttonsWrapper.on("touchend.owlControls mouseup.owlControls", "div[class^=\"owl\"]", function(event){
422				event.preventDefault();
423				if($(this).hasClass("owl-next")){
424					base.next();
425				} else{
426					base.prev();
427				}
428			})
429		},
430
431		buildPagination : function(){
432			var base = this;
433
434			base.paginationWrapper = $("<div class=\"owl-pagination\"/>");
435			base.owlControls.append(base.paginationWrapper);
436
437			base.paginationWrapper.on("touchend.owlControls mouseup.owlControls", ".owl-page", function(event){
438				event.preventDefault();
439				if(Number($(this).data("owl-page")) !== base.currentItem){
440					base.goTo( Number($(this).data("owl-page")), true);
441				}
442			});
443		},
444
445		updatePagination : function(){
446			var base = this;
447			if(base.options.pagination === false){
448				return false;
449			}
450
451			base.paginationWrapper.html("");
452
453			var counter = 0;
454			var lastPage = base.itemsAmount - base.itemsAmount % base.options.items;
455
456			for(var i = 0; i<base.itemsAmount; i++){
457				if(i % base.options.items === 0){
458					counter +=1;
459					if(lastPage === i){
460						var lastItem = base.itemsAmount - base.options.items;
461					}
462					var paginationButton = $("<div/>",{
463						"class" : "owl-page"
464						});
465					var paginationButtonInner = $("<span></span>",{
466						"text": base.options.paginationNumbers === true ? counter : "",
467						"class": base.options.paginationNumbers === true ? "owl-numbers" : ""
468					});
469					paginationButton.append(paginationButtonInner);
470
471					paginationButton.data("owl-page",lastPage === i ? lastItem : i);
472					paginationButton.data("owl-roundPages",counter);
473
474					base.paginationWrapper.append(paginationButton);
475				}
476			}
477			base.checkPagination();
478		},
479		checkPagination : function(){
480			var base = this;
481			if(base.options.pagination === false){
482				return false;
483			}
484			base.paginationWrapper.find(".owl-page").each(function(i,v){
485				if($(this).data("owl-roundPages") === $(base.$owlItems[base.currentItem]).data("owl-roundPages") ){
486					base.paginationWrapper
487						.find(".owl-page")
488						.removeClass("active");
489					$(this).addClass("active");
490				}
491			});
492		},
493
494		checkNavigation : function(){
495			var base = this;
496
497			if(base.options.navigation === false){
498				return false;
499			}
500			if(base.options.rewindNav === false){
501				if(base.currentItem === 0 && base.maximumItem === 0){
502					base.buttonPrev.addClass("disabled");
503					base.buttonNext.addClass("disabled");
504				} else if(base.currentItem === 0 && base.maximumItem !== 0){
505					base.buttonPrev.addClass("disabled");
506					base.buttonNext.removeClass("disabled");
507				} else if (base.currentItem === base.maximumItem){
508					base.buttonPrev.removeClass("disabled");
509					base.buttonNext.addClass("disabled");
510				} else if(base.currentItem !== 0 && base.currentItem !== base.maximumItem){
511					base.buttonPrev.removeClass("disabled");
512					base.buttonNext.removeClass("disabled");
513				}
514			}
515		},
516
517		updateControls : function(){
518			var base = this;
519			base.updatePagination();
520			base.checkNavigation();
521			if(base.owlControls){
522				if(base.options.items >= base.itemsAmount){
523					base.owlControls.hide();
524				} else {
525					base.owlControls.show();
526				}
527			}
528		},
529
530		destroyControls : function(){
531			var base = this;
532			if(base.owlControls){
533				base.owlControls.remove();
534			}
535		},
536
537		next : function(speed){
538			var base = this;
539
540			if(base.isTransition){
541				return false;
542			}
543
544			base.currentItem += base.options.scrollPerPage === true ? base.options.items : 1;
545			if(base.currentItem > base.maximumItem + (base.options.scrollPerPage == true ? (base.options.items - 1) : 0)){
546				if(base.options.rewindNav === true){
547					base.currentItem = 0;
548					speed = "rewind";
549				} else {
550					base.currentItem = base.maximumItem;
551					return false;
552				}
553			}
554			base.goTo(base.currentItem,speed);
555		},
556
557		prev : function(speed){
558			var base = this;
559
560			if(base.isTransition){
561				return false;
562			}
563
564			if(base.options.scrollPerPage === true && base.currentItem > 0 && base.currentItem < base.options.items){
565				base.currentItem = 0
566			} else {
567				base.currentItem -= base.options.scrollPerPage === true ? base.options.items : 1;
568			}
569			if(base.currentItem < 0){
570				if(base.options.rewindNav === true){
571					base.currentItem = base.maximumItem;
572					speed = "rewind"
573				} else {
574					base.currentItem =0;
575					return false;
576				}
577			}
578			base.goTo(base.currentItem,speed);
579		},
580
581		goTo : function(position,speed,drag){
582			var base = this;
583
584			if(base.isTransition){
585				return false;
586			}
587			if(typeof base.options.beforeMove === "function") {
588				base.options.beforeMove.apply(this,[base.$elem]);
589			}
590			if(position >= base.maximumItem){
591				position = base.maximumItem;
592			}
593			else if( position <= 0 ){
594				position = 0;
595			}
596
597			base.currentItem = base.owl.currentItem = position;
598			if( base.options.transitionStyle !== false && drag !== "drag" && base.options.items === 1 && base.browser.support3d === true){
599				base.swapSpeed(0)
600				if(base.browser.support3d === true){
601					base.transition3d(base.positionsInArray[position]);
602				} else {
603					base.css2slide(base.positionsInArray[position],1);
604				}
605				base.afterGo();
606				base.singleItemTransition();
607				
608				return false;
609			}
610			var goToPixel = base.positionsInArray[position];
611
612			if(base.browser.support3d === true){
613				base.isCss3Finish = false;
614
615				if(speed === true){
616					base.swapSpeed("paginationSpeed");
617					setTimeout(function() {
618						base.isCss3Finish = true;
619					}, base.options.paginationSpeed);
620
621				} else if(speed === "rewind" ){
622					base.swapSpeed(base.options.rewindSpeed);
623					setTimeout(function() {
624						base.isCss3Finish = true;
625					}, base.options.rewindSpeed);
626
627				} else {
628					base.swapSpeed("slideSpeed");
629					setTimeout(function() {
630						base.isCss3Finish = true;
631					}, base.options.slideSpeed);
632				}
633				base.transition3d(goToPixel);
634			} else {
635				if(speed === true){
636					base.css2slide(goToPixel, base.options.paginationSpeed);
637				} else if(speed === "rewind" ){
638					base.css2slide(goToPixel, base.options.rewindSpeed);
639				} else {
640					base.css2slide(goToPixel, base.options.slideSpeed);
641				}
642			}
643			base.afterGo();
644		},
645
646		jumpTo : function(position){
647			var base = this;
648			if(typeof base.options.beforeMove === "function") {
649				base.options.beforeMove.apply(this,[base.$elem]);
650			}
651			if(position >= base.maximumItem || position === -1){
652				position = base.maximumItem;
653			}
654			else if( position <= 0 ){
655				position = 0;
656			}
657			base.swapSpeed(0)
658			if(base.browser.support3d === true){
659				base.transition3d(base.positionsInArray[position]);
660			} else {
661				base.css2slide(base.positionsInArray[position],1);
662			}
663			base.currentItem = base.owl.currentItem = position;
664			base.afterGo();
665		},
666
667		afterGo : function(){
668			var base = this;
669
670			base.prevArr.push(base.currentItem);
671			base.prevItem = base.owl.prevItem = base.prevArr[base.prevArr.length -2];
672			base.prevArr.shift(0)
673
674			if(base.prevItem !== base.currentItem){
675				base.checkPagination();
676				base.checkNavigation();
677				base.eachMoveUpdate();
678
679				if(base.options.autoPlay !== false){
680					base.checkAp();
681				}
682			}
683			if(typeof base.options.afterMove === "function" && base.prevItem !== base.currentItem) {
684				base.options.afterMove.apply(this,[base.$elem]);
685			}
686		},
687
688		stop : function(){
689			var base = this;
690			base.apStatus = "stop";
691			clearInterval(base.autoPlayInterval);
692		},
693
694		checkAp : function(){
695			var base = this;
696			if(base.apStatus !== "stop"){
697				base.play();
698			}
699		},
700
701		play : function(){
702			var base = this;
703			base.apStatus = "play";
704			if(base.options.autoPlay === false){
705				return false;
706			}
707			clearInterval(base.autoPlayInterval);
708			base.autoPlayInterval = setInterval(function(){
709				base.next(true);
710			},base.options.autoPlay);
711		},
712
713		swapSpeed : function(action){
714			var base = this;
715			if(action === "slideSpeed"){
716				base.$owlWrapper.css(base.addCssSpeed(base.options.slideSpeed));
717			} else if(action === "paginationSpeed" ){
718				base.$owlWrapper.css(base.addCssSpeed(base.options.paginationSpeed));
719			} else if(typeof action !== "string"){
720				base.$owlWrapper.css(base.addCssSpeed(action));
721			}
722		},
723
724		addCssSpeed : function(speed){
725			var base = this;
726			return {
727				"-webkit-transition": "all "+ speed +"ms ease",
728				"-moz-transition": "all "+ speed +"ms ease",
729				"-o-transition": "all "+ speed +"ms ease",
730				"transition": "all "+ speed +"ms ease"
731			};
732		},
733
734		removeTransition : function(){
735			return {
736				"-webkit-transition": "",
737				"-moz-transition": "",
738				"-o-transition": "",
739				"transition": ""
740			};
741		},
742
743		doTranslate : function(pixels){
744			return {
745				"-webkit-transform": "translate3d("+pixels+"px, 0px, 0px)",
746				"-moz-transform": "translate3d("+pixels+"px, 0px, 0px)",
747				"-o-transform": "translate3d("+pixels+"px, 0px, 0px)",
748				"-ms-transform": "translate3d("+pixels+"px, 0px, 0px)",
749				"transform": "translate3d("+pixels+"px, 0px,0px)"
750			};
751		},
752
753		transition3d : function(value){
754			var base = this;
755			base.$owlWrapper.css(base.doTranslate(value));
756		},
757
758		css2move : function(value){
759			var base = this;
760			base.$owlWrapper.css({"left" : value})
761		},
762
763		css2slide : function(value,speed){
764			var base = this;
765
766			base.isCssFinish = false;
767			base.$owlWrapper.stop(true,true).animate({
768				"left" : value
769			}, {
770				duration : speed || base.options.slideSpeed ,
771				complete : function(){
772					base.isCssFinish = true;
773				}
774			});
775		},
776
777		checkBrowser : function(){
778			var base = this;
779
780			//Check 3d support
781			var	translate3D = "translate3d(0px, 0px, 0px)",
782				tempElem = document.createElement("div");
783
784			tempElem.style.cssText= "  -moz-transform:"    + translate3D +
785								  "; -ms-transform:"     + translate3D +
786								  "; -o-transform:"      + translate3D +
787								  "; -webkit-transform:" + translate3D +
788								  "; transform:"         + translate3D;
789			var	regex = /translate3d\(0px, 0px, 0px\)/g,
790				asSupport = tempElem.style.cssText.match(regex),
791				support3d = (asSupport !== null && asSupport.length === 1);
792
793			var isTouch = "ontouchstart" in window || navigator.msMaxTouchPoints;
794
795			base.browser = {
796				"support3d" : support3d,
797				"isTouch" : isTouch
798			}
799		},
800
801		moveEvents : function(){
802			var base = this;
803			if(base.options.mouseDrag !== false || base.options.touchDrag !== false){
804				base.gestures();
805				base.disabledEvents();
806			}
807		},
808
809		eventTypes : function(){
810			var base = this;
811			var types = ["s","e","x"];
812
813			base.ev_types = {};
814
815			if(base.options.mouseDrag === true && base.options.touchDrag === true){
816				types = [
817					"touchstart.owl mousedown.owl",
818					"touchmove.owl mousemove.owl",
819					"touchend.owl touchcancel.owl mouseup.owl"
820				];
821			} else if(base.options.mouseDrag === false && base.options.touchDrag === true){
822				types = [
823					"touchstart.owl",
824					"touchmove.owl",
825					"touchend.owl touchcancel.owl"
826				];
827			} else if(base.options.mouseDrag === true && base.options.touchDrag === false){
828				types = [
829					"mousedown.owl",
830					"mousemove.owl",
831					"mouseup.owl"
832				];
833			}
834
835			base.ev_types["start"] = types[0];
836			base.ev_types["move"] = types[1];
837			base.ev_types["end"] = types[2];
838		},
839
840		disabledEvents :  function(){
841			var base = this;
842			base.$elem.on("dragstart.owl", function(event) { event.preventDefault();});
843			base.$elem.on("mousedown.disableTextSelect", function(e) {
844				return $(e.target).is('input, textarea, select, option');
845			});
846		},
847
848		gestures : function(){
849			var base = this;
850
851			var locals = {
852				offsetX : 0,
853				offsetY : 0,
854				baseElWidth : 0,
855				relativePos : 0,
856				position: null,
857				minSwipe : null,
858				maxSwipe: null,
859				sliding : null,
860				dargging: null,
861				targetElement : null
862			}
863
864			base.isCssFinish = true;
865
866			function getTouches(event){
867				if(event.touches){
868					return {
869						x : event.touches[0].pageX,
870						y : event.touches[0].pageY
871					}
872				} else {
873					if(event.pageX !== undefined){
874						return {
875							x : event.pageX,
876							y : event.pageY
877						}
878					} else {
879						return {
880							x : event.clientX,
881							y : event.clientY
882						}
883					}
884				}
885			}
886
887			function swapEvents(type){
888				if(type === "on"){
889					$(document).on(base.ev_types["move"], dragMove);
890					$(document).on(base.ev_types["end"], dragEnd);
891				} else if(type === "off"){
892					$(document).off(base.ev_types["move"]);
893					$(document).off(base.ev_types["end"]);
894				}
895			}
896
897			function dragStart(event) {
898				var event = event.originalEvent || event || window.event;
899
900				if (event.which === 3) {
901					return false;
902				}
903				if(base.itemsAmount <= base.options.items){
904					return;
905				}
906				if(base.isCssFinish === false && !base.options.dragBeforeAnimFinish ){
907					return false;
908				}
909				if(base.isCss3Finish === false && !base.options.dragBeforeAnimFinish ){
910					return false;
911				}
912
913				if(base.options.autoPlay !== false){
914					clearInterval(base.autoPlayInterval);
915				}
916
917				if(base.browser.isTouch !== true && !base.$owlWrapper.hasClass("grabbing")){
918					base.$owlWrapper.addClass("grabbing")
919				}
920
921				base.newPosX = 0;
922				base.newRelativeX = 0;
923
924				$(this).css(base.removeTransition());
925
926				var position = $(this).position();
927				locals.relativePos = position.left;
928				
929				locals.offsetX = getTouches(event).x - position.left;
930				locals.offsetY = getTouches(event).y - position.top;
931
932				swapEvents("on");
933
934				locals.sliding = false;
935				locals.targetElement = event.target || event.srcElement;
936			}
937
938			function dragMove(event){
939				var event = event.originalEvent || event || window.event;
940
941				base.newPosX = getTouches(event).x- locals.offsetX;
942				base.newPosY = getTouches(event).y - locals.offsetY;
943				base.newRelativeX = base.newPosX - locals.relativePos;	
944
945				if (typeof base.options.startDragging === "function" && locals.dragging !== true && base.newRelativeX !== 0) {
946					locals.dragging = true;
947					base.options.startDragging.apply(base,[base.$elem]);
948				}
949
950				if(base.newRelativeX > 8 || base.newRelativeX < -8 && base.browser.isTouch === true){
951					event.preventDefault ? event.preventDefault() : event.returnValue = false;
952					locals.sliding = true;
953				}
954
955				if((base.newPosY > 10 || base.newPosY < -10) && locals.sliding === false){
956					$(document).off("touchmove.owl");
957				}
958
959				var minSwipe = function(){
960					return  base.newRelativeX / 5;
961				}
962				var maxSwipe = function(){
963					return  base.maximumPixels + base.newRelativeX / 5;
964				}
965
966				base.newPosX = Math.max(Math.min( base.newPosX, minSwipe() ), maxSwipe() );
967				if(base.browser.support3d === true){
968					base.transition3d(base.newPosX);
969				} else {
970					base.css2move(base.newPosX);
971				}
972			}
973
974			function dragEnd(event){
975				var event = event.originalEvent || event || window.event;
976				event.target = event.target || event.srcElement;
977
978				locals.dragging = false;
979
980				if(base.browser.isTouch !== true){
981					base.$owlWrapper.removeClass("grabbing");
982				}
983
984				if(base.newRelativeX<0){
985					base.dragDirection = base.owl.dragDirection = "left"
986				} else {
987					base.dragDirection = base.owl.dragDirection = "right"
988				}
989
990				if(base.newRelativeX !== 0){
991					var newPosition = base.getNewPosition();
992					base.goTo(newPosition,false,"drag");
993					if(locals.targetElement === event.target && base.browser.isTouch !== true){
994						$(event.target).on("click.disable", function(ev){
995							ev.stopImmediatePropagation();
996							ev.stopPropagation();
997							ev.preventDefault();
998							$(event.target).off("click.disable");
999						});
1000						var handlers = $._data(event.target, "events")["click"];
1001						var owlStopEvent = handlers.pop();
1002						handlers.splice(0, 0, owlStopEvent);
1003					}
1004				}
1005				swapEvents("off");
1006			}
1007			base.$elem.on(base.ev_types["start"], ".owl-wrapper", dragStart); 
1008		},
1009
1010		getNewPosition : function(){
1011			var base = this,
1012				newPosition;
1013			
1014			newPosition = base.closestItem();
1015
1016			if(newPosition>base.maximumItem){
1017				base.currentItem = base.maximumItem;
1018				newPosition  = base.maximumItem;
1019			} else if( base.newPosX >=0 ){
1020				newPosition = 0;
1021				base.currentItem = 0;
1022			}
1023			return newPosition;
1024		},
1025		closestItem : function(){
1026			var base = this,
1027				array = base.options.scrollPerPage === true ? base.pagesInArray : base.positionsInArray,
1028				goal = base.newPosX,
1029				closest = null;
1030
1031			$.each(array, function(i,v){
1032				if( goal - (base.itemWidth/20) > array[i+1] && goal - (base.itemWidth/20)< v && base.moveDirection() === "left") {
1033					closest = v;
1034					if(base.options.scrollPerPage === true){
1035						base.currentItem = $.inArray(closest, base.positionsInArray);
1036					} else {
1037						base.currentItem = i;
1038					}
1039				} 
1040				else if (goal + (base.itemWidth/20) < v && goal + (base.itemWidth/20) > (array[i+1] || array[i]-base.itemWidth) && base.moveDirection() === "right"){
1041					if(base.options.scrollPerPage === true){
1042						closest = array[i+1] || array[array.length-1];
1043						base.currentItem = $.inArray(closest, base.positionsInArray);
1044					} else {
1045						closest = array[i+1];
1046						base.currentItem = i+1;
1047					}
1048				}
1049			});
1050			return base.currentItem;
1051		},
1052
1053		moveDirection : function(){
1054			var base = this,
1055				direction;
1056			if(base.newRelativeX < 0 ){
1057				direction = "right"
1058				base.playDirection = "next"
1059			} else {
1060				direction = "left"
1061				base.playDirection = "prev"
1062			}
1063			return direction
1064		},
1065
1066		customEvents : function(){
1067			var base = this;
1068			base.$elem.on("owl.next",function(){
1069				base.next();
1070			});
1071			base.$elem.on("owl.prev",function(){
1072				base.prev();
1073			});
1074			base.$elem.on("owl.play",function(event,speed){
1075				base.options.autoPlay = speed;
1076				base.play();
1077				base.hoverStatus = "play";
1078			});
1079			base.$elem.on("owl.stop",function(){
1080				base.stop();
1081				base.hoverStatus = "stop";
1082			});
1083			base.$elem.on("owl.goTo",function(event,item){
1084				base.goTo(item)
1085			});
1086			base.$elem.on("owl.jumpTo",function(event,item){
1087				base.jumpTo(item)
1088			});
1089		},
1090		
1091		stopOnHover : function(){
1092			var base = this;
1093			if(base.options.stopOnHover === true && base.browser.isTouch !== true && base.options.autoPlay !== false){
1094				base.$elem.on("mouseover", function(){
1095					base.stop();
1096				});
1097				base.$elem.on("mouseout", function(){
1098					if(base.hoverStatus !== "stop"){
1099						base.play();
1100					}
1101				});
1102			}
1103		},
1104
1105		lazyLoad : function(){
1106			var base = this;
1107
1108			if(base.options.lazyLoad === false){
1109				return false;
1110			}
1111			for(var i=0; i<base.itemsAmount; i++){
1112				var $item = $(base.$owlItems[i]);
1113
1114				if($item.data("owl-loaded") === "loaded"){
1115					continue;
1116				}
1117
1118				var	itemNumber = $item.data("owl-item"),
1119					$lazyImg = $item.find(".lazyOwl"),
1120					follow;
1121
1122				if( typeof $lazyImg.data("src") !== "string"){
1123					$item.data("owl-loaded","loaded");
1124					continue;
1125				}				
1126				if($item.data("owl-loaded") === undefined){
1127					$lazyImg.hide();
1128					$item.addClass("loading").data("owl-loaded","checked");
1129				}
1130				if(base.options.lazyFollow === true){
1131					follow = itemNumber >= base.currentItem;
1132				} else {
1133					follow = true;
1134				}
1135				if(follow && itemNumber < base.currentItem + base.options.items && $lazyImg.length){
1136					base.lazyPreload($item,$lazyImg);
1137				}
1138			}
1139		},
1140
1141		lazyPreload : function($item,$lazyImg){
1142			var base = this,
1143				iterations = 0;
1144				if ($lazyImg.prop("tagName") === "DIV") {
1145					$lazyImg.css("background-image", "url(" + $lazyImg.data("src")+ ")" );
1146					var isBackgroundImg=true;
1147				} else {
1148					$lazyImg[0].src = $lazyImg.data("src");
1149				}
1150				checkLazyImage();
1151
1152			function checkLazyImage(){
1153				iterations += 1;
1154				if (base.completeImg($lazyImg.get(0)) || isBackgroundImg === true) {
1155					showImage();
1156				} else if(iterations <= 100){//if image loads in less than 10 seconds 
1157					setTimeout(checkLazyImage,100);
1158				} else {
1159					showImage();
1160				}
1161			}
1162			function showImage(){
1163				$item.data("owl-loaded", "loaded").removeClass("loading");
1164				$lazyImg.removeAttr("data-src");
1165				base.options.lazyEffect === "fade" ? $lazyImg.fadeIn(400) : $lazyImg.show();
1166				if(typeof base.options.afterLazyLoad === "function") {
1167					base.options.afterLazyLoad.apply(this,[base.$elem]);
1168				}
1169			}
1170		},
1171
1172		autoHeight : function(){
1173			var base = this;
1174			var $currentimg = $(base.$owlItems[base.currentItem]).find("img");
1175
1176			if($currentimg.get(0) !== undefined ){
1177				var iterations = 0;
1178				checkImage();
1179			} else {
1180				addHeight();
1181			}
1182			function checkImage(){
1183				iterations += 1;
1184				if ( base.completeImg($currentimg.get(0)) ) {
1185					addHeight();
1186				} else if(iterations <= 100){ //if image loads in less than 10 seconds 
1187					setTimeout(checkImage,100);
1188				} else {
1189					base.wrapperOuter.css("height", ""); //Else remove height attribute
1190				}
1191			}
1192
1193			function addHeight(){
1194				var $currentItem = $(base.$owlItems[base.currentItem]).height();
1195				base.wrapperOuter.css("height",$currentItem+"px");
1196				if(!base.wrapperOuter.hasClass("autoHeight")){
1197					setTimeout(function(){
1198						base.wrapperOuter.addClass("autoHeight");
1199					},0);
1200				}
1201			}
1202		},
1203
1204		completeImg : function(img) {
1205		    if (!img.complete) {
1206		        return false;
1207		    }
1208		    if (typeof img.naturalWidth !== "undefined" && img.naturalWidth == 0) {
1209		        return false;
1210		    }
1211		    return true;
1212		},
1213
1214		onVisibleItems : function(){
1215			var base = this;
1216
1217			if(base.options.addClassActive === true){
1218				base.$owlItems.removeClass("active");
1219			}
1220			base.visibleItems = [];
1221			for(var i=base.currentItem; i<base.currentItem + base.options.items; i++){
1222				base.visibleItems.push(i);
1223
1224				if(base.options.addClassActive === true){
1225					$(base.$owlItems[i]).addClass("active");
1226				}
1227			}
1228			base.owl.visibleItems = base.visibleItems;
1229		},
1230
1231		transitionTypes : function(className){
1232			var base = this;
1233			//Currently available: "fade","backSlide","goDown","fadeUp"
1234			base.outClass = "owl-"+className+"-out";
1235			base.inClass = "owl-"+className+"-in";
1236		},
1237
1238		singleItemTransition : function(){
1239			var base = this;
1240			base.isTransition = true;
1241
1242			var outClass = base.outClass,
1243				inClass = base.inClass,
1244				$currentItem = base.$owlItems.eq(base.currentItem),
1245				$prevItem = base.$owlItems.eq(base.prevItem),
1246				prevPos = Math.abs(base.positionsInArray[base.currentItem]) + base.positionsInArray[base.prevItem],
1247				origin = Math.abs(base.positionsInArray[base.currentItem])+base.itemWidth/2;
1248
1249            base.$owlWrapper
1250	            .addClass('owl-origin')
1251	            .css({
1252	            	"-webkit-transform-origin" : origin+"px",
1253	            	"-moz-perspective-origin" : origin+"px",
1254	            	"perspective-origin" : origin+"px"
1255	            });
1256	        function transStyles(prevPos,zindex){
1257				return {
1258					"position" : "relative",
1259					"left" : prevPos+"px"
1260				};
1261			}
1262
1263	        var animEnd = 'webkitAnimationEnd oAnimationEnd MSAnimationEnd animationend';
1264
1265			$prevItem
1266			.css(transStyles(prevPos,10))
1267			.addClass(outClass)
1268			.on(animEnd, function() {
1269				base.endPrev = true;
1270				$prevItem.off(animEnd);
1271		    	base.clearTransStyle($prevItem,outClass);
1272			});
1273
1274			$currentItem
1275			.addClass(inClass)
1276			.on(animEnd, function() {
1277				base.endCurrent = true;
1278				$currentItem.off(animEnd);
1279		    	base.clearTransStyle($currentItem,inClass);
1280		    });
1281		},
1282
1283		clearTransStyle : function(item,classToRemove){
1284			var base = this;
1285			item.css({
1286					"position" : "",
1287					"left" : ""
1288				})
1289				.removeClass(classToRemove);
1290			if(base.endPrev && base.endCurrent){
1291				base.$owlWrapper.removeClass('owl-origin');
1292				base.endPrev = false;
1293				base.endCurrent = false;
1294				base.isTransition = false;
1295			}
1296		},
1297
1298		owlStatus : function(){
1299			var base = this;
1300			base.owl = {
1301				"userOptions"	: base.userOptions,
1302				"baseElement" 	: base.$elem,
1303				"userItems"		: base.$userItems,
1304				"owlItems"		: base.$owlItems,
1305				"currentItem"	: base.currentItem,
1306				"prevItem"		: base.prevItem,
1307				"visibleItems"	: base.visibleItems,
1308				"isTouch" 		: base.browser.isTouch,
1309				"browser"		: base.browser,
1310				"dragDirection" : base.dragDirection
1311			}
1312		},
1313
1314		clearEvents : function(){
1315			var base = this;
1316			base.$elem.off(".owl owl mousedown.disableTextSelect");
1317			$(document).off(".owl owl");
1318			$(window).off("resize", base.resizer);
1319		},
1320
1321		unWrap : function(){
1322			var base = this;
1323			if(base.$elem.children().length !== 0){
1324				base.$owlWrapper.unwrap();
1325				base.$userItems.unwrap().unwrap();
1326				if(base.owlControls){
1327					base.owlControls.remove();
1328				}
1329			}
1330			base.clearEvents();
1331			base.$elem
1332				.attr("style", base.$elem.data("owl-originalStyles") || "")
1333				.attr("class", base.$elem.data("owl-originalClasses"));
1334		},
1335
1336		destroy : function(){
1337			var base = this;
1338			base.stop();
1339			clearInterval(base.checkVisible);
1340			base.unWrap();
1341			base.$elem.removeData();
1342		},
1343
1344		reinit : function(newOptions){
1345			var base = this;
1346			var options = $.extend({}, base.userOptions, newOptions);
1347		 	base.unWrap();
1348		 	base.init(options,base.$elem);
1349		},
1350
1351		addItem : function(htmlString,targetPosition){
1352			var base = this,
1353				position;
1354
1355			if(!htmlString){return false}
1356
1357			if(base.$elem.children().length === 0){
1358				base.$elem.append(htmlString);
1359				base.setVars();
1360				return false;
1361			}
1362			base.unWrap();
1363			if(targetPosition === undefined || targetPosition === -1){
1364				position = -1;
1365			} else {
1366				position = targetPosition;
1367			}
1368			if(position >= base.$userItems.length || position === -1){
1369				base.$userItems.eq(-1).after(htmlString)
1370			} else {
1371				base.$userItems.eq(position).before(htmlString)
1372			}
1373
1374			base.setVars();
1375		},
1376
1377		removeItem : function(targetPosition){
1378			var base = this,
1379				position;
1380
1381			if(base.$elem.children().length === 0){return false}
1382			
1383			if(targetPosition === undefined || targetPosition === -1){
1384				position = -1;
1385			} else {
1386				position = targetPosition;
1387			}
1388
1389			base.unWrap();
1390			base.$userItems.eq(position).remove();
1391			base.setVars();
1392		}
1393
1394	};
1395
1396	$.fn.owlCarousel = function( options ){
1397		return this.each(function() {
1398			if($(this).data("owl-init") === true){
1399				return false;
1400			}
1401			$(this).data("owl-init", true);
1402			var carousel = Object.create( Carousel );
1403			carousel.init( options, this );
1404			$.data( this, "owlCarousel", carousel );
1405		});
1406	};
1407
1408	$.fn.owlCarousel.options = {
1409
1410		items : 5,
1411		itemsCustom : false,
1412		itemsDesktop : [1199,4],
1413		itemsDesktopSmall : [979,3],
1414		itemsTablet : [768,2],
1415		itemsTabletSmall : false,
1416		itemsMobile : [479,1],
1417		singleItem : false,
1418		itemsScaleUp : false,
1419
1420		slideSpeed : 200,
1421		paginationSpeed : 800,
1422		rewindSpeed : 1000,
1423
1424		autoPlay : false,
1425		stopOnHover : false,
1426
1427		navigation : false,
1428		navigationText : ["prev","next"],
1429		rewindNav : true,
1430		scrollPerPage : false,
1431
1432		pagination : true,
1433		paginationNumbers : false,
1434
1435		responsive : true,
1436		responsiveRefreshRate : 200,
1437		responsiveBaseWidth	: window,
1438		
1439
1440		baseClass : "owl-carousel",
1441		theme : "owl-theme",
1442
1443		lazyLoad : false,
1444		lazyFollow : true,
1445		lazyEffect : "fade",
1446
1447		autoHeight : false,
1448
1449		jsonPath : false,
1450		jsonSuccess : false,
1451
1452		dragBeforeAnimFinish : true,
1453		mouseDrag : true,
1454		touchDrag : true,
1455
1456		addClassActive : false,
1457		transitionStyle : false,
1458
1459		beforeUpdate : false,
1460		afterUpdate : false,
1461		beforeInit : false,
1462		afterInit : false,
1463		beforeMove : false,
1464		afterMove : false,
1465		afterAction : false,
1466		startDragging : false,
1467		afterLazyLoad: false
1468		
1469	};
1470})( jQuery, window, document );
1471
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Mocha on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)