如何停止图像滑块上的动画文本

时间:2013-12-13 17:20:11

标签: javascript jquery animation

除了如何复制和粘贴它之外,我对jquery或javascript知之甚少。当幻灯片加载时,试图阻止我的文本弹回到页面上。我只是希望它出现在幻灯片上,不需要动画。

// Speed of the automatic slideshow
var slideshowSpeed = 5000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
    "title" : "Learn More",
    "image" : "nature.jpg",
    "url" : "#",
    "firstline" : "Hello There",
    "secondline" : "How are you doing today?"
}, {
    "title" : "View Products",
    "image" : "biking.jpg",
    "url" : "#",
    "firstline" : "Need Great Product",
    "secondline" : "See our full selection of goods"
}, {
    "title" : "Buy Now",
    "image" : "vacation.jpg",
    "url" : "#",
    "firstline" : "Here is the product",
    "secondline" : "So buy it up or you will be sorry"
}
];


$(document).ready(function() {

// Backwards navigation
$("#back").click(function() {
    stopAnimation();
    navigate("back");
});

// Forward navigation
$("#next").click(function() {
    stopAnimation();
    navigate("next");
});

var interval;
$("#control").toggle(function(){
    stopAnimation();
}, function() {
    // Change the background image to "pause"
    $(this).css({ "background-image" : "url(images/btn_pause.png)" });

    // Show the next image
    navigate("next");

    // Start playing the animation
    interval = setInterval(function() {
        navigate("next");
    }, slideshowSpeed);
});


var activeContainer = 1;    
var currentImg = 0;
var animating = false;
var navigate = function(direction) {
    // Check if no animation is running. If it is, prevent the action
    if(animating) {
        return;
    }

    // Check which current image we need to show
    if(direction == "next") {
        currentImg++;
        if(currentImg == photos.length + 1) {
            currentImg = 1;
        }
    } else {
        currentImg--;
        if(currentImg == 0) {
            currentImg = photos.length;
        }
    }

    // Check which container we need to use
    var currentContainer = activeContainer;
    if(activeContainer == 1) {
        activeContainer = 2;
    } else {
        activeContainer = 1;
    }

    showImage(photos[currentImg - 1], currentContainer, activeContainer);

};

var currentZindex = -1;
var showImage = function(photoObject, currentContainer, activeContainer) {
    animating = true;

    // Make sure the new container is always on the background
    currentZindex--;

    // Set the background image of the new active container
    $("#heroimg" + activeContainer).css({
        "background-image" : "url(images/" + photoObject.image + ")",
        "display" : "block",
        "z-index" : currentZindex
    });

    // Hide the hero text
    $("#slider-btns").css({"display" : "none"});

    // Set the hero text
    $("#firstline").html(photoObject.firstline);
    $("#secondline")
        .attr("href", photoObject.url)
        .html(photoObject.secondline);
    $("#btn-click")
        .attr("href", photoObject.url)
        .html(photoObject.title);


    // Fade out the current container
    // and display the header text when animation is complete
    $("#heroimg" + currentContainer).fadeOut(function() {
        setTimeout(function() {
            $("#slider-btns").css({"display" : "block"});
            animating = false;
        }, 300);
    });
};



// We should statically set the first image
navigate("next");

// Start playing the animation
interval = setInterval(function() {
    navigate("next");
}, slideshowSpeed);

});

1 个答案:

答案 0 :(得分:1)

文本在图像切换期间反弹,您隐藏了#slider-btns div,并在图像切换后显示。 由于文本堆叠在#slider-btns div之后,当你隐藏#slider-btns时文本会上升,当你显示文本时,文本就会消失。