如何在Bootstrap中設計半頁輪播圖?


輪播圖是一種流行的網頁設計技術,被網頁開發者廣泛使用。這是一種以互動方式顯示一系列影像或其他媒體內容的方法。這些輪播圖是各種網站(如旅遊網站、教育網站等)的重要設計元素,我們需要利用影像來吸引使用者。半頁輪播圖略有不同,它只佔據整個頁面的二分之一。

使用網格系統

Bootstrap網格系統允許我們建立響應式且視覺上吸引人的影像佈局。我們可以使用網格系統放置任何型別的容器,包括文字、影像、段落等。在Bootstrap中,我們需要使用“row”和“col”類名來定義行和列以使用網格系統。使用CSS樣式,我們可以將輪播圖的高度設定為螢幕高度的一半和螢幕寬度的100%。

示例

以下程式碼是使用流行的前端框架Bootstrap建立半頁輪播圖的示例。該輪播圖包含三張圖片,每張圖片佔據螢幕高度的一半。程式碼使用了Bootstrap提供的“carousel”元件,幷包含三個“carousel-item”元素,每個元素都包含一個來自“picsum.photos”網站的圖片URL。圖片使用“d-block”和“w-100”類分別實現響應式設計,使圖片顯示為塊元素並將寬度設定為其父容器的100%。 “container-fluid”類建立了一個全寬容器,“container”類為標題和段落文字建立了一個居中的容器。最後,程式碼包含三個指令碼標籤,用於匯入輪播圖正常執行所需的JavaScript庫。

<!DOCTYPE html>
<html>
<head>
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
   <div class="container-fluid px-0">
      <div id="carouselExampleSlidesOnly" class="carousel slide" dataride="carousel">
         <div class="carousel-inner">
            <div class="carousel-item active">
               <img class="d-block w-100" src="https://picsum.photos/800/400?random=1" alt="First slide" style="height: 50vh !important;">
         </div>
         <div class="carousel-item">
            <img class="d-block w-100" src="https://picsum.photos/800/400?random=2" alt="Second slide"style="height: 50vh !important;">
         </div>
         <div class="carousel-item">
            <img class="d-block w-100" src="https://picsum.photos/800/400?random=3" alt="Third slide" style="height: 50vh !important;">
            </div>
         </div>
      </div>
   </div>
   
   <div class="container my-5">
      <h1 class="text-center">Half-Page Carousel Example</h1>
      <p class="text-center">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
   </div>
   
   <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js "></script>
</body>
</html>

使用模態框建立半頁輪播圖

模態框是現代網站中重要的設計元素。它們是顯示在當前頁面之上的對話方塊或彈出視窗。通常,我們會在其中顯示一些附加資訊、提示並提示使用者輸入。要使用模態框建立半頁輪播圖,我們可以相應地設定模態框的尺寸。

示例程式碼

我們在下面的程式碼中使用模態框實現了半頁輪播圖。我們使用了兩個影像。按鈕觸發模態框。觸發後,我們會逐一顯示影像。使用者可以透過提供的遊標瀏覽影像。

<!DOCTYPE html>
<html>
<head>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
   <style>
      .modal-dialog {
         max-width: 100vw;
         height: 50vh;
         margin: 0;
   }
   
   .modal-content {
      width: 100%;
      height: 100%;
   }
   
   .carousel-item img {
      object-fit: cover;
      width: 100%;
      height: 50Vh !important;
   }
   </style>
   <script>
      window.addEventListener('DOMContentLoaded', function() {
         var modal = document.getElementById('carouselModal');
         modal.addEventListener('show.bs.modal', function() {
            var windowHeight = window.innerHeight || document.documentElement.clientHeight;
            var modalHeight = windowHeight * 0.5; // Set modal height to 50% ofthe window height
            var modalDialog = modal.querySelector('.modal-dialog');
            modalDialog.style.height = modalHeight + 'px';
         });
      });
   </script>
</head>
<body>
   <div class="container">
      <div class="row">
         <div class="col-md-6">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#carouselModal">
               Open Carousel
            </button>
         </div>
      </div>
   </div>

   <div class="modal fade" id="carouselModal" tabindex="-1" role="dialog" aria-labelledby="carouselModalLabel" aria-hidden="true">
   <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
         <div id="carouselExampleControls" class="carousel slide" dataride=" carousel">
            <div class="carousel-inner">
            <div class="carousel-item active">
               <img src="https://lh3.googleusercontent.com/p/AF1QipOwuHy1QZ8xoEEm09pfzalu3h-JoyMti5MrordD=w1080-h608-p-k-no-v0" alt="Image 1">
               </div>
               <div class="carousel-item">
               <img src="https://static.ambitionbox.com/api/v2/photo/M2RLbTIvbklJQzh3Wk5pakZobWpHUT09" alt="Image 2">
               </div>
            </div>
            <a class="carousel-control-prev" href="#carouselExampleControls"role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" ariahidden="true"></span>
            <span class="sr-only">Previous</span>
         </a>
         <a class="carousel-control-next" href="#carouselExampleControls"role="button" data-slide="next">
            <span class="carousel-control-next-icon" ariahidden="true"></span>
               <span class="sr-only">Next</span>
            </a>
         </div>
      </div>
   </div>
</div>

   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>

結論

設計輪播圖是使網站看起來更具吸引力的好方法。這在部落格應用、旅遊網站等高度依賴影像來吸引使用者的網站中廣泛使用。但是,我們也應該記住,當我們新增大型影像時,網頁載入時間會更長。

更新於:2023年7月28日

458 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.