目錄
1. Structure the HTML
2. Style with Flexbox and Animation
3. Create Auto-Scrolling with CSS Animation
4. Make It Responsive
5. Optional: Pause on Hover
Tips for Better Results
首頁 web前端 前端問答 如何使用CSS創建響應性的推薦滑塊

如何使用CSS創建響應性的推薦滑塊

Aug 12, 2025 am 09:42 AM
css

使用純CSS創建響應式自動輪播的推薦語滑塊是可行的,只需結合HTML結構、Flexbox佈局和CSS動畫。 2. 首先構建包含多個推薦語項的語義化HTML容器,每個.item包含引用內容和作者信息。 3. 通過設置父容器display: flex、width: 300%(三張幻燈片)並應用overflow: hidden實現橫向排列。 4. 利用@keyframes定義從0%到-100%的translateX變換,配合animation: scroll 15s linear infinite實現無縫自動滾動。 5. 添加媒體查詢在768px和480px以下時調整字體、間距及延長動畫週期至20秒以提升小屏體驗。 6. 可選地通過:hover狀態設置animation-play-state: paused實現懸停暫停。 7. 為提高性能可添加will-change: transform,同時建議增加ARIA標籤以增強可訪問性。最終得到一個輕量、流暢且跨設備兼容的純CSS推薦語輪播組件。

How to create a responsive testimonial slider with CSS

Creating a responsive testimonial slider with CSS doesn't require JavaScript if you keep it simple—using CSS animations, flexbox, and media queries. Here's how you can build one that works well across devices.

How to create a responsive testimonial slider with CSS

1. Structure the HTML

Start with a clean and semantic HTML structure. Use a container for the slider and individual items for each testimonial.

 <div class="testimonial-slider">
  <div class="slide">
    <p class="quote">"This product changed the way I work. Highly recommended!"</p>
    <p class="author">— Jane Doe</p>
  </div>
  <div class="slide">
    <p class="quote">"Outstanding support and amazing quality. Worth every penny."</p>
    <p class="author">— John Smith</p>
  </div>
  <div class="slide">
    <p class="quote">"Simple, elegant, and effective. Exactly what I needed."</p>
    <p class="author">— Alex Brown</p>
  </div>
</div>

2. Style with Flexbox and Animation

Use CSS Flexbox to align the slides in a row and animate them to scroll automatically.

How to create a responsive testimonial slider with CSS
 .testimonial-slider {
  display: flex;
  width: 100%;
  overflow: hidden;
  padding: 20px 0;
  box-sizing: border-box;
}

.slide {
  flex: 0 0 auto;
  width: 100%;
  padding: 20px;
  text-align: center;
  background: #f9f9f9;
  border-radius: 8px;
  margin: 0 10px;
  opacity: 0.6;
  transform: scale(0.95);
  transition: all 0.3s ease;
}

.slide:hover {
  opacity: 1;
  transform: scale(1);
}

.slide p.quote {
  font-style: italic;
  margin-bottom: 10px;
  color: #333;
}

.slide p.author {
  font-weight: bold;
  color: #007acc;
}

3. Create Auto-Scrolling with CSS Animation

Make the slides scroll automatically using @keyframes and animation .

 .testimonial-slider {
  display: flex;
  width: 300%; /* Adjust based on number of slides */
  animation: scroll 15s linear infinite;
}

@keyframes scroll {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-100%);
  }
}

Note: If you have 3 slides and each takes 100% width, the total container is 300%. To scroll one full slide left, you translate by -100%.

How to create a responsive testimonial slider with CSS

4. Make It Responsive

Use media queries to adjust layout and animation timing for smaller screens.

 @media (max-width: 768px) {
  .slide {
    padding: 15px;
    font-size: 0.9em;
  }

  .slide p.quote {
    font-size: 0.95em;
  }
}

@media (max-width: 480px) {
  .slide {
    padding: 10px;
    margin: 0 5px;
  }

  .slide p.quote {
    font-size: 0.85em;
  }

  .slide p.author {
    font-size: 0.9em;
  }

  .testimonial-slider {
    animation: scroll 20s linear infinite;
  }
}

5. Optional: Pause on Hover

Improve UX by pausing the slider when users hover over it.

 .testimonial-slider:hover {
  animation-play-state: paused;
}

Tips for Better Results

  • Add more slides? Adjust the total width (eg, 400% for 4 slides) and update the keyframes ( translateX(-300%) ).
  • Want manual controls? That would require JavaScript, but for pure CSS, auto-play works great.
  • Smoothness: Use will-change: transform on .testimonial-slider for better performance.
  • Accessibility: Consider adding ARIA labels or a hidden list for screen readers.

Basically, with flexbox, keyframe animations, and responsive design, you can build a smooth, mobile-friendly testimonial slider using only HTML and CSS. It's lightweight, fast, and works across all modern browsers.

以上是如何使用CSS創建響應性的推薦滑塊的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1582
276
如何更改CSS中的列表樣式 如何更改CSS中的列表樣式 Aug 17, 2025 am 10:04 AM

要更改CSS列表樣式,首先使用list-style-type改變項目符號或編號樣式,1.使用list-style-type設置ul的項目符號為disc、circle或square,ol的編號為decimal、lower-alpha、upper-alpha、lower-roman或upper-roman,2.用list-style:none完全移除標記,3.使用list-style-image:url('bullet.png')替換為自定義圖像,4.通過list-style-position:in

如何在CSS中創建虛線邊框 如何在CSS中創建虛線邊框 Aug 15, 2025 am 04:56 AM

使用CSS創建點狀邊框只需設置border屬性為dotted即可,例如“border:3pxdotted#000”可為元素添加3像素寬的黑色點狀邊框,通過調整border-width可改變點的大小,較寬的邊框產生更大的點,且可單獨為某一邊設置點狀邊框如“border-top:2pxdottedred”,點狀邊框適用於div、input等塊級元素,常用於焦點狀態或可編輯區域以提升可訪問性,需注意顏色對比度,同時區別於dashed的短線樣式,dotted呈現圓形點狀,該特性在所有主流瀏覽器中均被廣泛

如何使用CSS創建響應性的推薦滑塊 如何使用CSS創建響應性的推薦滑塊 Aug 12, 2025 am 09:42 AM

使用純CSS創建響應式自動輪播的推薦語滑塊是可行的,只需結合HTML結構、Flexbox佈局和CSS動畫。 2.首先構建包含多個推薦語項的語義化HTML容器,每個.item包含引用內容和作者信息。 3.通過設置父容器display:flex、width:300%(三張幻燈片)並應用overflow:hidden實現橫向排列。 4.利用@keyframes定義從0%到-100%的translateX變換,配合animation:scroll15slinearinfinite實現無縫自動滾動。 5.添加媒體

如何更改CSS中的光標 如何更改CSS中的光標 Aug 16, 2025 am 05:00 AM

Usebuilt-incursortypeslikepointer,help,ornot-allowedtoprovideimmediatevisualfeedbackfordifferentinteractiveelements.2.ApplycustomcursorimageswiththecursorpropertyusingaURL,optionallyspecifyingahotspotandalwaysincludingafallbacklikeautoorpointer.3.Fol

如何使用CSS創建分屏佈局 如何使用CSS創建分屏佈局 Aug 11, 2025 pm 10:59 PM

使用CSS創建分屏佈局可通過Flexbox實現,首先將容器設為display:flex並設置高度為100vh,通過flex:1使子元素均分空間,水平佈局默認即可,垂直佈局需設置flex-direction:column,配合媒體查詢@media(max-width:768px)可實現移動端堆疊響應式效果,若需固定側邊欄則給其設置固定寬度,主內容區用flex:1自適應剩餘空間,同時建議全局使用box-sizing:border-box以避免padding影響佈局,最終實現簡潔且響應式的分屏設計。

CSS:nth-child()選擇器示例 CSS:nth-child()選擇器示例 Aug 11, 2025 pm 11:22 PM

:nth-child()用於根據元素在同級中的位置來選中並樣式化,常用於創建交替樣式或特定模式;1.使用even和odd關鍵字可實現隔行變色,如li:nth-child(even)設置偶數項背景為淺灰色;2.使用an b公式可精確控制選中規律,如3n 1選中第1、4、7等項;3.特殊模式如-n 3可選中前3個子元素;4.注意nth-child從1開始計數且計算所有同級元素,若需僅按同類元素計數應使用nth-of-type()。

HTML隱藏屬性和CSS顯示之間的區別:無 HTML隱藏屬性和CSS顯示之間的區別:無 Aug 12, 2025 am 02:08 AM

hIdentattributeanddisplay:nonthnobothhideelementsbutdifferinsemantics,行為和usecases.1.theDhideTributeIsaseSemantIsAsemantIsAsemantIsAsemantIsAsemantIsemantIseMantIsemanthathideseletselementsElementselementselementselementselementselements andisoverridendbybbycssunlesssunlessspificityor!

如何將CSS梯度用於背景 如何將CSS梯度用於背景 Aug 17, 2025 am 08:39 AM

CSSgradientsprovidesmoothcolortransitionswithoutimages.1.Lineargradientstransitioncolorsalongastraightlineusingdirectionsliketobottomorangleslike45deg,andsupportmultiplecolorstopsforcomplexeffects.2.Radialgradientsradiatefromacentralpointusingcircleo

See all articles