I am trying to use the horizontal photo scroller from here and it works when I have only one instance.
It is supposed to be that there is a stretch of images that you hit the arrow buttons to scroll left or right. I wanted to have two sets of images that scroll, and when I copy the same JS again (adjusting the id so it is performing on the correct button click) only the second one works.
I have tried to call the event listener on the div
tag that the buttons are inside of, but haven't had any luck... I am not experienced in event listeners at all, so I am not sure what rule I'm missing.
Any ideas on how to get both images to scroll?
JS:
<script>
window.addEventListener("load" , function (){
$(".previous_button1").on("click",function()
scroll(this,false); });
$(".next_button1").on("click", function(){
scroll(this,true); });
}, false);
function scroll(elem,next){
let target = $(elem).siblings(".data_preview_area1");
let width = target.outerWidth()
if (next){
target.animate({ scrollLeft:"+=" + String(width) } ,300);
}
else{
target.animate({ scrollLeft:"-=" + String(width) } ,300);
}
}
</script>
<script>
window.addEventListener("load" , function (){
$(".previous_button2").on("click",function(){
scroll(this,false); });
$(".next_button2").on("click", function(){
scroll(this,true); });
}, false);
function scroll(elem,next){
let target = $(elem).siblings(".data_preview_area2");
let width = target.outerWidth()
if (next){
target.animate({ scrollLeft:"+=" + String(width) } ,300);
}
else{
target.animate({ scrollLeft:"-=" + String(width) } ,300);
}
}
</script>