I wanted to use Swipe.js
for my Slider App, but I found out that when I resize the browser window, the images will overlap. Please check the below screenshot, to better picture what I mean:
This effect seems to appear when previous image was bigger than the current one. I cannot figure out what is wrong.
Here is my CSS:
/* GALLERY CSS*/
.slider {
overflow: hidden;
/* visibility: hidden; */
position: relative;
}
.slider-wrap {
overflow: hidden;
position: relative;
}
.slider-wrap > div {
float: left;
width: 100%;
position: relative;
}
#image {
display: block;
height: auto;
width: auto;
min-height: 400px;
max-height: 400px;
display: block;
margin: 0 auto;
}
And this is the functions that is called when resize event occurs:
function setup() {
// cache slides
slides = slider.children;
length = slides.length;
// If there is only 1 slide, looping does not make sense
if( slides.length < 2 ) options.loopPanel = false;
// slides length correction, minus cloned slides
for (var i = 0; i < slides.length; i++) {
if (slides[i].getAttribute('data-cloned')) length--;
}
// special case if two slides
if (browser.transitions && options.loopPanel && slides.length < 3) {
slider.appendChild(slides[0].cloneNode(true));
slider.appendChild(slides[1].cloneNode(true));
slides = slider.children;
}
// create an array to store current positions of each slide
slidePos = new Array(slides.length);
// determine width of each slide
width = options.container.getBoundingClientRect().width || options.container.offsetWidth;
slider.style.width = (slides.length * width * 2) + 'px';
// stack elements
var pos = slides.length;
while(pos--) {
var slide = slides[pos];
slide.style.width = width + 'px';
slide.setAttribute('data-index', pos);
if ( browser.transitions ) {
slide.style.left = (pos * -width) + 'px';
move(pos, index > pos ? -width : (index < pos ? width : 0), 0);
}
}
// reposition elements before and after index
if ( options.loopPanel && browser.transitions) {
move(circle(index-1), -width, 0);
move(circle(index+1), width, 0);
}
if (!browser.transitions) slider.style.left = (index * -width) + 'px';
options.container.style.visibility = 'visible';
detachEvents();
attachEvents();
}
When I resize back to full screen it goes back to normal. Happens sooner on mobile devices, because their inital screen size is small, so I assume it has to do something with the image sizes and some properties are not properly adjusted when resizing. However, I am starring at my code and the source code on Github for soo long and I cannot find what I am doing wrong.
UPDATE:
Adding max-width: 100% to image style seems to fix issue of overlapping, but if I do that, my images won't preserve aspect ratio. Wider images are squished and lose aspect ratio this way. So still looking for solution.