我嘗試使用 Bootstrap5 創(chuàng)建如下屏幕所示的滑塊/輪播產(chǎn)品:
在這里我粘貼,這是我當(dāng)前的代碼:
<style> <!-- Temporary --> .carousel-control-next-icon { background: black; } </style>
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap 5 Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <!-- Home Section End --> <div class="container-fluid mt-3"> <div class="row g-4"> <div class="col-xl-6 col-md-4 ratio_medium d-sm-block d-none"> <div class="home-section bg-white div-content" style="height:236px;"> <!-- With Captions --> <div id="carouselExampleCaption" class="carousel slide" data-bs-ride="carousel"> <div class="carousel-inner" role="listbox"> <div class="carousel-item"> <img src="https://img.freepik.com/free-photo/elegant-watch-with-silver-golden-chain-isolated_181624-27080.jpg?w=740&t=st=1674127568~exp=1674128168~hmac=5ce04dff715884c11a1cddc3e0a50b7c625fedf703c03ed40824dd972da0f012" style="width:20%;" alt="" class="d-block img-fluid mx-auto"> </div> <div class="carousel-item active"> <img src="https://img.freepik.com/free-photo/elegant-watch-with-silver-golden-chain-isolated_181624-27080.jpg?w=740&t=st=1674127568~exp=1674128168~hmac=5ce04dff715884c11a1cddc3e0a50b7c625fedf703c03ed40824dd972da0f012" style="width:20%;" alt="" class="d-block img-fluid mx-auto"> </div> <div class="carousel-item"> <img src="https://img.freepik.com/free-photo/elegant-watch-with-silver-golden-chain-isolated_181624-27080.jpg?w=740&t=st=1674127568~exp=1674128168~hmac=5ce04dff715884c11a1cddc3e0a50b7c625fedf703c03ed40824dd972da0f012" style="width:20%;" alt="" class="d-block img-fluid mx-auto"> </div> </div> <a class="carousel-control-prev" href="#carouselExampleCaption" role="button" data-bs-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExampleCaption" role="button" data-bs-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> </div> </div> <div class="col-xl-6 ratio_65 d-xl-block d-none"> <div class="home-section bg-white div-content" style="height:236px;"> <!-- With Captions --> <div id="carouselExampleCaption2" class="carousel slide" data-bs-ride="carousel"> <div class="carousel-inner" role="listbox"> <div class="carousel-item"> <img src="https://img.freepik.com/free-photo/elegant-watch-with-silver-golden-chain-isolated_181624-27080.jpg?w=740&t=st=1674127568~exp=1674128168~hmac=5ce04dff715884c11a1cddc3e0a50b7c625fedf703c03ed40824dd972da0f012" style="width:20%;" alt="" class="d-block img-fluid mx-auto"> </div> <div class="carousel-item active"> <img src="https://img.freepik.com/free-photo/elegant-watch-with-silver-golden-chain-isolated_181624-27080.jpg?w=740&t=st=1674127568~exp=1674128168~hmac=5ce04dff715884c11a1cddc3e0a50b7c625fedf703c03ed40824dd972da0f012" style="width:20%;" alt="" class="d-block img-fluid mx-auto"> </div> <div class="carousel-item"> <img src="https://img.freepik.com/free-photo/elegant-watch-with-silver-golden-chain-isolated_181624-27080.jpg?w=740&t=st=1674127568~exp=1674128168~hmac=5ce04dff715884c11a1cddc3e0a50b7c625fedf703c03ed40824dd972da0f012" style="width:20%;" alt="" class="d-block img-fluid mx-auto"> </div> </div> <a class="carousel-control-prev" href="#carouselExampleCaption2" role="button" data-bs-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExampleCaption2" role="button" data-bs-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> </div> </div> </div> </div> <!-- Home Section End -->
但目前這看起來(lái)很糟糕:
只顯示一項(xiàng),但我需要像這樣顯示三項(xiàng)半。我還嘗試在照片價(jià)格和產(chǎn)品標(biāo)題下添加,并且圖像必須具有響應(yīng)性。誰(shuí)能幫我糾正這個(gè)代碼嗎?
如果您要搜索此內(nèi)容,您可以找到此Codepen和此 Codeply。
將兩者結(jié)合起來(lái)可以為您提供一個(gè)工作示例(Codepen)。
JS 克隆每個(gè) .carousel-item
的內(nèi)容,因此每個(gè) .carousel-item
將保存其 (5) 個(gè)同級(jí)的內(nèi)容。 CSS 更改變換
,以便滑塊相應(yīng)移動(dòng)。
它還具有響應(yīng)能力,僅在最小斷點(diǎn)之前顯示一項(xiàng),但這可以輕松調(diào)整(僅限 CSS)。
這是一個(gè)片段,使用“全頁(yè)”鏈接全屏查看它:
注意:必須在 CSS 中添加一堆 !important
,如果您在 Bootstrap 之后加載 CSS,則不需要,例如 Codepen。
let items = document.querySelectorAll('.carousel .carousel-item') items.forEach((el) => { const minPerSlide = 5 let next = el.nextElementSibling for (var i = 1; i.carousel-item .col { padding: 0 5px; } @media (max-width: 767px) { .carousel-inner .carousel-item>div { display: none !important; } .carousel-inner .carousel-item>div:first-child { display: block !important; } } .carousel-inner .carousel-item.active, .carousel-inner .carousel-item-next, .carousel-inner .carousel-item-prev { display: flex !important; } /* medium and up screens */ @media (min-width: 768px) { .carousel-inner .carousel-item-end.active, .carousel-inner .carousel-item-next { transform: translateX(20%) !important; } .carousel-inner .carousel-item-start.active, .carousel-inner .carousel-item-prev { transform: translateX(-20%) !important; } /* half slides CSS */ .carousel-inner:before { position: absolute; top: 0; bottom: 0; right: 85%; left: 0; content: ""; display: block; background-color: #fff; z-index: 2; } .carousel-inner:after { position: absolute; top: 0; bottom: 0; right: 0; left: 85%; content: ""; display: block; background-color: #fff; z-index: 2; } .carousel-control-prev { left: 12% !important; } .carousel-control-next { right: 12% !important; } } .carousel-inner .carousel-item-end, .carousel-inner .carousel-item-start { transform: translateX(0) !important; }sssccc![]()
Card title
This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.
![]()
Card title
This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.
![]()
Card title
This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.
![]()
Card title
This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.
![]()
Card title
This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.
![]()
Card title
This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.