我是 jQuery 新手,无法找出循环一组基本轮播/横幅旋转器的代码的正确方法。我已经尝试了几个版本的“for”语句和 .each(),但我无法让它自己工作,所以我正在寻求帮助。
到目前为止,这是我的代码:
$('.next-1').click(function () {
$('.featured-1').fadeOut(500,function(){
$('.featured-2').fadeIn(500,function(){
$('.featured-2').toggleClass("hide");
});
});
});
$('.next-2').click(function () {
$('.featured-2').fadeOut(500,function(){
$('.featured-3').fadeIn(500,function(){
$('.featured-3').toggleClass("hide");
});
});
});
然后是用于返回滑块的类似代码块:
$('.prev-2').click(function () {
$('.featured-2').fadeOut(500,function(){
$('.featured-1').fadeIn(500,function(){
$('.featured-2').toggleClass("hide");
});
});
});
$('.prev-3').click(function () {
$('.featured-3').fadeOut(500,function(){
$('.featured-2').fadeIn(500,function(){
$('.featured-3').toggleClass("hide");
});
});
});
这段代码现在确实可以工作,当我知道我可以循环它时,我只是不想输出这么多不必要的代码行。我希望能够循环直到没有更多的“featured-n” div 循环通过(能够循环到开头也很棒!)
这是我用来生成每个“featured-n” div 块的 PHP/HTML:
function home_slider_loop() {
$count = 0;
query_posts ('tag=slider');
if (have_posts()) : while (have_posts()) : the_post();
$count++;
?>
<div class="featured-post featured-<?php echo $count; if ($count>1) { echo ' hide';}?>">
<div class="featured-header">
<a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>"><h1 class="featured-title"><?php the_title(); ?></h1></a>
<p class="author">Written by Evan Luzi</p>
</div>
<div class="image-wrap">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('full', array('class' => 'slider-image')); ?></a>
<div class="slider-nav">
<div class="featured-prev prev-<?php echo $count; ?>"></div>
<div class="featured-next next-<?php echo $count; ?>"></div>
</div><!--End Navigation-->
</div><!--End Image <?php echo $count; ?>-->
<div class="featured-footer">
<?php the_excerpt(); ?>
<a class="more-link" href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>" >Read more</a>
</div>
</div><!--End Featured <?php echo $count; ?>-->
<?php
endwhile;
endif;
}
这是其中一个静态 HTML 输出的示例(想象一下,随着“featured-n”类的递增,它迭代了几次:
<div class="featured-1">
<div class="featured-header">
<a href="http://www.tbabdev.com/?p=27" title="5 Useful Cinematography Apps for iOS You Should Download Today"><h1 class="featured-title">5 Useful Cinematography Apps for iOS You Should Download Today</h1></a>
<p class="author">Written by Evan Luzi</p>
</div>
<div class="image-wrap">
<a href="http://www.tbabdev.com/?p=27" title="5 Useful Cinematography Apps for iOS You Should Download Today"><img width="1018" height="416" src="http://www.tbabdev.com/wp-content/uploads/2013/07/cinematography-apps-8-hero.jpg" class="slider-image wp-post-image" alt="cinematography-apps-8-hero" /></a>
<div class="slider-nav">
<div class="featured-prev prev-1"></div>
<div class="featured-next next-1"></div>
</div><!--End Navigation-->
</div><!--End Image 1-->
<div class="featured-footer">
<p>The devices we have in our pockets, the ones that can run these apps, these are the new leathermans. They have everything we need. They eliminate the need to carry paper manuals and enable us to do complex timelapse calculations in a fraction of the time as a paper and pen.</p>
<a class="more-link" href="http://www.tbabdev.com/?p=27" alt="5 Useful Cinematography Apps for iOS You Should Download Today" >Read more</a>
</div>
</div><!--End Featured 1-->
您可以在http://www.tbabdev.com/上查看运行中的代码
提前感谢您的帮助,请善待n00b :)