You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

轮播组件激活项居左问题求助:需实现居中展示+层级缩放

解决Swiper轮播激活项居中+层级缩放问题

问题分析

当前轮播切换功能正常,但激活的幻灯片始终左对齐,需要实现激活项居中显示,同时非激活项根据与中心的距离依次缩小的效果。

解决方案

1. 调整Swiper核心配置(关键)

Elementor集成的Swiper默认左对齐,需启用centeredSlides参数让激活项居中,同时设置slidesPerView: 'auto'让幻灯片自适应宽度。在现有JS代码中添加以下配置修改逻辑:

$(document).ready(function(){
    $('.as-slider').each(function(){
        var $this = $(this);
        // 等待Swiper初始化完成后修改参数
        var swiperCheck = setInterval(function(){
            var swiperInstance = $this.find('.as-side-slider .swiper-container')[0]?.swiper;
            if(swiperInstance){
                swiperInstance.params.centeredSlides = true;
                swiperInstance.params.slidesPerView = 'auto';
                swiperInstance.params.spaceBetween = 20; // 可按需调整幻灯片间距
                swiperInstance.update(); // 更新Swiper配置
                clearInterval(swiperCheck);
            }
        }, 100);
        // ... 保留你的其他JS代码
    });
});

2. 修改CSS实现层级缩放效果

更新现有CSS,给非激活幻灯片添加缩放样式,确保激活项保持最大尺寸:

selector{
    --radius: 8px;
    --height: 320px;
    --active-height: 410px;
    --overlay: 0.75;
    --scale-factor: 0.8; /* 非激活项基础缩放比例 */
}
selector{
    opacity: 0;
    transform: translateX(100px);
    transition: all 0.8s ease-in-out;
}
selector.loaded{
    opacity: 1;
    transform: translateX(0);
}

/* 调整swiper-wrapper布局,配合centeredSlides */
selector .swiper-wrapper{
    height: var(--active-height);
    align-items: center;
    justify-content: center;
}
selector:not(.loaded) .swiper-wrapper{
    transition-duration: 0s !important;
}
selector .swiper-slide{
    display: flex;
    align-items: flex-end;
    border-radius: var(--radius);
    height: var(--height);
    box-shadow: 0 0 50px rgba(0,0,0,0.15);
    transform: scale(var(--scale-factor));
    transition: all 0.3s ease-in-out 0.2s;
    opacity: 0.7;
}
selector.loaded .swiper-slide{
    transition: all 0.3s ease-in-out 0.2s;
}
/* 激活项恢复原尺寸和层级 */
selector .swiper-slide.swiper-slide-active{
    height: var(--active-height);
    transform: scale(1);
    opacity: 1;
    z-index: 10;
}
/* 相邻幻灯片稍大缩放,增强层级感 */
selector .swiper-slide.swiper-slide-next,
selector .swiper-slide.swiper-slide-prev{
    transform: scale(calc(var(--scale-factor) + 0.1));
    opacity: 0.85;
}

/* 保留原有CSS样式 */
selector .swiper-slide:before{
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    background: rgb(0,0,0);
    background: linear-gradient(20deg, rgba(0,0,0,var(--overlay)) 0%, rgba(0,0,0,0) 100%);
    height: 100%;
    width: 100%;
    z-index: 1;
}
selector .elementor-testimonial__footer{
    display: block;
}
selector img{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: var(--radius);
}
selector .elementor-testimonial__cite{
    z-index: 2;
    position: relative;
}
selector .elementor-testimonial__name{
    margin-bottom: 5px;
}
selector .swiper-pagination,
selector .elementor-swiper-button{
    display: none;
}
selector .swiper-container{
    overflow: visible; /* 桌面端允许非激活项部分显示在容器外 */
    margin-left: auto;
    margin-right: auto;
    width: 100%;
}

@media (max-width: 1024px){
selector{
    --height: 180px;
    --active-height: 250px;
}
}
@media (max-width: 767px){
selector{
    --height: 80px;
    --active-height: 105px;
    width: 100% !important;
    max-width: var(--container-widget-width, 300px) !important;
    --scale-factor: 0.9; /* 移动端调整缩放比例 */
}
selector .elementor-testimonial__cite{
    opacity: 0;
}
/* 移动端恢复容器溢出隐藏 */
selector .swiper-container{
    overflow: hidden;
}
}

关键修改说明

  • Swiper配置:centeredSlides: true是激活项居中的核心,slidesPerView: 'auto'让幻灯片自适应宽度,spaceBetween控制间距避免拥挤。
  • CSS缩放:通过自定义属性统一控制缩放比例,激活项设为原尺寸,相邻项稍大,其他项默认缩放,配合透明度强化层级差异。
  • 容器溢出:桌面端设overflow: visible让非激活项部分外露,增强视觉延伸感;移动端改回hidden避免布局错乱。

内容的提问来源于stack exchange,提问作者Drip Kartel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:44:54