javascript - 淡出、淡入和停止

标签 javascript jquery fadein fadeout

如何使用下面的代码使图像淡入并停止?我要添加什么? 预先感谢您的任何正确答案,非常感谢!

HTML:

<div class="fadeImg" >
    <img src="images/25sprout.jpg">
    <img style="display:none;" src="images/alex.jpg">
    <img style="display:none;" src="images/thundersha.jpg">
    <img style="display:none;" src="images/cathy.jpg">
    <img style="display:none;" src="images/david.jpg">
</div>

JQuery:

<script type="text/javascript" src="js/package/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $next = 1;          // fixed, please do not modfy;
    $current = 0;       // fixed, please do not modfy;
    $interval = 4000;   // You can set single picture show time;
    $fadeTime = 800;    // You can set fadeing-transition time;
    $imgNum = 5;        // How many pictures do you have

    $(document).ready(function(){
        //NOTE : Div Wrapper should with css: relative;
        //NOTE : img should with css: absolute;
        //NOTE : img Width & Height can change by you;
        $('.fadeImg').css('position','relative');
        $('.fadeImg img').css({'position':'absolute','width':'332px','height':'500px'});

        nextFadeIn();
    });

    function nextFadeIn(){
        //make image fade in and fade out at one time, without splash vsual;
        $('.fadeImg img').eq($current).delay($interval).fadeOut($fadeTime)
            .end().eq($next).delay($interval).hide().fadeIn($fadeTime, nextFadeIn);

        // if You have 5 images, then (eq) range is 0~4 
        // so we should reset to 0 when value > 4; 
        if($next < $imgNum-1){ $next++; } else { $next = 0;}
        if($current < $imgNum-1){ $current++; } else { $current =0; }
    };
</script>

最佳答案

最简单的选择是当 $current 匹配 $imgNum 时停止:

 function nextFadeIn() {
    if ($current >= $imgNum) return;
    //make image fade in and fade out at one time, without splash vsual;

尽管纯粹主义者可能不喜欢这样,因为这意味着 nextFadeIn() 的额外迭代,但它应该以最小的更改涵盖您所需要的内容。

关于javascript - 淡出、淡入和停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37951304/

相关文章:

javascript - 将类应用于 javascript 数据表中的每一行

javascript - Fancybox - 是否可以使用 javascript 变量字符串作为 HTML 内容?

javascript - jquery 图像悬停在我悬停时不断增长

Android 使用 ImageView 淡入淡出

javascript - 关于内容淡入/淡出效果的简单 jQuery 故障

javascript - 如果 div 被隐藏,如何使用 fadeIn()

javascript - 尝试包含多个js源时出错: Check your javascripts. joinTo config

javascript - 断言正确时 TestCafe 触发 "Assertion Error"

jquery - 根据复选框验证按钮

javascript - 折叠行组 Shiny