SlideToggle不动画

时间:2018-09-10 15:09:45

标签: javascript jquery html css slidetoggle

我有两个容器div,每个容器div都有四个行。

单击按钮后,我希望第二个div向上滑动,隐藏或向下滑动并显示。

但是,当我单击它时,它仅会出现或消失,并且不会为幻灯片过渡设置动画。研究表明,通常这是绝对定位的问题,但是没有一个元素是绝对定位的。

此外,如果我将onclick侦听器应用于第一个div,它也可以正常工作。我在js fiddle中重新创建了该问题:

$('.more').click(function() {

  $('#group2').slideToggle(400);

})
html,
body {
  height: 100%;
  width: 100%;
}

.container {
  height: 100%;
  width: 100%;
}

#content-container {
  clear: both;
}

.group-containers a {
  background-color: blue;
  height: 200px;
  width: 23.5%;
  float: left;
  margin-top: 2%;
}

.group-containers a:not(:nth-child(4n)) {
  margin-right: 2%;
}

.boxes {
  background-color: blue !important;
  height: 200px;
  width: 100%;
  float: left;
}

.boxes:not(:nth-child(4n)) {
  margin-right: 2%;
}

.boxesTwo {
  background-color: red !important;
  height: 200px;
  width: 100%;
  float: left;
}

.boxesTwo:not(:nth-child(4n)) {
  margin-right: 2%;
}

.labels {
  background-color: blue;
  height: 50px;
  text-align: center;
  color: white;
  margin-top: 25%;
  padding-top: 5%;
  margin-left: 15%;
  margin-right: 15%;
}

.overflow-group {
  display: none;
}

.more {
  height: 30px;
  background-color: green;
  width: 100%;
  clear: both;
  margin-top: 2%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <section id="content-container">
    <section class="group-sections">
      <div class="group-containers" id="group1" style="display: block;">
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
      </div>

      <div class="group-containers overflow-group" id="group2" style="display: block;">
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
      </div>

      <div class="more">See more</div>
    </section>
  </section>
</div>

2 个答案:

答案 0 :(得分:1)

问题出在float上的.group-containers a内。这是一个使用display: inline-block的示例。

$('.more').click(function() {

  $('#group2').slideToggle(400);

});
html,
body {
  height: 100%;
  width: 100%;
}

.container {
  height: 100%;
  width: 100%;
}

#content-container {
  clear: both;
}

.group-containers a {
  background-color: blue;
  height: 200px;
  width: 22%;
  /*float: left;*/
  margin-top: 2%;
  display: inline-block;
}

.group-containers a:not(:nth-child(4n)) {
  margin-right: 2%;
}

.boxes {
  background-color: blue !important;
  height: 200px;
  width: 100%;
  float: left;
}

.boxes:not(:nth-child(4n)) {
  margin-right: 2%;
}

.boxesTwo {
  background-color: red !important;
  height: 200px;
  width: 100%;
  float: left;
}

.boxesTwo:not(:nth-child(4n)) {
  margin-right: 2%;
}

.labels {
  background-color: blue;
  height: 50px;
  text-align: center;
  color: white;
  margin-top: 25%;
  padding-top: 5%;
  margin-left: 15%;
  margin-right: 15%;
}

.overflow-group {
  display: none;
}

.more {
  height: 30px;
  background-color: green;
  width: 100%;
  clear: both;
  margin-top: 2%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <section id="content-container">
    <section class="group-sections">
      <div class="group-containers" id="group1" style="display: block;">
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
      </div>

      <div class="group-containers overflow-group" id="group2" style="display: block;">
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
      </div>

      <div class="more">See more</div>
    </section>
  </section>
</div>

答案 1 :(得分:-1)

这似乎不是问题。可能是由于jsfiddle。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $('.more').click(function(){

  $('#group2').slideToggle(400);
 
})
});
</script>

</head>
<body>
<style>

html, body{
	height:100%;
	width:100%;
}
.container{
	height:100%;
	width:100%;
}
#content-container{
	clear:both;
}

.group-containers a{
	background-color:blue;
	height: 200px;
	width:23.5%;
	float:left;
	margin-top:2%;
	
}
.group-containers a:not(:nth-child(4n)){
	margin-right:2%;
}
.boxes{
	background-color:blue !important;
	height: 200px;
	width:100%;
	float:left;
}
.boxes:not(:nth-child(4n)){
	margin-right:2%;
}
.boxesTwo{
	background-color:red !important;
	height: 200px;
	width:100%;
	float:left;
}
.boxesTwo:not(:nth-child(4n)){
	margin-right:2%;
}
.labels{
	background-color:blue;
	height:50px;
	text-align:center;
	color:white;
	margin-top:25%;
	padding-top:5%;
	margin-left: 15%;
    margin-right: 15%;
}
.overflow-group{
	display:none;
}
.more{
	height: 30px;
    background-color: green;
    width: 100%;
    clear:both;
    margin-top:2%;
}
</style>
<div class="container">
  <section id="content-container">
    <section class="group-sections">
	    <div class="group-containers" id="group1" style="display: block;">
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxes lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
      </div>
      
      <div class="group-containers overflow-group" id="group2" style="display: block;">
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
        <a href="">
          <div class="boxesTwo lazy" style="background: url() no-repeat center">
            <div class="labels">label</div>
          </div>
        </a>
      </div>
	
    <div class="more">See more</div>
  </section>
 </section>
</div>

</body>
</html>