DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS3 + JS 互動頭像牆:滑鼠移過就翻轉的網格展示效果
編輯:CSS詳解     

這次要分享一個很有巧思的頭像展示效果:滑鼠移到哪一格,那一格就會滑出一塊色塊蓋住圖片,同時原本的人像往反方向位移;移開之後再退回原位。程式碼量不大,但因為切換方向會依滑鼠的移動路徑改變,互動起來意外地靈活。

這個範例的樣式部分其實還有很多發揮空間。舉例來說,滑鼠移入時不一定只能換成純色底加文字,也可以替換成同一個人物的其他表情或另一張照片,那會有趣得多。

HTML 結構

結構極簡:一個 ul.grid,要放幾張圖就寫幾個 li。li 裡固定是「一張圖 + 一層 .info」的組合。

<ul class="grid">
  <li>
    <a href="https://www.jiawin.com/creative-CSS3-avatar-display">
      <img src=/School/UploadFiles_7810/201603/20160324094804996.jpg" alt="" />
      <div class="info">
        <p>1</p>
      </div>
    </a>
  </li>
  <!-- 更多 li 依此類推 -->
</ul>

CSS 樣式

這段只是示範用的基礎設定,配色、字級、動畫參數都可以自由替換成你自己的風格。

body {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  margin: 0;
  padding: 0;
  background-color: #E6E6E6;
}

.grid {
  margin: 100px auto 80px auto;
  padding: 0;
  width: 100%;
  height: auto;
  overflow: hidden;
  max-width: 1000px;
  box-shadow: 0 40px 50px rgba(0, 0, 0, 0.3);
}

.grid li {
  width: 10%;
  background: #000;
  float: left;
  position: relative;
  overflow: hidden;
}

.grid img {
  float: left;
  width: 100%;
  height: auto;
  position: relative;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  transform: translate3d(0, 0, 0);
}

.grid .info {
  position: absolute;
  width: 100%;
  height: 100%;
  padding: 15px;
  background: #DC584C;
  display: none;
  z-index: 2;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  transform: translate3d(0, 0, 0);
}

.grid p {
  font-size: 22px;
  font-weight: bolder;
  color: #FFF;
}

這裡的 translate3d(0, 0, 0) 與 backface-visibility: hidden 是為了強迫瀏覽器把元素提升到獨立的合成層,讓後續的位移動畫跑得更順、避免文字閃爍。

JavaScript:判斷滑鼠從哪個方向來

真正決定效果「聰明不聰明」的是這一段。它把每格的索引拆成「行」與「列」,再和上一格的位置比較,如果是同一列往右走,內容就從左側滑入、圖片往右退;往左走則反之。上下同理。

(function() {
  $(function() {
    var columns, current, easing, grid, hideItem, showItem, time;

    grid = $(".grid");
    current = { index: -1, column: 0, line: 0 };
    columns = 10;
    easing = "cubic-bezier(0.165, 0.840, 0.440, 1.000)";
    time = 400;

    grid.on("mouseenter", "a", function(e) {
      var column, el, image, index, info, item, line;
      el = $(e.currentTarget);
      info = el.find(".info");
      image = el.find("img");
      index = el.parent().index();
      column = index % columns;
      line = Math.floor(index / columns);

      item = {
        el: el, index: index, column: column,
        line: line, info: info, image: image
      };

      if (current.el && current.index === index) return;

      if (line === current.line && column > current.column) {
        showItem(item, "-100%", 0, "25%", 0);
        hideItem(current, "100%", 0, "-25%", 0);
      } else if (line === current.line && column < current.column) {
        showItem(item, "100%", 0, "-25%", 0);
        hideItem(current, "-100%", 0, "25%", 0);
      } else if (line > current.line && column === current.column) {
        showItem(item, 0, "-100%", 0, "25%");
        hideItem(current, 0, "100%", 0, "-25%");
      } else {
        showItem(item, 0, "100%", 0, "-25%");
        hideItem(current, 0, "-100%", 0, "25%");
      }
      return current = item;
    });

    showItem = function(item, infoX, infoY, imageX, imageY) {
      item.info.stop().css({
        x: infoX, y: infoY, display: "block"
      }).transition({
        x: 0, y: 0, duration: time, easing: easing
      });
      return item.image.stop().transition({
        x: imageX, y: imageY, opacity: 0.5,
        duration: time, easing: easing
      });
    };

    hideItem = function(item, infoX, infoY, imageX, imageY) {
      if (!item.el) return;
      item.info.stop().transition({
        x: infoX, y: infoY, duration: time, easing: easing
      });
      return item.image.stop().css({
        x: imageX, y: imageY, opacity: 0.5
      }).transition({
        x: 0, y: 0, opacity: 1, duration: time, easing: easing
      });
    };
  });
}).call(this);

別忘了引入相依套件

照上面的步驟做完,如果畫面完全沒反應,通常就是漏了這一步——除了 jQuery,還需要 jQuery Transit:

<script src="https://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.transit/0.9.9/jquery.transit.min.js"></script>

jQuery Transit 是一個把 CSS3 的 transition 與 transform 包裝成 jQuery 語法的外掛。它幾乎和 .animate() 用法相同,因此遷移成本很低;同時多了 callback、自動補瀏覽器前綴、method chaining 等便利功能。正因為它呼叫的是真正的 CSS3 規則,所以它無法在過舊的瀏覽器上運作。

值得留意的是,columns = 10 這個變數必須和 CSS 裡 .grid li { width: 10% } 保持一致,否則行列判斷會失準,切換方向就會亂掉。這也是這類「JS 與 CSS 需協同」的動畫最容易被忽略的一環。

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