Jquery向下滑动show div with loop

时间:2016-05-08 15:54:26

标签: javascript jquery html css

我有多个带有嵌套div的复杂div,包括文本和图像。当页面加载时,我显示4个div。下面显示了一个按钮,其中包含显示更多信息的选项。每次用户按下show more按钮时,使用Jquery向下滑动并显示接下来的2个div。共有28个div,所以它将是14行。一旦所有div都可见,按钮将变为"显示更少"其余的div将会向上滑动或隐藏(暂时已经到了这个部分)

由于某些原因,JsFiddle甚至没有正常工作:(,我不是最好的JavaScript。下面是代码和Jsfiddle Link。我在示例中制作了简单的div。

的jsfiddle

Link To Js Fiddle

HTML

<div class="leaders">

  <div class="colum-left">

 </div>
 <div class="colum-right">

</div>
   <div class="colum-left">

  </div>
  <div class="colum-right">

 </div>
</div>

 <div class="leaders-hidden">

   <div class="colum-left">

   </div>
 <div class="colum-right">

 </div>

</div>

<div class="leaders-hidden-2">

 <div class="colum-left">

  </div>
  <div class="colum-right">

 </div>

</div>

<div class="leaders-hidden-3">

 <div class="colum-left">

</div>
<div class="colum-right">

 </div>

</div>


 <center><span style="font-size: 18pt; color: #017dc5;"><a id="showmoreleaders" class="read_btw" style="color: #017dc5;" href="#">SEE MORE</a></span></center>

JS

var count = 0;

$(".leaders-hidden").addClass('hide');
$(".leaders-hidden-2").addClass('hide');
$(".leaders-hidden-3").addClass('hide');



$("#showmoreleaders").click(function() {
count++;
 });

if (count == 1) {
 $(".leaders-hidden").slideDown("slow", function() {
   // Animation complete.
   });
 }


 if (count == 2) {
   $(".leaders-hidden-2").slideDown("slow", function() {
   // Animation complete.
   });
 }

if (count == 3) {
  $(".leaders-hidden-3").slideDown("slow", function() {
  // Animation complete.
  });
}

CSS

.read_btw {
  border: 1px solid #017dc5;
  margin-top: 10px;
  padding: 15px;
  display: inline-block;
 }

.hide {
display: none;
}

.colum-left {
  float: left;
  width: 48%;
  border: 1px solid #d9dada;
  margin: 0px;
  border-radius: 6px;
  margin-right: 7.5px;
  margin-bottom: 20px;
  height: 200px;
   }

 .colum-right {
  float: right;
  width: 48%;
  border: 1px solid #d9dada;
  margin: 0px;
  border-radius: 6px;
  margin-right: 7.5px;
  margin-bottom: 20px;
  height: 200px;
 }

的jsfiddle

Link To Js Fiddle

1 个答案:

答案 0 :(得分:2)

你走了。

你有你的if在功能之外,这就是为什么它不起作用,也更新了所以你可以在它们全部显示后隐藏它们。

我也从a标签中删除了href,因为它导致页面一直向上移动。如果您没有要转到的链接,则不应将href设置为标记

<强> HTML

    <div class="leaders">

  <div class="colum-left">

  </div>
  <div class="colum-right">

  </div>
  <div class="colum-left">

  </div>
  <div class="colum-right">

  </div>
</div>

<div class="leaders-hidden">

  <div class="colum-left">

  </div>
  <div class="colum-right">

  </div>

</div>

<div class="leaders-hidden-2">

  <div class="colum-left">

  </div>
  <div class="colum-right">

  </div>

</div>

<div class="leaders-hidden-3">

  <div class="colum-left">

  </div>
  <div class="colum-right">

  </div>

</div>


<center><span style="font-size: 18pt; color: #017dc5;"><a id="showmoreleaders" class="read_btw" style="color: #017dc5;">SEE MORE</a></span></center>

<强> CSS

.read_btw {
  border: 1px solid #017dc5;
  margin-top: 10px;
  padding: 15px;
  display: inline-block;
}

.colum-left {
  float: left;
  width: 48%;
  border: 1px solid #d9dada;
  margin: 0px;
  border-radius: 6px;
  margin-right: 7.5px;
  margin-bottom: 20px;
  height: 200px;
}

.colum-right {
  float: right;
  width: 48%;
  border: 1px solid #d9dada;
  margin: 0px;
  border-radius: 6px;
  margin-right: 7.5px;
  margin-bottom: 20px;
  height: 200px;
}

.hide{
  display: none;
}

#showmoreleaders{
  cursor: pointer;
}

<强> JS

var count = 0;

$("#showmoreleaders").click(function() {

    count++;

    if (count == 1) {
        $(".leaders-hidden").slideDown("slow", function() {
            // Animation complete.
        });
    }

    if (count == 2) {
        $(".leaders-hidden-2").slideDown("slow", function() {
            // Animation complete.
        });
    }

    if (count == 3) {
        $(".leaders-hidden-3").slideDown("slow", function() {
            // Animation complete.
        });
        $('#showmoreleaders').html('SEE LESS');
    }

    if (count == 4) {
        $(".leaders-hidden-3").slideUp("slow", function() {
            // Animation complete.
        });
    }


    if (count == 5) {
        $(".leaders-hidden-2").slideUp("slow", function() {
            // Animation complete.
        });
    }

    if (count == 6) {
        $(".leaders-hidden").slideUp("slow", function() {
            // Animation complete.
        });
    }

    if (count == 7) {
        $('#showmoreleaders').html('SEE MORE');
        count = 0;
    }
});

$(".leaders-hidden").addClass('hide');
$(".leaders-hidden-2").addClass('hide');
$(".leaders-hidden-3").addClass('hide');