$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Understanding JavaScriptjQuery code snippet - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Understanding JavaScriptjQuery code snippet - Stack Overflow

matteradmin12PV0评论

I came across the following code and was wondering what the IMG.active refers too. If someone could help, could you go line by line and write ments too?

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');

    // unment 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 );
});

I think this code is trying to pull images from underneath each image?

I came across the following code and was wondering what the IMG.active refers too. If someone could help, could you go line by line and write ments too?

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');

    // unment 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 );
});

I think this code is trying to pull images from underneath each image?

Share Improve this question edited Oct 30, 2014 at 19:03 locoboy asked Nov 15, 2011 at 19:57 locoboylocoboy 39.1k73 gold badges188 silver badges263 bronze badges 2
  • Looks like carousel code sorgalla./projects/jcarousel/examples/static_circular.html – lsl Commented Nov 15, 2011 at 20:03
  • +1 ... I had fun to figure out and build the gallery using your code! – Roko C. Buljan Commented Nov 15, 2011 at 21:26
Add a ment  | 

3 Answers 3

Reset to default 5

IMG.active refers to all image tags (<img />) with the active class, so:

<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

    // unment 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 plete 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 );
});

The ments are added below. Note: $active.next() also selects non-image elements. This is probably not intended. If you want to select the next image element, use $active.nextAll("img:first") instead.

function slideSwitch() {
    // Selects all <img class="active"> elements which are a child of 
    // an element with id="slideshow"
    var $active = $('#slideshow IMG.active');

    // If the collection $active contains no elements, select the last
    // <img> element which is a child of <.. id=slideshow>
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // If there's another element after <img>, select it. Otherwise, select
    // first <img>
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // Add `class=last-active` to the selected element.
    $active.addClass('last-active');

    $next.css({opacity: 0.0})   // Set opacity 0
        .addClass('active')     // Set class `active`
        .animate({opacity: 1.0}, 1000, function() { //Animate
            $active.removeClass('active last-active');
        });
}

// Create an interval when the document is ready
$(function() {
    setInterval( "slideSwitch()", 5000 );
});
//what is going on here?
var $next =  $active.next().length ? $active.next()
    : $('#slideshow IMG:first');

This is a ternary operator. Basically, if the next() function returns anything other than 0, the next image is assigned as whatever next() returns. Otherwise, it uses the first img element in the #slideshow element.

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

This adds the class of last-active to whichever element currently has a class of active.

Post a comment

comment list (0)

  1. No comments so far