简体   繁体   English

单击按钮时如何缩短所有div框?

[英]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);
}); });

我怎么做<div>单击按钮时部分不可见(JS)?</div><div id="text_translate"><p> 如何使用 JS 隐藏&lt;div&gt;部分和</p><pre>&lt;div class="alertB1"&gt; &lt;div class="sticky"&gt; &lt;h1&gt; This site is still being built&lt;/h1&gt; &lt;p&gt; Please remember this site is still being built. Click &lt;a href="form1.html"&gt;here to report a bug &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; } &lt;/style&gt;</pre><p> 所以我希望包含一个按钮:</p><pre> &lt;span id="a33"&gt;&lt;button&gt;&amp;times;&lt;/button&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 如何让&lt;span&gt;隐藏&lt;div&gt;标签? 谢谢,环形游戏</p></div> - How do i make a <div> section invisible when a button is clicked (JS)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何做 <div> 单击按钮时滑入视图? - How Do I make a <div> slide into view when a button is clicked? 我怎么做<div>单击按钮时部分不可见(JS)?</div><div id="text_translate"><p> 如何使用 JS 隐藏&lt;div&gt;部分和</p><pre>&lt;div class="alertB1"&gt; &lt;div class="sticky"&gt; &lt;h1&gt; This site is still being built&lt;/h1&gt; &lt;p&gt; Please remember this site is still being built. Click &lt;a href="form1.html"&gt;here to report a bug &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; } &lt;/style&gt;</pre><p> 所以我希望包含一个按钮:</p><pre> &lt;span id="a33"&gt;&lt;button&gt;&amp;times;&lt;/button&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 如何让&lt;span&gt;隐藏&lt;div&gt;标签? 谢谢,环形游戏</p></div> - How do i make a <div> section invisible when a button is clicked (JS)? 单击按钮时如何使用 jquery 显示此 div - How do I display this div using jquery when a button is clicked 单击按钮时如何显示div? - How do I show div when button is clicked? 单击按钮时如何忽略div单击? - how do I ignore div click when button is clicked? 单击禁用按钮时如何显示模式? - How do I make a modal appear when the disabled button is clicked? 单击按钮时,如何使导航栏滚动页面? - How do I make a navbar scroll the page when clicked on a button? 单击“提交”按钮后,如何触发所有验证? - How do I trigger all validations when submit button is clicked? 点击javascript时如何让盒子消失? - How to make boxes disappear when clicked in javascript? 当我只单击div时如何单击按钮 - How can i make a button clicked when i just click a div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM