繁体   English   中英

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

[英]How do I make all div boxes shorten when a button is clicked?

我有以下代码:

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:

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:

<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>

题:

如何使用上面带有Toggle的锚标签,当单击该锚标签时,所有四个框的大小都会从300px减小到150px? 我现在拥有的代码只是将其删除,但我需要将其从300px缩小到150px。 并再次单击它应返回300px。

$(document).ready(function () {
    $('[href="#del"]').click(function () {
        var $divs = $(this).closest('.container').find('.box');
        $divs.css({
            'height': '150px'
        });
    });
});

演示---> http://jsfiddle.net/jttwB/3/

动画演示---> http://jsfiddle.net/jttwB/2/

我认为这使您正在寻找的动画。 这将提供同时切换单个或同时切换全部的能力。 如果只需要一种方式,则可以删除一组锚点和关联的jquery。 如果“ toggleAll”出现故障,它将使它们全部恢复..反之亦然。

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)
        });
    });

只需取出$(this).closest部分并添加一个.each。

$('.box').each(function (index, box) {
  $(box).slideUp(800, function() {
     $(box).remove();
   });
});

尝试一下以获得更好的效果... 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM