html - CSS3 过渡不适用于显示属性

标签 html css css-transitions

<分区>

每当我悬停其父元素时,我一直在尝试使用 css 来显示隐藏的 Div 淡入。

到目前为止,我所能做的就是让隐藏的 div 显示出来,但是从来没有缓和过渡。

这是我在 JSfiddle 上的代码 http://jsfiddle.net/9dsGP/

这是我的代码:

HTML:

<div id="header">
<div id="button">This is a Button
    <div class="content">
    This is the Hidden Div
    </div>
</div>
</div>

CSS:

#header #button {width:200px; background:#eee}

#header #button:hover > .content {display:block; opacity:1;}

#header #button .content:hover { display:block;}

#header #button .content {

-webkit-transition: all .3s ease .15s;
-moz-transition: all .3s ease .15s;
-o-transition: all .3s ease .15s;
-ms-transition: all .3s ease .15s;
transition: all .3s ease .15s;

    opacity:0;
    clear: both;
    display: none;

    top: -1px;
    left:-160px;
    padding: 8px;
    min-height: 150px;
    border-top: 1px solid #EEEEEE;
    border-left: 1px solid #EEEEEE;
    border-right: 1px solid #EEEEEE;
    border-bottom: 1px solid #EEEEEE;
    -webkit-border-radius: 0px 7px 7px 7px;
    -moz-border-radius: 0px 7px 7px 7px;
    -khtml-border-radius: 0px 7px 7px 7px;
    border-radius: 0px 7px 7px 7px;
    -webkit-box-shadow: 0px 2px 2px #DDDDDD;
    -moz-box-shadow: 0px 2px 2px #DDDDDD;
    box-shadow: 0px 2px 2px #DDDDDD;
    background: #FFF;
}

关于我做错了什么的任何线索?当我将鼠标悬停在按钮上时,只是想为隐藏的内容获得平滑效果。提前致谢!

最佳答案

display:none; removes a block from the page as if it were never there. A block cannot be partially displayed; it’s either there or it’s not. The same is true for visibility; you can’t expect a block to be half hidden which, by definition, would be visible! Fortunately, you can use opacity for fading effects instead.
- reference

作为替代 CSS 解决方案,您可以使用 opacityheightpadding 属性来达到理想的效果:

#header #button:hover > .content {
    opacity:1;
    height: 150px;
    padding: 8px;    
}

#header #button .content {
    opacity:0;
    height: 0;
    padding: 0 8px;
    overflow: hidden;
    transition: all .3s ease .15s;
}

(为简洁起见省略了供应商前缀。)

这是一个 working demo 。这里还有a similar topic在 SO 上。

#header #button {
  width:200px;
  background:#ddd;
  transition: border-radius .3s ease .15s;
}

#header #button:hover, #header #button > .content {
    border-radius: 0px 0px 7px 7px;
}

#header #button:hover > .content {
  opacity: 1;
  height: 150px;
  padding: 8px;    
}

#header #button > .content {
  opacity:0;
  clear: both;
  height: 0;
  padding: 0 8px;
  overflow: hidden;

  -webkit-transition: all .3s ease .15s;
  -moz-transition: all .3s ease .15s;
  -o-transition: all .3s ease .15s;
  -ms-transition: all .3s ease .15s;
  transition: all .3s ease .15s;

  border: 1px solid #ddd;

  -webkit-box-shadow: 0px 2px 2px #ddd;
  -moz-box-shadow: 0px 2px 2px #ddd;
  box-shadow: 0px 2px 2px #ddd;
  background: #FFF;
}

#button > span { display: inline-block; padding: .5em 1em }
<div id="header">
  <div id="button"> <span>This is a Button</span>
    <div class="content">
      This is the Hidden Div
    </div>
  </div>
</div>

关于html - CSS3 过渡不适用于显示属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103006/

相关文章:

javascript - - 我正在尝试编写输入应为 abbbcc 且输出应为 a1b3c2 的代码

html - 如何用CSS在圆圈内显示价格?

javascript - 需要在视口(viewport)上加载 div

javascript - 元素宽度沿一个方向减小

下拉菜单的 CSS 转换不起作用

css - 将顶部设置为自动时触发 CSS 转换

javascript - 如何使用 Javascript 更改没有 ID 的 HTML 元素?

javascript - 如何使我的嵌套 ListView 可折叠和可展开

javascript - 如何创建编辑按钮在 CRUD 中正常工作?

CSS3 滑动图像