javascript - 理解 JavaScript/jQuery 代码片段

标签 javascript jquery

我遇到了以下代码,并且想知道 IMG.active 指的是什么。如果有人可以帮忙,你能逐行写评论吗?

function slideSwitch() {
    //what does this and the next line do?
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    //what is going on here?
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    //can someone elaborate on these lines?
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

我认为这段代码试图从每个图像下面提取图像?

最佳答案

IMG.active引用所有带有 <img /> 的图像标签 ( active )类,所以:

<img src="..." class="active" />

--

function slideSwitch() {
    var $active = $('#slideshow IMG.active');//get all `img` elements with the `active` class within the #slideshow element
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');//if no elements were selected on the last line, then set the active slide (`$active`) to the last image in the slideshow

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');//check to see if there is another image after the current $active element, if not then set the $next variable to the first image in the slideshow

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');//add the `last-active` class to the current $active element

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });//these lines set the $next element to invisible, adds the `active` class, then animates its opacity to show the image, after the animation is complete it removes the `active` and `last-active` classes from the $next element.
}

//this runs the function above on an interval of 5sec when the `document.ready` event fires
$(function() {
    setInterval( "slideSwitch()", 5000 );
});

关于javascript - 理解 JavaScript/jQuery 代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8142359/

相关文章:

javascript - 测试事件处理程序是否绑定(bind)到 jQuery 中的元素

javascript - 使用 jQuery 获取格式化的日期和时间

javascript - Jquery 导航栏无法正常工作

javascript - 有没有办法从函数外部检索函数的参数

javascript - 侧面菜单不会在移动设备上滚动

javascript - 从 Firestore 获取记录进行分页 - Ionic/Angular

Javascript Regex - 仅匹配一组 4 个数字

javascript - 与 REQUEST_METHOD 相同的域 JQuery $.ajax 发送选项

jquery - 为什么我的 window.width 方法不能正常工作?

javascript - 如何在两个数字之间添加一个点?