2015-07-21 138 views
0
  1. 首先,有一行图片的下方有一个“See more”按钮 。
  2. 当单击“See more”按钮时,在第一行图像之后添加第二行图像,并且“See more”按钮被替换为“See less”按钮。
  3. 当单击“See less”按钮时,在上述步骤中添加的第二行图像 将被删除,并且“See less”按钮将被删除 。

问题:我怎样才能让这个本是一个无限循环,这里的“查看更多”按钮,然后将第3步后重新出现,从而使重复?jQuery:点击时显示,点击时隐藏,无限循环

这里是jQuery的:

$(document).ready(function() { 
     //Hides the new row to prepare for click event 
     $('.images-section').children('.new-row').hide(); 
     //Creating variables for see more/less buttons 
     var more = $('.images-button-more'); 
     var less = $('.images-button-less'); 
     less.hide(); 
     //Add new row on click of see more button 
     more.on('click', function() { 
      $('.images-section').children('.new-row').show(function() { 
       $(this).insertAfter('.old-row').slideDown('1000'); 
       more.fadeOut('slow', function() { 
        less.insertAfter(more).fadeIn('slow'); 
       }); 
      }); 
     }); 
     //Remove new row on click of see less button 
     less.on('click', function() { 
      $('.new-row').slideUp('1000', function() { 
       less.fadeOut('slow'); 
      }); 
     }); 
    }); 

这里的HTML:

<section class="no-padding remove-tablet"> 
     <div class="container-fluid images-section"> 
      <div class="row no-gutter old-row"> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/ant-johns.jpg" class="img-responsive" alt="Ant Johns" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/white-guy.jpg" class="img-responsive" alt="Arnold Jr" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/enrique-double-bi.jpg" class="img-responsive" alt="Enrique Double Bi" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/anthony-b.jpg" class="img-responsive" alt="Anthony B" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/manny-most-muscular.jpg" class="img-responsive" alt="Manny Most Muscular" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/remi-model-tats.jpg" class="img-responsive" alt="Remi Model Tats" width="317px" height="322px"> 
       </div> 
      </div> 
      <div class="row no-gutter new-row"> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/ant-johns.jpg" class="img-responsive" alt="Ant Johns" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/white-guy.jpg" class="img-responsive" alt="Arnold Jr" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/enrique-double-bi.jpg" class="img-responsive" alt="Enrique Double Bi" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/anthony-b.jpg" class="img-responsive" alt="Anthony B" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/manny-most-muscular.jpg" class="img-responsive" alt="Manny Most Muscular" width="317px" height="322px"> 
       </div> 
       <div class="col-lg-2 col-sm-6"> 
        <img src="img/remi-model-tats.jpg" class="img-responsive" alt="Remi Model Tats" width="317px" height="322px"> 
       </div> 
      </div> 
      <div class="row"> 
       <div class="col-sm-12 text-center"> 
        <a class="images-button-more" style="padding-bottom: 3px; border-bottom: 1px dotted rgb(130,181,65); position: relative; top: 5px; cursor: pointer; text-decoration: none; border-radius: 50%">See more</a> 
       </div> 
       <div class="col-sm-12 text-center"> 
        <a class="images-button-less" style="padding-bottom: 3px; border-bottom: 1px dotted rgb(130,181,65); position: relative; top: 5px; cursor: pointer; text-decoration: none; border-radius: 50%">See less</a> 
       </div> 
      </div> 
     </div> 
    </section> 

我试图把jQuery的周围的if语句,并把像 “isClosed,isNotClosed” 新的变量,然后改变他们的值从0到1基于某些事件和循环,如果isClosed是真实的,但这并没有对我有任何正义。

+0

也请添加html – depperm

+0

添加html =) – Lansana

回答

1

你只是缺少了jQuery一行:

less.on('click', function() { 
    $('.new-row').slideUp('1000', function() { 
     less.fadeOut('slow'); 
     more.insertAfter(less).fadeIn('slow');//this line 
    }); 
}); 
+0

这就是它,非常感谢!我知道这是一个小的东西..不能包裹我的头 - 几天前才开始学习jQuery哈哈。再次感谢。 – Lansana

2

一个选项是加载图像的第二排,当页面加载但display: none隐藏它,并给它像id='second-row'

一个ID,然后在点击事件,你可以做到以下几点:

$('#buttonId').on('click', function() { 
$('#second-row').slideToggle(); 
}); 

继承人小提琴http://jsfiddle.net/kqvzhoo1/

+0

很棒的建议!这也可以,谢谢。唯一的问题是我必须重写一些代码,但仍然有效。 – Lansana