DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> 關於CSS
CSS 懸浮遮罩層:從基礎到進階的兩種實作
編輯:關於CSS     

滑鼠移入圖片或卡片時,浮出一層半透明遮罩顯示說明文字,是作品集與商品列表最常見的互動之一。以下從最簡陋的版本開始,再進到可以量產的圖示版本。

版本一:由下往上推入

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>遮罩層</title>
<style type="text/css">
.mask-wrapper {
  position: relative;
  overflow: hidden;
}

.mask-inner {
  position: absolute;
  left: 0;
  top: 100%;                 /* 先藏在容器下方 */
  width: 100%;
  height: 100%;
  transition: top ease 200ms;
}

.mask-wrapper:hover .mask-inner {
  top: 0;                    /* 滑入後蓋滿容器 */
}

#my-mask {
  width: 300px;
  height: 200px;
  background: red;
}

#my-mask .mask-inner {
  background: rgba(0, 0, 0, .5);
}
</style>
</head>
<body>
<div id="my-mask" class="mask-wrapper">
  <p>Lorem ipsum</p>
  <div class="mask-inner">
    <p>foo bar</p>
  </div>
</div>
</body>
</html>

結構只有兩層:外層負責裁剪(overflow: hidden)與提供定位上下文,內層 positioning 到 top: 100% 藏在視野外,滑入時改回 top: 0。

我在這裡把原本散落的 -moz-、-o-、-webkit- 前綴移除了。這些是舊版 Firefox 與 Opera 的遺留物,現在只保留標準 transition 即可,樣式立刻清爽許多。

效果如下:

版本二:可量產的圓形圖示

第二個版本示範如何把同樣的思路套用到一列圓形圖示上,並額外鋪一層陰影圖增加立體感。

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>圖示遮罩</title>
<style type="text/css">
* { margin: 0; padding: 0; }

body { font: 12px/1.5 arial; }
ul { list-style: none; }

.icon-lists {
  overflow: hidden;
  background: #f7f7f7;
  padding: 40px;
}

.icon-lists .box {
  float: left;
  margin-right: 10px;
}

.box {
  position: relative;
  width: 46px;
  height: 46px;
  overflow: hidden;
  z-index: 1;
}

.box i, .box .info {
  display: block;
  width: 46px;
  height: 46px;
  border-radius: 46px;
}

/* 陰影圖鋪在最上層,營造浮起感 */
.box .shadow {
  position: absolute;
  top: 0;
  z-index: 10;
  border-radius: 0;
  background: url(shadow.png) no-repeat;
}

.box .icon {
  position: absolute;
  top: 0;
  line-height: 46px;
  text-align: center;
  background: #eee;
  color: #333;
  font-size: 14px;
}

/* 說明層先藏在下方 46px */
.box .info {
  position: absolute;
  top: 46px;
  z-index: 2;
  background: orange;
  color: #fff;
  text-align: center;
  line-height: 46px;
  transition: top .2s ease-in;
}

.box:hover > .info {
  top: 0;
}
</style>
</head>
<body>
<ul class="icon-lists">
  <li class="box">
    <i class="shadow"></i>
    <i class="icon">A</i>
    <div class="info">服裝</div>
  </li>
  <li class="box">
    <i class="shadow"></i>
    <i class="icon">B</i>
    <div class="info">鞋包</div>
  </li>
  <li class="box">
    <i class="shadow"></i>
    <i class="icon">C</i>
    <div class="info">配件</div>
  </li>
  <li class="box">
    <i class="shadow"></i>
    <i class="icon">D</i>
    <div class="info">運動</div>
  </li>
</ul>
</body>
</html>

效果如下:

兩個值得改進的地方

用 transform 取代 top。動畫 top 會觸發重排(reflow),改成 transform: translateY 只走合成階段,效能好得多:

.mask-inner {
  top: 0;
  transform: translateY(100%);
  transition: transform .2s ease;
}
.mask-wrapper:hover .mask-inner {
  transform: translateY(0);
}

讓觸控裝置也能看到內容。hover 在手机上不存在,若遮罩裡的文字是必要資訊(例如圖說、價格),請勿只依賴 hover——改為預設顯示,或搭配 @media (hover: hover) 只在真正支援 hover 的裝置上隱藏。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved