admin管理员组文章数量:1295939
I have a testimonials part on my website like this, when I have a total of three testimonials:
Now, if I add more testimonials, however, it becomes crowded like this:
Let one section be three testimonials, as shown in the first image. I want a total of three sections, with three testimonials per each section like I showed in the first image. It should look exactly like that. So, a total of 9 testimonials (3 sections, 3 testimonials per section).
Here's my code (with a total of 6 testimonials):
let currentTestimonialIndex = 0;
const testimonialsPerSlide = 3;
const totalTestimonials = 9; // Total testimonials
const totalSections = totalTestimonials / testimonialsPerSlide;
const slider = document.querySelector(".testimonial-slider");
function showTestimonial(index) {
let offset = -(index * 100); // Moves 100% per section
slider.style.transform = `translateX(${offset}%)`;
}
function prevTestimonial() {
currentTestimonialIndex = (currentTestimonialIndex === 0) ? totalSections - 1 : currentTestimonialIndex - 1;
showTestimonial(currentTestimonialIndex);
}
function nextTestimonial() {
currentTestimonialIndex = (currentTestimonialIndex === totalSections - 1) ? 0 : currentTestimonialIndex + 1;
showTestimonial(currentTestimonialIndex);
}
/* Testimonials Section */
.testimonials-container {
background-color: #E7C79A;
/* Soft beige background matching the website */
padding: 60px 50px;
text-align: center;
border-radius: 15px;
margin-top: 50px;
max-width: 90%;
margin-left: auto;
margin-right: auto;
position: relative;
overflow: hidden;
}
/* Section Title */
.testimonials-title {
color: rgba(50, 40, 30, 0.8);
letter-spacing: 1px;
margin-bottom: 5px;
}
.testimonials-heading {
font-weight: bold;
color: #6D4C41;
/* Brown color matching the website */
margin-bottom: 30px;
/* Moved testimonials 30px down */
}
/* Testimonials Wrapper */
.testimonial-wrapper {
display: flex;
justify-content: center;
/* Centers the testimonial */
align-items: center;
position: relative;
overflow: hidden;
width: 100%;
}
/* Hide testimonials except for the active one */
.testimonial-slider {
display: flex;
transition: transform 0.5s ease-in-out;
/* Smooth sliding effect */
width: 210%;
/* Ensures all testimonials are positioned in a row */
}
.testimonial {
flex: 0 0 100%;
/* Each testimonial takes full width */
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: left;
box-sizing: border-box;
flex-basis: 20%;
max-width: 800px;
}
/* Ensure only the active testimonial is shown */
.testimonial.active {
display: block;
}
/* Star Rating */
.star-rating {
color: #F4C150;
/* Gold stars */
font-size: 18px;
}
/* Testimonial Text */
.testimonial-text {
font-size: 16px;
color: rgba(50, 40, 30, 0.9);
line-height: 1.5;
}
/* Testimonial Author */
.testimonial-author {
font-size: 18px;
font-weight: bold;
color: #6D4C41;
/* Brown matching the website */
}
.testimonial-role {
font-size: 14px;
color: rgba(50, 40, 30, 0.7);
}
/* Navigation Buttons */
.testimonial-nav {
position: absolute;
top: 50%;
/* Centers buttons relative to testimonial */
transform: translateY(-50%);
background: white;
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
font-size: 18px;
color: #6D4C41;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.testimonial-nav:hover {
background: #6D4C41;
color: white;
}
.left-nav {
left: 10px;
}
.right-nav {
right: 10px;
}
<div class="testimonials-container">
<h2 class="testimonials-title">What Our Users Say</h2>
<h1 class="testimonials-heading">Trusted by Users & Consultants</h1>
<div class="testimonial-wrapper">
<button class="testimonial-nav left-nav" onclick="prevTestimonial()">❮</button>
<div class="testimonial-slider">
<div class="testimonial active">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"This consultation service made it easy to find the right expert. The process was seamless, and I got valuable insights!"
</div>
<div class="testimonial-author">James R.</div>
<div class="testimonial-role">Consultation User</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"The platform is intuitive and efficient. I booked a consult within minutes and got exactly the advice I needed!"
</div>
<div class="testimonial-author">Samantha L.</div>
<div class="testimonial-role">User - Business Strategy Consultation</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
</div>
<button class="testimonial-nav right-nav" onclick="nextTestimonial()">❯</button>
</div>
</div>
I have a testimonials part on my website like this, when I have a total of three testimonials:
Now, if I add more testimonials, however, it becomes crowded like this:
Let one section be three testimonials, as shown in the first image. I want a total of three sections, with three testimonials per each section like I showed in the first image. It should look exactly like that. So, a total of 9 testimonials (3 sections, 3 testimonials per section).
Here's my code (with a total of 6 testimonials):
let currentTestimonialIndex = 0;
const testimonialsPerSlide = 3;
const totalTestimonials = 9; // Total testimonials
const totalSections = totalTestimonials / testimonialsPerSlide;
const slider = document.querySelector(".testimonial-slider");
function showTestimonial(index) {
let offset = -(index * 100); // Moves 100% per section
slider.style.transform = `translateX(${offset}%)`;
}
function prevTestimonial() {
currentTestimonialIndex = (currentTestimonialIndex === 0) ? totalSections - 1 : currentTestimonialIndex - 1;
showTestimonial(currentTestimonialIndex);
}
function nextTestimonial() {
currentTestimonialIndex = (currentTestimonialIndex === totalSections - 1) ? 0 : currentTestimonialIndex + 1;
showTestimonial(currentTestimonialIndex);
}
/* Testimonials Section */
.testimonials-container {
background-color: #E7C79A;
/* Soft beige background matching the website */
padding: 60px 50px;
text-align: center;
border-radius: 15px;
margin-top: 50px;
max-width: 90%;
margin-left: auto;
margin-right: auto;
position: relative;
overflow: hidden;
}
/* Section Title */
.testimonials-title {
color: rgba(50, 40, 30, 0.8);
letter-spacing: 1px;
margin-bottom: 5px;
}
.testimonials-heading {
font-weight: bold;
color: #6D4C41;
/* Brown color matching the website */
margin-bottom: 30px;
/* Moved testimonials 30px down */
}
/* Testimonials Wrapper */
.testimonial-wrapper {
display: flex;
justify-content: center;
/* Centers the testimonial */
align-items: center;
position: relative;
overflow: hidden;
width: 100%;
}
/* Hide testimonials except for the active one */
.testimonial-slider {
display: flex;
transition: transform 0.5s ease-in-out;
/* Smooth sliding effect */
width: 210%;
/* Ensures all testimonials are positioned in a row */
}
.testimonial {
flex: 0 0 100%;
/* Each testimonial takes full width */
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: left;
box-sizing: border-box;
flex-basis: 20%;
max-width: 800px;
}
/* Ensure only the active testimonial is shown */
.testimonial.active {
display: block;
}
/* Star Rating */
.star-rating {
color: #F4C150;
/* Gold stars */
font-size: 18px;
}
/* Testimonial Text */
.testimonial-text {
font-size: 16px;
color: rgba(50, 40, 30, 0.9);
line-height: 1.5;
}
/* Testimonial Author */
.testimonial-author {
font-size: 18px;
font-weight: bold;
color: #6D4C41;
/* Brown matching the website */
}
.testimonial-role {
font-size: 14px;
color: rgba(50, 40, 30, 0.7);
}
/* Navigation Buttons */
.testimonial-nav {
position: absolute;
top: 50%;
/* Centers buttons relative to testimonial */
transform: translateY(-50%);
background: white;
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
font-size: 18px;
color: #6D4C41;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.testimonial-nav:hover {
background: #6D4C41;
color: white;
}
.left-nav {
left: 10px;
}
.right-nav {
right: 10px;
}
<div class="testimonials-container">
<h2 class="testimonials-title">What Our Users Say</h2>
<h1 class="testimonials-heading">Trusted by Users & Consultants</h1>
<div class="testimonial-wrapper">
<button class="testimonial-nav left-nav" onclick="prevTestimonial()">❮</button>
<div class="testimonial-slider">
<div class="testimonial active">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"This consultation service made it easy to find the right expert. The process was seamless, and I got valuable insights!"
</div>
<div class="testimonial-author">James R.</div>
<div class="testimonial-role">Consultation User</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"The platform is intuitive and efficient. I booked a consult within minutes and got exactly the advice I needed!"
</div>
<div class="testimonial-author">Samantha L.</div>
<div class="testimonial-role">User - Business Strategy Consultation</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
<div class="testimonial">
<div class="star-rating">★★★★★</div>
<div class="testimonial-text">
"As a consultant, I appreciate the seamless booking system. Clients can connect with me easily, and the interface is smooth!"
</div>
<div class="testimonial-author">Dr. Michael K.</div>
<div class="testimonial-role">Registered Consultant</div>
</div>
</div>
<button class="testimonial-nav right-nav" onclick="nextTestimonial()">❯</button>
</div>
</div>
Share
edited Feb 12 at 10:49
vsync
131k59 gold badges340 silver badges422 bronze badges
asked Feb 12 at 9:11
God GamerGod Gamer
311 silver badge6 bronze badges
2 Answers
Reset to default 1Below is a CSS-only solution for styling such a carousel component so if there are many items, they will overflow the scrollable container and will retain their gap between each other.
The flex gap is built dynamically so only 3 items fit, no matter what:
At the end it's just mathematics of gap-management.
Try resizing the below carousel to see it in action. All you have to do now is scroll to the next/previous 3rd item (scrollIntoView
) and detect edges so you could just to the other end of the carousel and an edge has reched.
.carousel {
--item-width: 100px;
--items-per-view: 3;
width: 477px;
border: 2px solid red;
position: relative;
overflow: hidden;
resize: horizontal;
> ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
gap: calc((100% - (var(--item-width) * var(--items-per-view))) / (var(--items-per-view) - 1));
justify-content: space-between;
> li {
height: 160px;
min-width: var(--item-width); /*
本文标签:
javascriptLimit viewable number of carousel items to 3 (at a time)Stack Overflow
版权声明:本文标题:javascript - Limit viewable number of carousel items to 3 (at a time) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1741613006a2388367.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论