slideToggle

时间:2016-03-01 13:03:02

标签: javascript jquery html css

我正在网站上工作,并且我提供了以下代码:



//SlideToggle
$('a').attr('href', 'javascript:void(0)');
$('a').click(function() {
  $(this).next().slideToggle();
});

$('.container div').addClass('cf');

.hidden {
  display: none;
}
.container {
  width: 100%;
  margin: 0 auto;
}
.container div {
  width: 25%;
  display: inline-block;
  vertical-align: top;
  text-align: center;
  background: lightblue;
  margin: 0 0 0.25em;
  padding: 25px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia ipsam, dolorem illum. Amet officiis dignissimos eligendi excepturi nam, aliquid architecto atque nostrum illo. Ratione nesciunt, id nulla dolores? Blanditiis, praesentium.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error sint nihil labore molestias expedita corrupti ipsa iusto iure sit nisi ab nobis maxime unde voluptates magni, distinctio quae. Deserunt, incidunt.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt accusamus facilis tempore repellat eligendi accusantium laudantium quis, magni a soluta deleniti voluptatibus quam, est aliquid. Atque a, esse. Itaque, consequuntur.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aperiam, quo, possimus. Quam odit laudantium, ex alias, repellat laborum quae nobis expedita aperiam iusto iure at facilis obcaecati doloribus quis earum.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quam ratione doloribus laborum deleniti quae cumque quasi, repellat, ex nemo nulla corporis voluptatum vel doloremque nesciunt cupiditate, veritatis excepturi vero harum.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam cupiditate quisquam, dicta eligendi ut eos culpa nisi quod nesciunt vero soluta voluptates illo esse voluptatibus, ab, sint impedit repudiandae dolorum!</p>
  </div>
</div>
&#13;
&#13;
&#13;

slideToggle运行良好。但是,当我使用slideToggle时,我希望此操作仅影响底部的div而不影响其他div。让我告诉你我想要的东西:

enter image description here

(我不想触摸div高度,他们必须保持这样)

希望我清楚。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

这很接近,也许有人能够改善这个......

&#13;
&#13;
//SlideToggle
	$('a').attr('href','javascript:void(0)');
	$('a').click(function() { 
       var $self = $(this);
       var index = $self.parent().index();     
		$(this).next().slideToggle(100, function() {           
            var margin = $self.parent().toggleClass("active").find("p").outerHeight();   
            if($self.parent().hasClass("active")) {
                 $(".container div").eq(index + 3).css("margin-top", margin);   
            }
             else {
                $(".container div").eq(index + 3).css("margin-top", 0);            
            }                     
        });         
	});
	$('.container div').addClass('cf');
&#13;
* {
  box-sizing:border-box;
  }

.hidden {
  display: none;
}

.container {
  width: 100%;
  margin: 0 auto;
}

.container div {
  width: 33.3333333333%;
  display: inline-block;
  vertical-align: top;
  text-align: center;
  background: lightblue;
  margin:0;
  padding: 25px;
  position:relative;
  -webkit-transition: ease-in-out, margin .1s ease-in-out;
  transition: ease-in-out, margin .1s ease-in-out;
  float:left;
}

.container div p {
  position:absolute;
  width:100%;
 left:0;
  padding:25px;
  margin:0;
  top:100%;
  background: lightblue;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia ipsam, dolorem illum. Amet officiis dignissimos eligendi excepturi nam, aliquid architecto atque nostrum illo. Ratione nesciunt, id nulla dolores? Blanditiis, praesentium.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error sint nihil labore molestias expedita corrupti ipsa iusto iure sit nisi ab nobis maxime unde voluptates magni, distinctio quae. Deserunt, incidunt.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt accusamus facilis tempore repellat eligendi accusantium laudantium quis, magni a soluta deleniti voluptatibus quam, est aliquid. Atque a, esse. Itaque, consequuntur.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aperiam, quo, possimus. Quam odit laudantium, ex alias, repellat laborum quae nobis expedita aperiam iusto iure at facilis obcaecati doloribus quis earum.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quam ratione doloribus laborum deleniti quae cumque quasi, repellat, ex nemo nulla corporis voluptatum vel doloremque nesciunt cupiditate, veritatis excepturi vero harum.</p>
  </div>
  <div><a href="#">See more</a>
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam cupiditate quisquam, dicta eligendi ut eos culpa nisi quod nesciunt vero soluta voluptates illo esse voluptatibus, ab, sint impedit repudiandae dolorum!</p>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个:

在执行幻灯片切换之前隐藏所有p标签。

$('a').click(function() {
        $('p').hide(); 
        $(this).next().slideToggle(); 
});