Setting initialSlide with Canvas Swiper

2 replies · opened May 23, 2017

ZzarniwoopMay 23, 2017

Is it possible to set a inital slide (say slide 5) to be shown when a page loads. According to the swiper API it can be done but I am not sure how to implement within Canvas.

SSemicolon WebSTAFFMay 27, 2017

Hello,

This is Definitely Possible but you will need to modify the js/functions.js a bit. Simply find the sliderRun: function() Function Block and replace the entire Function Block with the following code:

sliderRun: function(){

	if( typeof Swiper === 'undefined' ) {
		console.log('sliderRun: Swiper not Defined.');
		return true;
	}

	if( $slider.hasClass('customjs') ) { return true; }

	if( $slider.hasClass('swiper_wrapper') ) {

		var element = $slider.filter('.swiper_wrapper'),
			elementDirection = element.attr('data-direction'),
			elementInitialSlide = element.attr('data-initial-slide'),
			elementSpeed = element.attr('data-speed'),
			elementAutoPlay = element.attr('data-autoplay'),
			elementLoop = element.attr('data-loop'),
			elementEffect = element.attr('data-effect'),
			elementGrabCursor = element.attr('data-grab'),
			slideNumberTotal = element.find('#slide-number-total'),
			slideNumberCurrent = element.find('#slide-number-current'),
			sliderVideoAutoPlay = element.attr('data-video-autoplay');

		if( !elementSpeed ) { elementSpeed = 300; }
		if( !elementDirection ) { elementDirection = 'horizontal'; }
		if( !elementInitialSlide ) { elementInitialSlide = 0; }
		if( elementAutoPlay ) { elementAutoPlay = Number( elementAutoPlay ); }
		if( elementLoop == 'true' ) { elementLoop = true; } else { elementLoop = false; }
		if( !elementEffect ) { elementEffect = 'slide'; }
		if( elementGrabCursor == 'false' ) { elementGrabCursor = false; } else { elementGrabCursor = true; }
		if( sliderVideoAutoPlay == 'false' ) { sliderVideoAutoPlay = false; } else { sliderVideoAutoPlay = true; }

		if( element.find('.swiper-pagination').length > 0 ) {
			var elementPagination = '.swiper-pagination',
				elementPaginationClickable = true;
		} else {
			var elementPagination = '',
				elementPaginationClickable = false;
		}

		var elementNavNext = '#slider-arrow-right',
			elementNavPrev = '#slider-arrow-left';

		swiperSlider = new Swiper( element.find('.swiper-parent') ,{
			initialSlide: Number( elementInitialSlide ),
			direction: elementDirection,
			speed: Number( elementSpeed ),
			autoplay: elementAutoPlay,
			loop: elementLoop,
			effect: elementEffect,
			slidesPerView: 1,
			grabCursor: elementGrabCursor,
			pagination: elementPagination,
			paginationClickable: elementPaginationClickable,
			prevButton: elementNavPrev,
			nextButton: elementNavNext,
			onInit: function(swiper){
				SEMICOLON.slider.sliderParallaxDimensions();
				element.find('.yt-bg-player').removeClass('customjs');
				SEMICOLON.widget.youtubeBgVideo();
				$('.swiper-slide-active [data-caption-animate]').each(function(){
					var $toAnimateElement = $(this),
						toAnimateDelay = $toAnimateElement.attr('data-caption-delay'),
						toAnimateDelayTime = 0;
					if( toAnimateDelay ) { toAnimateDelayTime = Number( toAnimateDelay ) + 750; } else { toAnimateDelayTime = 750; }
					if( !$toAnimateElement.hasClass('animated') ) {
						$toAnimateElement.addClass('not-animated');
						var elementAnimation = $toAnimateElement.attr('data-caption-animate');
						setTimeout(function() {
							$toAnimateElement.removeClass('not-animated').addClass( elementAnimation + ' animated');
						}, toAnimateDelayTime);
					}
				});
				$('[data-caption-animate]').each(function(){
					var $toAnimateElement = $(this),
						elementAnimation = $toAnimateElement.attr('data-caption-animate');
					if( $toAnimateElement.parents('.swiper-slide').hasClass('swiper-slide-active') ) { return true; }
					$toAnimateElement.removeClass('animated').removeClass(elementAnimation).addClass('not-animated');
				});
				SEMICOLON.slider.swiperSliderMenu();
			},
			onSlideChangeStart: function(swiper){
				if( slideNumberCurrent.length > 0 ){
					if( elementLoop == true ) {
						slideNumberCurrent.html( Number( element.find('.swiper-slide.swiper-slide-active').attr('data-swiper-slide-index') ) + 1 );
					} else {
						slideNumberCurrent.html( swiperSlider.activeIndex + 1 );
					}
				}
				$('[data-caption-animate]').each(function(){
					var $toAnimateElement = $(this),
						elementAnimation = $toAnimateElement.attr('data-caption-animate');
					if( $toAnimateElement.parents('.swiper-slide').hasClass('swiper-slide-active') ) { return true; }
					$toAnimateElement.removeClass('animated').removeClass(elementAnimation).addClass('not-animated');
				});
				SEMICOLON.slider.swiperSliderMenu();
			},
			onSlideChangeEnd: function(swiper){
				element.find('.swiper-slide').each(function(){
					var slideEl = $(this);
					if( slideEl.find('video').length > 0 && sliderVideoAutoPlay == true ) { slideEl.find('video').get(0).pause(); }
					if( slideEl.find('.yt-bg-player.mb_YTPlayer:not(.customjs)').length > 0 ) { slideEl.find('.yt-bg-player.mb_YTPlayer:not(.customjs)').YTPPause(); }
				});
				element.find('.swiper-slide:not(".swiper-slide-active")').each(function(){
					var slideEl = $(this);
					if( slideEl.find('video').length > 0 ) {
						if( slideEl.find('video').get(0).currentTime != 0 ) { slideEl.find('video').get(0).currentTime = 0; }
					}
					if( slideEl.find('.yt-bg-player.mb_YTPlayer:not(.customjs)').length > 0 ) {
						slideEl.find('.yt-bg-player.mb_YTPlayer:not(.customjs)').YTPGetPlayer().seekTo( slideEl.find('.yt-bg-player.mb_YTPlayer:not(.customjs)').attr('data-start') );
					}
				});
				if( element.find('.swiper-slide.swiper-slide-active').find('video').length > 0 && sliderVideoAutoPlay == true ) { element.find('.swiper-slide.swiper-slide-active').find('video').get(0).play(); }
				if( element.find('.swiper-slide.swiper-slide-active').find('.yt-bg-player.mb_YTPlayer:not(.customjs)').length > 0 && sliderVideoAutoPlay == true ) { element.find('.swiper-slide.swiper-slide-active').find('.yt-bg-player.mb_YTPlayer:not(.customjs)').YTPPlay(); }

				element.find('.swiper-slide.swiper-slide-active [data-caption-animate]').each(function(){
					var $toAnimateElement = $(this),
						toAnimateDelay = $toAnimateElement.attr('data-caption-delay'),
						toAnimateDelayTime = 0;
					if( toAnimateDelay ) { toAnimateDelayTime = Number( toAnimateDelay ) + 300; } else { toAnimateDelayTime = 300; }
					if( !$toAnimateElement.hasClass('animated') ) {
						$toAnimateElement.addClass('not-animated');
						var elementAnimation = $toAnimateElement.attr('data-caption-animate');
						setTimeout(function() {
							$toAnimateElement.removeClass('not-animated').addClass( elementAnimation + ' animated');
						}, toAnimateDelayTime);
					}
				});
			}
		});

		if( slideNumberCurrent.length > 0 ) {
			if( elementLoop == true ) {
				slideNumberCurrent.html( Number( element.find('.swiper-slide.swiper-slide-active').attr('data-swiper-slide-index') ) + 1 );
			} else {
				slideNumberCurrent.html( swiperSlider.activeIndex + 1 );
			}
		}
		if( slideNumberTotal.length > 0 ) {
			slideNumberTotal.html( element.find('.swiper-slide:not(.swiper-slide-duplicate)').length );
		}

	}
},

Now, you can simply add the data-initial-slide="0" Attribute to the #slider Tag to set the Initial Slide. The value will be an Index Number which starts from 0.

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

ZzarniwoopMay 27, 2017

Many thanks SemiColonWeb Team. Will try very soon. If I have any problems I will get back to you. This also makes it very clear as to how I can add any other need features.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard