2014-09-30 190 views
2

我在Jquery中创建了一个函数来展开和折叠div的内容。但现在我想只能用CSS来使它也使用箭头的图像,像这样的人用css展开和折叠

enter image description here enter image description here

观看现场jsFiddle

我也想消除所有这些标签跨度和保持唯一的div和它的内容

这里我到目前为止的代码。

<div class='showHide'> 
    <span class='expand'><span id="changeArrow">&#8593;</span>Line expand and collapse</span> 
    <fieldset id="fdstLorem">Lorem ipsum...</fieldset> 
</div> 
$(document).ready(function() { 
    $('.showHide>span').click(function() { 
     $(this).next().slideToggle("slow"); 
     return false; 
    }); 
    $(".showHide>span").toggle(function() { 
     $(this).children("#changeArrow").text("↓"); 
    }, function() { 
     $(this).children("#changeArrow").text("↑"); 
    }); 
}); 
+0

你不能这样做只有在CSS。 – deKajoo 2014-09-30 12:42:12

+0

我以为我可以。那么我怎么能至少用这些箭头去除不必要的html? – gbvisconti 2014-09-30 12:43:22

+1

看看这个纯粹的CSS解决方案:http://stackoverflow.com/questions/9100344/pure-css-multi-level-drop-down-menu – dippas 2014-09-30 12:47:13

回答

12

.fieldsetContainer { 
 
    height: 0; 
 
    overflow: hidden; 
 
    transition: height 400ms linear; 
 
} 
 

 
#toggle { 
 
    display: none; 
 
} 
 

 
#toggle:checked ~ .fieldsetContainer { 
 
    height: 50px; 
 
} 
 

 
label .arrow-dn { display: inline-block; } 
 
label .arrow-up { display: none; } 
 

 
#toggle:checked ~ label .arrow-dn { display: none; } 
 
#toggle:checked ~ label .arrow-up { display: inline-block; }
<div class='showHide'> 
 
    <input type="checkbox" id="toggle" /> 
 
    
 
    <label for="toggle"> 
 
     <span class='expand'> 
 
      <span class="changeArrow arrow-up">↑</span> 
 
      <span class="changeArrow arrow-dn">↓</span> 
 
      Line expand and collapse 
 
     </span> 
 
    </label> 
 
    
 
    <div class="fieldsetContainer"> 
 
     <fieldset id="fdstLorem"> 
 
      Lorem ipsum... 
 
     </fieldset> 
 
    </div> 
 
</div>

+0

是否有一些简洁的方法可以让每个页面为多个可折叠区域做这项工作? – ehdr 2017-07-30 09:37:32

0

这是我做的方式,相较于复选框做的东西很简洁干净,而只是扩展了悬停或点击&保持(其对触摸有好处)...

<style> 

.collapse-able { 
    height: 1.2rem; 
    overflow: hidden; 
} 

.collapse-able:active, .collapse-able:hover { 
    height: auto; 
} 

</style> 

<div class="collapse-able"> 
    <h1> The first line will always show </h1> 
    </br> 
    But what's underneath won't until hover or click and drag. Which is neat. 
</div>