Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Сокращение кода (https://javascript.ru/forum/misc/75236-sokrashhenie-koda.html)

JIeuTo 15.09.2018 10:56

Сокращение кода
 
Здравствуйте, есть данные настройки по swiper-master, и я сделал что бы при маленьких разрешениях было другое кол-во слайдов, подскажите как можно сократить код:
if( window.innerWidth >= 992 ){
	var swiper4 = new Swiper('.swiper-4', {
		slidesPerView: 6,
		spaceBetween: 30,
		loop: true,
		pagination: {
			el: '.swiper-pagination4',
			clickable: true,
		},
		autoplay: {
			delay: 3000,
			disableOnInteraction: false,
		},
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		},
	});
} else if ( window.innerWidth <= 480 ) {
	var swiper4 = new Swiper('.swiper-4', {
		slidesPerView: 1,
		spaceBetween: 30,
		loop: true,
		autoplay: {
			delay: 3000,
			disableOnInteraction: false,
		},
		pagination: {
			el: '.swiper-pagination4',
			clickable: true,
		},
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		},
	});
} else {
	var swiper4 = new Swiper('.swiper-4', {
		slidesPerView: 3,
		spaceBetween: 30,
		loop: true,
		autoplay: {
			delay: 3000,
			disableOnInteraction: false,
		},
		pagination: {
			el: '.swiper-pagination4',
			clickable: true,
		},
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		},
	});
}

destus 15.09.2018 11:15

var swiper4 = new Swiper('.swiper-4', {
    spaceBetween: 30,
    slidesPerView: window.innerWidth >= 992 ? 6 : window.innerWidth <= 480 ? 1 : 3
    loop: true,
    pagination: {
        el: '.swiper-pagination4',
        clickable: true,
    },
    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
    },
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    }
});

JIeuTo 15.09.2018 11:22

destus,
благодарю Вас, +


Часовой пояс GMT +3, время: 05:27.