I am currently working on creating my own image slider without relying on plugins.
1st inquiry : How can I achieve horizontal animation for the slider?
2nd inquiry : I want the images to cover both the height and width of their container while maintaining their original proportions. The images may be partially displayed. How can I accomplish this?
3rd inquiry : If anyone has any suggestions on optimizing and refining the slide code to make it more lightweight and elegant, please share your insights.
$(function(){
setInterval(function(){
var displayed = $(".img-header.displayed");
displayed.animate({opacity : 0}, 500, function() {
displayed.css("display","none");
displayed.addClass("not-displayed").removeClass("displayed");
if(displayed.next(".img-header.not-displayed").length == 0){
$(".img-header:first").css("display","inline-block").css("opacity","1");
$(".img-header:first").addClass("displayed").removeClass("not-displayed");
$(".img-header:first").animate({opacity : 1}, 500);
}
else{
displayed.next(".img-header.not-displayed").css("display","inline-block").css("opacity","1");
displayed.next(".img-header.not-displayed").addClass("displayed").removeClass("not-displayed");
displayed.next(".img-header.not-displayed").animate({opacity : 1}, 500);
}
});
}, 4000);
});
#slider-container{height: 200px; width: 200px;}
#slider-container img.img-header{ width: 100%; height: auto;}
.displayed{display: inline-block;}
.not-displayed{display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slider-container">
<img class="img-header displayed" src="https://i.stack.imgur.com/AIHnl.png" />
<img class="img-header not-displayed" src="https://i.stack.imgur.com/XQrms.png" />
</div>