JSFiddle - React, Tailwind, and code Playground

by Felipe Alfaro

HTML

<div class="mySlider setup" data-slider-width="100%">
    <section>lorem ipsum 1</section>
    <section>lorem ipsum 2</section>
    <section>lorem ipsum 3</section>
    <section>lorem ipsum DFSD FSD 4</section>
    <section>lorem ipsum 5</section>
    <section>lorem ipsum 6</section>
</div>

CSS

html, body{
  margin: 0; background: black;
}
.mySliderWrapper{
  box-sizing: border-box;
  padding: 0 15px;
  position: relative;
}
.mySliderWrapper > a{
  display: block;
  position: absolute;
  top: 50%;
  margin-top: -15px;
  width: 0;
  height: 0;
  cursor: pointer;
  border-top: solid 15px transparent;
  border-bottom: solid 15px transparent;
}
.mySliderWrapper > a:nth-child(2){
  border-right: solid 15px white;
  left: 0;
}
.mySliderWrapper > a:nth-child(3){
  border-left: solid 15px white;
  right: 0;
}
.mySliderMask{
  min-height: 30px;
  overflow: hidden;
  position: relative;
}
.mySlider{
  min-width: 100%;
  display: flex;
  flex-direction: row;
  height: 100%;
  -webkit-transition: 200ms;
  -moz-transition: 200ms;
  -ms-transition: 200ms;
  -o-transition: 200ms;
  transition: 200ms;
}
.mySlider > *{
  box-sizing: border-box;
  width: 100%;
}
.setup{
  width: 200%;
}
@media screen and (max-width:400px){
  .setup{
    width: 600% !important;
  }
}

JavaScript

(function(doc){
	document.addEventListener("DOMContentLoaded", function(event) { 

		var sliders = doc.getElementsByClassName('mySlider');
		[].forEach.call(sliders, function(slider){
			var width = slider.getAttribute('data-slider-width') || '400px',
			parent = slider.parentNode,
			wrapper = doc.createElement('div'),
			mask = doc.createElement('div'),
			buttonPrev = doc.createElement('a'),
			buttonNext = doc.createElement('a'),
			m = 0;
			wrapper.style.width = width;
			wrapper.className = 'mySliderWrapper';
			mask.className = 'mySliderMask';
			parent.replaceChild(wrapper, slider);
			wrapper.appendChild(mask);
			wrapper.appendChild(buttonPrev);
			wrapper.appendChild(buttonNext);
			mask.appendChild(slider);
			buttonNext.onclick = function(e){
				e.preventDefault();
				var sliderWidth = mask.offsetWidth,
				cartWidth = slider.offsetWidth;
				if( m*sliderWidth >= cartWidth-sliderWidth ){
					m = 0;
				}else{
					m += 1;
				}
				slider.style.marginLeft = '-'+m*100+'%';
			};
			buttonPrev.onclick = function(e){
				e.preventDefault();
				var maskWidth = mask.offsetWidth,
				cartWidth = slider.offsetWidth;
				if( m*maskWidth == 0 ){
				m = Math.ceil(cartWidth/maskWidth) - 1;
				}else{
				m -= 1;
				}
				slider.style.marginLeft = '-'+m*100+'%';
			};
			function resizeFunc(){
				var maskWidth = mask.offsetWidth,
				cartWidth = slider.offsetWidth;
				if( m*maskWidth >= cartWidth-maskWidth ){
					m = 0;
					slider.style.marginLeft = '-'+m*100+'%';
				}
			};
			window.addEventListener('resize', resizeFunc, true);
		});

	});

})(document);