gpt4 book ai didi

javascript - 在光滑的 slider 中仅禁用一次上一个箭头

转载 作者:行者123 更新时间:2023-12-01 16:36:35 25 4
gpt4 key购买 nike

我编写了一个 javascript 函数,当当前幻灯片为 0 时禁用“上一个”箭头,当我到达最后一张幻灯片时禁用“下一个”箭头。我希望在页面加载时仅禁用一次“上一个”箭头,之后即使在到达第一张幻灯片(当前幻灯片:0)之后,我也希望“上一个”箭头可见。目前,我有 3 张幻灯片 (0,1,2)

我尝试使用 onload() 函数仅在第一次加载时隐藏箭头,但这不起作用。

$(function(){  
$('.hero-slider').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
dots:false,
infinite: true
});
var currentSlide = $('.hero-slider').slick('slickCurrentSlide');
if(currentSlide==0)
{
$('.slick-prev').hide();
}
else if(currentSlide==2)
{
$('.slick-next').hide();
}
});

$('.hero-slider').on('afterChange', function(){
console.log($('.hero-slider').slick('slickCurrentSlide'));
var currentSlide = $('.hero-slider').slick('slickCurrentSlide');
if(currentSlide==0)
{
$('.slick-prev').hide();
$('.slick-next').show();
}
else if(currentSlide==2)
{
$('.slick-next').hide();
$('.slick-prev').show();
}
if(currentSlide>0 && currentSlide<2)
{
$('.slick-prev').show();
$('.slick-next').show();
}
});

最佳答案

只保留 slider 初始化代码并删除事件处理程序和其他代码。

jQuery(function($) {  
$('.hero-slider').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
dots:false,
infinite: true
});
});

jQuery(function($) {  
$('.hero-slider').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
dots:false,
infinite: true
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.js"></script>
<section class="info-banner">
<div class="hero-slider">
<div class="info-slider-content" style="background-image: url(images/visual2.jpg);">
<div class="banner-content info-content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et </p>
</div>
</div>
<div class="info-slider-content" style="background-image: url(images/visual3.jpg);">
<div class="banner-content info-content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
</div>
</div>
<div class="info-slider-content" style="background-image: url(images/visual2.jpg);">
<div class="banner-content info-content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et </p>
</div>
</div>
</div>
</section>



更新:如果您想最初隐藏按钮,则在幻灯片更改后更新事件处理程序中的按钮一次(使用 one() 方法只执行一次)。
jQuery(function($) {
$('.hero-slider').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
dots: false,
infinite: true
});
var currentSlide = $('.hero-slider').slick('slickCurrentSlide');
$('.slick-prev').toggle(currentSlide != 0);
$('.slick-next').toggle(currentSlide != 2);

$('.hero-slider').one('afterChange', function() {
$('.slick-prev,.slick-next').show();
});
});

jQuery(function($) {
$('.hero-slider').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
dots: false,
infinite: true
});
var currentSlide = $('.hero-slider').slick('slickCurrentSlide');
$('.slick-prev').toggle(currentSlide != 0);
$('.slick-next').toggle(currentSlide != 2);

$('.hero-slider').one('afterChange', function() {
$('.slick-prev,.slick-next').show();
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.js"></script>
<section class="info-banner">
<div class="hero-slider">
<div class="info-slider-content" style="background-image: url(images/visual2.jpg);">
<div class="banner-content info-content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et</p>
</div>
</div>
<div class="info-slider-content" style="background-image: url(images/visual3.jpg);">
<div class="banner-content info-content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>
</div>
</div>
<div class="info-slider-content" style="background-image: url(images/visual2.jpg);">
<div class="banner-content info-content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et</p>
</div>
</div>
</div>
</section>

关于javascript - 在光滑的 slider 中仅禁用一次上一个箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41671215/

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