DIV CSS 佈局教程網

用 jQuery 搭配 CSS 在圖片上疊加按鈕的實作技巧
編輯:JavaScript綜合知識     

在課程封面、產品主視覺這類場景裡,我們常需要把一個「試看」或「立即購買」的按鈕浮在圖片上方。本文整理了在圖片上放置按鈕的完整做法,並釐清一個關鍵觀念,分享給大家參考。

先搞懂定位的基礎:relative 與 absolute 的搭配

日常開發中,position:relative 通常是設定給 position:absolute 元素的父層使用的。當父層設為 relative、子層設為 absolute 時,子層便會以父層的邊界作為定位參考點。

如果反過來沒有幫父層加上 relative,那麼 absolute 元素就會逐層向上尋找第一個設定了定位屬性的祖先元素作為基準,一路找到 body 為止,結果往往不是我們想要的位置。這也是為什麼「父 relative、子 absolute」會是這類需求的標準寫法。

第一種寫法:把 CSS 直接一起塞進 style 屬性

這種做法的優點是不必另外維護樣式表,適合快速驗證或結構較單純的頁面。透過 jQuery 取得免費影片的網址後,直接把帶有完整內距樣式的錨點附加到容器裡:

var freeOne = "";
freeOne = $(".freePreviewOne").attr("data-url");
if (freeOne == null) {
    // 沒有免費影片
} else {
    $("#coursePicture").append(
        "<a class='hide-650 fade5' " +
        "style='top:94px;left:150px;position:absolute;z-index:100;" +
        "width:180px;height:60px;border:2px solid white;" +
        "display:block;color:white;text-decoration:none;" +
        "letter-spacing:1px;font-size:16px;line-height:20px;" +
        "text-align:center;padding-top:18px;" +
        "background-color:rgba(0,0,0,0.44);" +
        "-webkit-backface-visibility:hidden;-webkit-transition:all .3s ease-in-out;" +
        "-moz-transition:all .3s ease-in-out;-ms-transition:all .3s ease-in-out;" +
        "-o-transition:all .3s ease-in-out;border-radius:10px;'" +
        "href='#modal' data-toggle='modal' " +
        "data-url='" + freeOne + "'> " +
        "<span class='contactus' style='font-size:26px;'>試 看</span> " +
        "<span class='glyphicon glyphicon-play' style='font-size:20px;top:0;bottom:2px;'></span> " +
        "</a>"
    );
}

對應的頁面結構如下,重點是幫 #coursePicture 加上 position: relative;,讓它成為按鈕的定位基準:

<div class="es-row-wrap container-gap course-cover">
  <div class="row row-5-7 course-cover-heading">
    <div id="coursePicture" class="col-sm-5 picture hidden-xs" style="position: relative;">
      <#if course.coverImage?has_content>
        <img src="${course.coverImage}" class="img-responsive" width="436px" height="245px" />
      <#else>
        <img src="/resources/assets/img/default/course-large.png" class="img-responsive" />
      </#if>
    </div>
  </div>
</div>

第二種寫法:把樣式抽到 CSS 類別中

如果同一個按鈕會重複出現在多個地方,把樣式抽成一個類別會更好維護。先定義 .freePreviewPicture 這個類別,再讓 jQuery 只負責附加結構:

// 在課程圖片上放置按鈕
var freeOne = "";
freeOne = $(".freePreviewOne").attr("data-url");
if (freeOne == null) {
    // 沒有免費影片
} else {
    $("#coursePicture").append(
        "<a class='freePreviewPicture hide-650 fade5' " +
        "href='#modal' data-toggle='modal' " +
        "data-url='" + freeOne + "'> " +
        "<span class='contactus' style='font-size:26px;'>試 看</span> " +
        "<span class='glyphicon glyphicon-play' style='font-size:20px;top:0;bottom:2px;'></span> " +
        "</a>"
    );
}

樣式與頁面結構則像這樣:

<style>
.freePreviewPicture {
  top: 94px;
  left: 150px;
  position: absolute;
  z-index: 100;
  width: 180px;
  height: 60px;
  border: 2px solid white;
  display: block;
  color: white;
  text-decoration: none;
  letter-spacing: 1px;
  font-size: 16px;
  line-height: 20px;
  text-align: center;
  padding-top: 18px;
  background-color: rgba(0, 0, 0, 0.44);
  -webkit-backface-visibility: hidden;
  -webkit-transition: all .3s ease-in-out;
  -moz-transition: all .3s ease-in-out;
  -ms-transition: all .3s ease-in-out;
  -o-transition: all .3s ease-in-out;
  border-radius: 10px;
}
</style>

<div class="es-row-wrap container-gap course-cover">
  <div class="row row-5-7 course-cover-heading">
    <div id="coursePicture" class="col-sm-5 picture hidden-xs" style="position: relative;">
      <#if course.coverImage?has_content>
        <img src="${course.coverImage}" class="img-responsive" width="436px" height="245px" />
      <#else>
        <img src="/resources/assets/img/default/course-large.png" class="img-responsive" />
      </#if>
    </div>
  </div>
</div>

運行效果如下圖所示:

以上就是兩種在圖片上放置按鈕的做法。第一種適合快速上手,第二種則更利於樣式統一與後續維護,可依專案規模自由選擇。希望本文對大家的 jQuery 開發有所幫助。

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