jQuery Callback for a fadeIn and fadeOut

妖精的绣舞 提交于 2019-12-02 18:15:22

问题


I have looked at some of the other posts in the this community about the below issue and have not quite found what I am looking for...although I did find the below HTML and jQuery that was similar but not quite what I was looking for. This is for homework by the way, I would like to be up front with that. However I am pulling my hair out.

With that being said, I am looking for help in order to do a callback function to the below set of statements that will fade out the original image and then display the new caption and image automatically. The old caption and image should fade out and the new caption and image should fade in. I am a noob to all this and would like to learn all I can. I know that I am missing something. Every time I place the commented out piece, it makes the caption disappear. Below is the script:

$(document).ready(function() {
  $("#image_list a").click(function(pre) {
    pre.preventDefault();
    $("#image").fadeOut(1000);
    change($(this)).delay(1000);
    $("#caption").fadeOut(1000);
    change($(this)).delay(1000);
  }); //end click
}); //end ready

//image and caption fade
var change = function(img) {
  var caption = img.attr("title");
  var imageURL = img.attr("href");
  $("#caption").text(caption);
  //$(#caption).fadeIn();
  $("#image").attr("src", imageURL);
  $("#image").fadeIn();
}; //end image and caption fade

Here is the HTML:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Image Swap</title>
    <link rel="stylesheet" href="main.css" />
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="image_swap.js"></script>
</head>
<body>
    <main>
        <h1>Ram Tap Combined Test</h1>
        <ul id="image_list">
            <li><a href="images/h1.jpg" title="James Allison: 1-1">
                <img src="thumbnails/t1.jpg" alt=""></a></li>
            <li><a href="images/h2.jpg" title="James Allison: 1-2">
                <img src="thumbnails/t2.jpg" alt=""></a></li>
            <li><a href="images/h3.jpg" title="James Allison: 1-3">
                <img src="thumbnails/t3.jpg" alt=""></a></li>
            <li><a href="images/h4.jpg" title="James Allison: 1-4">
                <img src="thumbnails/t4.jpg" alt=""></a></li>
            <li><a href="images/h5.jpg" title="James Allison: 1-5">
                <img src="thumbnails/t5.jpg" alt=""></a></li>
            <li><a href="images/h6.jpg" title="James Allison: 1-6">
                <img src="thumbnails/t6.jpg" alt=""></a></li>
        </ul>
        <h2 id="caption">James Allison: 1-1</h2>               
        <p><img src="images/h1.jpg" alt="" id="image"></p>
    </main> 
</body>
</html>

回答1:


Use the callback provided:

$("#image").fadeOut(1000, function() {
    console.log("Done fading, do something else");
});


来源:https://stackoverflow.com/questions/35467008/jquery-callback-for-a-fadein-and-fadeout

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