Issues recalling an animation function whilst making a jQuery image gallery. Next/Previous buttons

一个人想着一个人 提交于 2019-12-08 12:18:38

问题


this is my first ever post to Stack Overflow and it's because I've been tearing my hair out over this issue for the past day.

I'm pretty new to jQuery and have a basic understanding of how things work but I'm really struggling with getting a function to loop back on itself after it has processed. It's a simple if/else function that runs through a series of five images to see if they are visible or not.

If an image is visible (in other words, the current image in a gallery) then the next and previous buttons will control which image is next to be faded in and subsequently fade out the previous image.

I have the Next/Previous links working for the first image but I can't get it to work once the second image has faded in. I'm guessing it's because I need to recall the script for it to check if/else the images are visible. My problem is that I don't know where to place the code. I've tried the code in various places and I've been researching into jQuery delays and callbacks to see if I can try and work out whether it's the timings not working etc. But I just can't work it out!

If anyone can help get this working or even suggest a simpler way of creating this that I don't know, I would be greatly appreciative!

The images are simply listed within a div:

<div class="images">
    <img class="image gallery-img" id="1" src="/images/01.jpg" />
    <img class="image gallery-img" id="2" src="/images/02.jpg" />
    <img class="image gallery-img" id="3" src="/images/03.jpg" />
    <img class="image gallery-img" id="4" src="/images/04.jpg" />
    <img class="image gallery-img" id="5" src="/images/05.jpg" />
</div>

<div class="gallery-controls">
    <a class="gallery-button" id="previous" href="#previous">Previous</a>
    <a class="gallery-button" id="next" href="#next">Next</a>
</div>

And the jQuery is:

window.addEventListener( 'DOMContentLoaded', function(){

function galleryLoop(){

        if( $('div.images img:nth-child(1)').css({ 'display' : 'inline' }) ){

            //#NEXT CLICK
            $('a.gallery-button#next').bind('click', function(){
                $('div.images img:nth-child(1)').fadeOut( '100' , function(){ 
                    $('div.images img:nth-child(2)').fadeIn();
                });//END FADE OUT
            }); //END #NEXT CLICK

            //#PREVIOUS CLICK
            $('a.gallery-button#previous').bind('click', function(){
                $('div.images img:nth-child(1)').fadeOut( '100' , function(){ 
                    $('div.images img:nth-child(5)').fadeIn();
                });//END FADE OUT
            }); //END #PREVIOUS CLICK

        } //END IF

        else if( $('div.images img:nth-child(2)').css({ 'display' : 'inline' }) ){

            //#NEXT CLICK
            $('a.gallery-button#next').bind('click', function(){
                $('div.images img:nth-child(2)').fadeOut( '100' , function(){ 
                    $('div.images img:nth-child(3)').fadeIn();
                });//END FADE OUT
            }); //END #NEXT CLICK

            //#PREVIOUS CLICK
            $('a.gallery-button#previous').bind('click', function(){
                $('div.images img:nth-child(2)').fadeOut( '100' , function(){ 
                    $('div.images img:first-child').fadeIn();
                });//END FADE OUT
            }); //END #PREVIOUS CLICK

        }; //END IF

    };

galleryLoop();  

});//END SCRIPT


回答1:


Ok I spent a lot of time on this so I hope it solves your problem!

Here is the link to the JSFiddle: http://jsfiddle.net/ekUvj/22/

What you want to do is change you javascript to this:

var imgNum = 1;
$("#1").fadeIn(300);


function prev () {
    newImageNum=(imgNum == 1)?5:imgNum-1;
    $("#".concat(imgNum)).fadeOut(300, function () {$("#".concat(newImageNum)).fadeIn(300);});
    imgNum = newImageNum;
}

function next () {
    newImageNum=(imgNum == 5)?1:imgNum+1;
    $("#".concat(imgNum)).fadeOut(300, function () {$("#".concat(newImageNum)).fadeIn(300);});
    imgNum = newImageNum;
}​

And then make sure your buttons call the functions:

<div class="gallery-controls">
    <button onclick="prev()">Previous</a>
    <button onclick="next()">Next</a>
</div>​

Then make sure your sure your images are hidden at first in the CSS:

.gallery-img {
    display: none;
}​



回答2:


Take the function out of the event listener and you're gonna be able to call it anytime.



来源:https://stackoverflow.com/questions/11003205/issues-recalling-an-animation-function-whilst-making-a-jquery-image-gallery-nex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!