-1

I've a slider in my code, it's need to be transition, but it doesn't work.

Here is the JS code It's also not working in CSS... It needs to be transitioning when the image is changing. I need a slow changing of images in my slider.

actually I tried this in js:

document.getElementById("img").style.transition = "5s ease";

Here is HTML

        <div class="photo_div">
            <img id="img" src="">
        </div>
        <button onclick="prev()">PREV</button>
        <button onclick="next()">NEXT</button>
    </div>

Here is JS

var names = ["img1.jpg", "img2.jpg", "img3.jpg"];
var index = 0;

function init() {
    document.getElementById("img").src = "photo/" + names[index];
    autoSlide();
}

function next() {
    index++;
    if (index == names.length) {
        index = 0;
    }
    document.getElementById("img").src = "photo/" + names[index];
    

}

function prev() {

    if (index == 0) {
        index = names.length
    }
    index--;

    document.getElementById("img").src = "photo/" + names[index];

}

function autoSlide() {
    if (index < names.length) {
        setInterval(function() {
         next();
        }, 3000);
    }
}
Nissa
  • 4,636
  • 8
  • 29
  • 37
VCF
  • 19
  • 1
  • 5
  • I've added the part HTML code – VCF Feb 15 '17 at 11:17
  • What sort of effect are you trying to achieve through the transition? fade transition? side transition? – Hamza Ishak Feb 15 '17 at 11:26
  • simple transition: transition = "5s ease"; – VCF Feb 15 '17 at 11:27
  • it's need to be transition when the image is changing, I need a slow changing of images in my slider – VCF Feb 15 '17 at 11:37
  • @VCF Transition happen when the text is changing. Refer link this jsfiddle- https://jsfiddle.net/kcwu006e/4/. lf you do with image, you should be proload images(that will cached) for perfect transition. – Mathankumar K Feb 18 '17 at 07:46

1 Answers1

0

In the next and prev functions fade out the image - and set queuing to true, then change the image, then fade the img tag back in.

You can try the following for next, and something similar for prev:

function next() {
    index++;
    if (index == names.length) {
        index = 0;
    }
    
    $('#img').fadeOut("slow", "linear", function(){
      document.getElementById("img").src = "photo/" + names[index];
       $('#img').fadeIn("slow", "linear");
    });
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you can't use jQuery, maybe have a look at this.

Community
  • 1
  • 1
user7491506
  • 186
  • 1
  • 3