[英]How do I make all div boxes shorten when a button is clicked?
I have the following code: 我有以下代码:
JS: JS:
<script type="text/javascript">
$(document).ready(function(){
$('[href=#del]').click(function(){
var myParent = $(this).closest('.box');
myParent.slideUp(800, function(){
myParent.remove();
}); }); });
</script>
CSS: CSS:
h1 {
font-family: Arial;
font-weight: 300;
color: #444;
}
a {
color: #fff;
font-family: Arial;
text-decoration:none;
margin-left: 5px;
}
.box {
width: 150px;
height: 300px;
margin-right: 5px;
}
.box1 {
background-color: red;
float: left;
display: inline-block;
}
.box2 {
background-color: green;
float: left;
display: inline-block;
}
.box3 {
background-color: blue;
float: left;
display: inline-block;
}
.box4 {
background-color: black;
float: left;
display: inline-block;
}
.container {
width: 660px;
margin: 50px auto;
}
HTML: HTML:
<div class="container">
<div class="box box1"><a href="#del">Toggle</a></div>
<div class="box box2"><a href="#del">Toggle</a></div>
<div class="box box3"><a href="#del">Toggle</a></div>
<div class="box box4"><a href="#del">Toggle</a></div>
</div>
Question: 题:
How can I use one anchor tag that has Toggle on it and when that is clicked all four boxes will reduce in size from height 300px to 150px? 如何使用上面带有Toggle的锚标签,当单击该锚标签时,所有四个框的大小都会从300px减小到150px? The code i have right now simply deletes it but i need it ot shrink the box to 150px from 300px.
我现在拥有的代码只是将其删除,但我需要将其从300px缩小到150px。 and when clicked it again it should return to 300px.
并再次单击它应返回300px。
$(document).ready(function () {
$('[href="#del"]').click(function () {
var $divs = $(this).closest('.container').find('.box');
$divs.css({
'height': '150px'
});
});
});
Demo --->
http://jsfiddle.net/jttwB/3/ 演示
--->
http://jsfiddle.net/jttwB/3/
Demo with animation --->
http://jsfiddle.net/jttwB/2/ 动画演示
--->
http://jsfiddle.net/jttwB/2/
I think this animate what you are looking for. 我认为这使您正在寻找的动画。 This will provide both the ability to toggle a single one or toggle all at the same time.
这将提供同时切换单个或同时切换全部的能力。 If you only need it one way, you can remove one set of the anchors and the associated jquery.
如果只需要一种方式,则可以删除一组锚点和关联的jquery。 If you "toggleAll" on one that is down, it will make them all up.. and vice versa.
如果“ toggleAll”出现故障,它将使它们全部恢复..反之亦然。
http://jsfiddle.net/pb6fM/7/ http://jsfiddle.net/pb6fM/7/
$(document).ready(function () {
$('[href=#toggleMe]').click(function () {
var myParent = $(this).closest('.box');
var h = "150px";
if(myParent.height() == "150") { h = "300px"; }
myParent.animate({
height: h
}, 800)
});
$('[href=#toggleAll]').click(function () {
var boxes = $('.box');
var h = "150px";
if($(this).closest('.box').height() == "150") { h = "300px"; }
boxes.animate({
height: h
}, 800)
});
});
Just take out the $(this).closest part and add an .each. 只需取出$(this).closest部分并添加一个.each。
$('.box').each(function (index, box) {
$(box).slideUp(800, function() {
$(box).remove();
});
});
Try this for a nicer effect... http://jsfiddle.net/KsV2L/ 尝试一下以获得更好的效果... http://jsfiddle.net/KsV2L/
$(document).ready(function(){
$('[href=#del]').click(function(){
$(this).closest('.box').animate({height:'150px',width:'0'},800,function(){$(this).remove()});
$(this).closest('.container').find('.box').animate({height:'150px'},800);
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.