0

我编写了以下 jQuery 代码,当单击链接时,它会使 div 向下滑动。在没有 jQuery 的纯 css3 中是否有可能?

http://jsbin.com/exakuj/5

HTML

<header>
  <h1>Heading</h1>
</header>
<section>
  <a href='#' id='click'>Lorem ipsum dolor sit amet, consectetur adipiscing elit vestibulum convallis.</a>
  <div id='show'></div>
</section>

jQuery

$('#click').click(function() {
$('#show').slideDown('slow', function() {
// Animation complete.
  });
});
4

4 回答 4

1

没有 JQUERY 的演示小提琴

这是代码 -

html -

<label for="test1" class="test">Click Me ;)</label>
<input type="checkbox" id="test1"/>
<div class="test2">
    Yo!! you clicked it, now I am visible to you with a smooth animation
</div>

css-

.test{
    background: yellow;
    padding:10px;
    display:block;
    font-family: tahoma;
    text-align:center;
    width:100px;
    font-weight:bold;
    clear: both;
}
.test2{
    width:100px;
    height:0px;
    text-align:center;
    font-size:12px;
    font-family: tahoma;
    color:#fff;
    font-weight:bold;
    padding:10px;
    opacity:0;
    float:left;
    overflow:hidden;
    background:red;
    -webkit-transition: all 0.7s ease;
    -moz-transition: all 0.7s ease;
    -ms-transition: all 0.7s ease;
    -o-transition: all 0.7s ease;
    transition: all 0.7s ease;
}
#test1:checked + .test2{
    height: 70px;
    opacity:1;
}
#test1{
    display:none;
}
于 2013-06-02T12:34:49.693 回答
1

是的,但你可能还是想要一些 javascript。

#show {
   padding: 20px;
   background: #ccc;
   max-height:0;
   overflow:hidden;
   -webkit-transition:max-height 1s;
   -moz-transition:max-height 1s;
   transition:max-height 1s;
}

#show:target {
    max-height:1000px; /* you can't transistion to height:auto */
}

http://jsfiddle.net/richbradshaw/hgdrw/1/

这里有几件事:

  1. :target 选择器
  2. 如果不使用JS取消该行为,页面将跳转到该ID
  3. 关闭它的唯一方法是更改​​ URL,使其末尾不包含 #show

如果我这样做,我会使用我在这里拥有的,但不是目标选择器,而是使用 JS 向#show 添加一个名为“active”的类,然后更改#show:target#show.active.

http://jsfiddle.net/richbradshaw/hgdrw/2/

我在这里使用 jQuery,因为它非常简单,

根据我的经验,目标选择器在实践中并不是很好用。

于 2013-06-02T12:41:50.873 回答
0

通过使用顶部填充/边距,您可以获得与幻灯片过渡类似的感觉:

http://jsbin.com/bicoseroze/1/

于 2015-03-09T10:53:43.700 回答
0

如果有帮助,我已将 Rohit 的答案改编成导航标签内的手风琴菜单,每个 div 都有内容:请参阅此JSFiddle

div 的固定高度对于纯 CSS 版本是必需的,这是一个问题 - 你不能使用height: auto;.

于 2014-01-09T10:31:07.040 回答