gpt4 book ai didi

javascript - 容器外的滑动导航按钮

转载 作者:搜寻专家 更新时间:2023-10-31 23:18:42 29 4
gpt4 key购买 nike

我试图在 swiper 容器外显示导航按钮,因为容器溢出:隐藏我必须创建一个带有 position: relative 的包装并绝对定位按钮。

那行得通,问题是现在导航按钮控制所有 slider ,我想不出解决这个问题的方法。

如果 slider 相同但内容不同,我不想用不同的类初始化多个 slider 。

JSFiddle

var sliderProdutosDestaque = new Swiper('.slider-produtos-destaque.swiper-container', {
slidesPerView: 2,
spaceBetween: 10,
navigation: {
nextEl: '.swiper-next',
prevEl: '.swiper-prev',
}
});
body {
padding: 20px 60px 40px;
}

.slider-produtos-wrap {
position: relative;
width: 100%;
margin-top: 20px;
}

.swiper-slide {
z-index: 1;
height: 150px;
background: blue;
}

.swiper-prev,
.swiper-next {
width: 60px;
height: 60px;
background: red;
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 60px;
z-index: 9999;
}

.swiper-prev {
left: -30px;
}

.swiper-next {
right: -30px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.1.0/css/swiper.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.1.0/js/swiper.min.js"></script>
<div class="slider-produtos-wrap">
<div class="slider-produtos-destaque swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>

<div class="swiper-prev"></div>
<div class="swiper-next"></div>

</div>

<div class="slider-produtos-wrap">
<div class="slider-produtos-destaque swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>

<div class="swiper-prev"></div>
<div class="swiper-next"></div>

</div>

最佳答案

简单、小巧、整洁。

document.querySelectorAll('.swiper-container').forEach(function(elem) {
new Swiper(elem, {
slidesPerView: 2,
spaceBetween: 10,
navigation: {
nextEl: elem.nextElementSibling.nextElementSibling,
prevEl: elem.nextElementSibling,
}
});
});
body {
padding: 20px 60px 40px;
}

.slider-produtos-wrap {
position: relative;
width: 100%;
margin-top: 20px;
}

.swiper-slide {
z-index: 1;
height: 150px;
background: blue;
}

.swiper-prev,
.swiper-next {
width: 60px;
height: 60px;
background: red;
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 60px;
z-index: 9999;
}

.swiper-prev {
left: -30px;
}

.swiper-next {
right: -30px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.1.0/css/swiper.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.1.0/js/swiper.min.js"></script>
<div class="slider-produtos-wrap">
<div class="slider-produtos-destaque swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>

<div class="swiper-prev"></div>
<div class="swiper-next"></div>

</div>

<div class="slider-produtos-wrap">
<div class="slider-produtos-destaque swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>

<div class="swiper-prev"></div>
<div class="swiper-next"></div>

</div>

关于javascript - 容器外的滑动导航按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48384265/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com