几种简单的"元素“消失的方法

2018-02-06  本文已影响0人  戴西西的染坊

几种元素or背景“消失”的方法

  1. 文档流中依然存在
    • opacity:元素的透明程度当值为0 时透明,位置依然“存在”,不具有继承性,但是在子元素未指定opacity时和父元素透明度相同。
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    .box1 {
      background:yellow;
      height:100px;
      border:5px solid black;
      opacity:0.2
    }
    .box2 {
      background:yellow;
      height:200px;
      opacity:0.6;
      border:5px solid blue;
    }
    .box3 {
      border:5px solid red;
      height:100px;
      opacity
    }
  </style>
</head>
<body>
    <div class="box1">
      ccc
    </div>
    <div class="box2">
      aaa
      <div class="box3">
          bbb
      </div>
    </div>
</body>
</html>
opacity
.box {
    background:rgba(255,0,0,0); /* 透明*/
    
    background:rgba(255,0,0,1);/* 红色*/
    
    background:rgba(0,0,0,0); 
    
    background:transparent;
}

2.在文档流中消失

transition (简单用法)

transition 是属性的简写 ( MDN中介绍: transition-property, transition-duration, transition-timing-function, 和 transition-delay
只对block 元素生效,作用是为一个元素不同情形下实现不同效果产生“过渡”效果。

visibility 和 transition 实现过渡

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
  </style>
</head>
<body>
  <div class="nav">
    <ul class="father clearfix">
      <li><a href="#">猜你喜欢</a></li>
      <li><a href="#">歌单</a></li>
      <li><a href="#">排行榜</a></li>
      <li><a href="#">电台</a></li>
      <li><a href="#">更多</a>
         <ul class="child">
           <li><a href="#">本地歌曲</a></li>
           <li><a href="#">最近播放</a></li>
           <li><a href="#">我的收藏</a></li>
         </ul>
        
     </ul>
  </div>
</body>
</html>
/* css部分 */

* {
  margin:0;
  padding:0;
} 

ul {
  list-style:none;
}
a {
  text-decoration:none;
  color:#000;
}
.clearfix:after {
  content:'';
  display:block;
  clear:both;
}
.nav {
  width:950px;

  margin:10px auto;
}

.father>li {
  float:left;
  position:relative;
}
.father>li a {
  padding-left:18px;
  padding-right:18px;
  border:1px solid #969696;
  border-radius:3px;
}

.father>li a:hover {
  background-color:#eee;
}

.father .child {
  width:160px;
  position:absolute;
  opacity:0;
  transition:opacity .6s;
  visibility:hidden;
}
.father>li:hover .child{
  visibility:visible;
  opacity:1;
}
效果图

参考资料 :

  1. MDN transition 过渡
  2. CSS MDN
  3. CSS世界
上一篇 下一篇

猜你喜欢

热点阅读