简体   繁体   中英

Easier way to do the following

I have made a script which slides thought some customers quotes. See code below

<script>

jQuery(document).ready(function () {
  jQuery(".da-feedback .da-quote").hide();
  jQuery(".da-feedback .da-quote:first-child").show();
  setTimeout('NextItem(1)',6000);
});

function NextItem($n) {

 jQuery('.da-feedback .da-quote:nth-child('+$n+')').slideUp('slow');
 $n = $n + 1;
 jQuery('.da-feedback .da-quote:nth-child('+$n+')').slideDown('slow');
 setTimeout('NextItem('+$n+')',6000);
}

</script>

<div class="da-feedback">
   <div class="da-quote" style="display: none; ">....</div>
   <div class="da-quote" style="display: none; ">....</div>
   <div class="da-quote" style="display: none; ">....</div>
</div>

I would like to know if jquerys gives you a function to make this easier.

Have you looked into .delay() ?

function NextItem(n) {
    $('.da-feedback .da-quote:nth-child('+$n+')').slideUp('slow')
        .delay(6000).slideDown('slow', NextItem(n++)); 
    // callback fires upon completion
};
NextItem(1);

Incidentally, it doesn't actually affect anything, but with JavaScript you don't need to start your variables with a $, and with jQuery it's considered a convention to do so if and only if the variable is a jQuery object.

Check out jQuery toggle () and jQuery next () and prev () methods.

Ohw...and maybe slideToggle () might also be of service for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM