chandan
chandan

Reputation: 1574

Looping through for slider

Firstly here's the link

I am trying to create a slider, after the slider finishes looping I am unable to display the first slide. I want the slider to show all the sliders in continous loops without any break.

HTML Code:

<div class="main-container">
    <div class="main-slider">
        <div id="mydiv">    
        </div>
    </div>
</div>

Inside the div '#mydiv' the slider is added.

JS code:

$(document).ready(function() {
var screenHeight = $(window).height();
var screenWidth = $(document).width();

$(".main-slider").css("height", screenHeight + 4);


var imageURLS = [
    "http://images6.fanpop.com/image/photos/36800000/Beautiful-Landscapes-image-beautiful-landscapes-36803145-1920-1080.jpg",
    "http://www.cnmuqi.com/data/out/222/images-of-landscapes-7409399.jpeg",
    "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcS5cXFv_tmqWQFHllbUOvL9reoqTB9jQLWc-16Sj_r2vKPhNqobaQ",
    "http://www.ucreative.com/wp-content/uploads/2014/11/Landscape-Photography-Banner1.jpg",
    "http://www.qqxxzx.com/images/images-of-landscapes/images-of-landscapes-8.jpg",
    "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRNO6NIwG_n-pFppB_-DFN3Pt5M-sWe_-CMxR6MDz3eV9i38bA2",
    "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTyer4s2_wLSkH86aSggqXv5nXU6z6pQC_RNGYEcIgjuQAESwtG",
    "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSwQAHl6_-8GpV7c86T_qSSWlF6bsyyl4JoYVh4flC2dcZDiZGL"

];

$.each(imageURLS, function(k, v) {
    $('#mydiv').append('<div class="image_block"><img  src="' + v + '" /></div>');
});

$(".image_block img").css("width", screenWidth);
$(".image_block img").css("height", screenHeight);

function initial_call() {

    $('.image_block img').eq(0).attr('src', imageURLS[0]);
    $('.image_block img').eq(1).attr('src', imageURLS[1]);
}
//  initial_call();
var count = 1;
setInterval(function() {

    translate_value = count * screenHeight + count * 4;
    $("#mydiv").css("transform", "translate(0px,-" + translate_value + "px)");

    count++;
}, 2000);




}); //end of $(document).ready();

Any help would be appreciated.

Upvotes: 1

Views: 634

Answers (2)

MohammadReza Mahmoudi
MohammadReza Mahmoudi

Reputation: 1324

I add var finishNumber = imageHeight * imageURLS.length; so with this variable i can check the finish point and set the translate_value to 0.

if(translate_value == finishNumber){
    translate_value = 0;
}

Final Code

jQuery:

$(document).ready(function() {
var screenHeight = $(window).height();
var screenWidth = $(document).width();
var imageHeight = screenHeight;
$(".main-slider").css("height", screenHeight);

var imageURLS = [
    "http://images6.fanpop.com/image/photos/36800000/Beautiful-Landscapes-image-beautiful-landscapes-36803145-1920-1080.jpg",
    "http://www.cnmuqi.com/data/out/222/images-of-landscapes-7409399.jpeg",
    "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcS5cXFv_tmqWQFHllbUOvL9reoqTB9jQLWc-16Sj_r2vKPhNqobaQ",
    "http://www.ucreative.com/wp-content/uploads/2014/11/Landscape-Photography-Banner1.jpg",
    "http://www.qqxxzx.com/images/images-of-landscapes/images-of-landscapes-8.jpg",
    "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRNO6NIwG_n-pFppB_-DFN3Pt5M-sWe_-CMxR6MDz3eV9i38bA2",
    "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTyer4s2_wLSkH86aSggqXv5nXU6z6pQC_RNGYEcIgjuQAESwtG",
    "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSwQAHl6_-8GpV7c86T_qSSWlF6bsyyl4JoYVh4flC2dcZDiZGL"
];

$.each(imageURLS, function(k, v) {
    $('#mydiv').append('<div class="image_block"><img  src="' + v + '" /></div>');
});

$(".image_block img").css("width", screenWidth);
$(".image_block img").css("height", screenHeight);

function initial_call() {

    $('.image_block img').eq(0).attr('src', imageURLS[0]);
    $('.image_block img').eq(1).attr('src', imageURLS[1]);
}
//  initial_call();
var count = 1;
var translate_value = 0;
var finishNumber = imageHeight * imageURLS.length;
setInterval(function() {
    console.log(imageURLS.length)
    translate_value = translate_value + screenHeight;
    if(translate_value == finishNumber){
        translate_value = 0;
    }
    $("#mydiv").css("transform", "translate(0px,-" + translate_value + "px)");

    count++;
}, 2000);




}); //end of $(document).ready();

HTML:

<div class="main-container">
    <div class="main-slider">
        <div id="mydiv">    
        </div>
    </div>
</div>

CSS:

html,body{
  padding: 0px;
  margin: 0px;
}
.main-container{
  width: 100%;
}
.main-slider{
  height: 100%;
  overflow: hidden;
}
.image_block img{
  display: block;
}
#mydiv{
  -webkit-transition: all 1s cubic-bezier(.77,0,.175,1); /* For Safari 3.1 to 6.0 */
  transition: all 1s cubic-bezier(.77,0,.175,1);

  display: inline-block;
 }
 .primary-slide img,
 .followup-slide img,
 .slide img{
    width: 100%;
 }

Working example: https://jsfiddle.net/81jhnb5a/2/

Upvotes: 1

depperm
depperm

Reputation: 10746

You can simply add the following line:

count++;
count%=imageURLS.length;

Count will continue to increment but then wrap once it reaches the end.

Working example

Upvotes: 1

Related Questions