最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide swiper js navigation buttons(left and right) when no more images are present in the particular direction - Sta

programmeradmin5浏览0评论

How can I hide swiper js navigation buttons(left and right) when no more images are present in the particular direction

Eg. When there are no images in the right direction then the right navigation button should get hidden.

How can I hide swiper js navigation buttons(left and right) when no more images are present in the particular direction

Eg. When there are no images in the right direction then the right navigation button should get hidden.

Share Improve this question asked Jan 12, 2021 at 15:04 Ajinkya BodadeAjinkya Bodade 5711 gold badge5 silver badges11 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

I found a simple solution using CSS

.swiper-button-disabled{
    display:none;   
}

This will automatically hide the navigation button of that direction when no images are present in that direction.

If you want to hide it with a fading effect you can simply do this:

.swiper-button-next, .swiper-button-prev {
  opacity: 1;
  transition: 0.5s ease-in-out;
}
    
.swiper-button-disabled {
      visibility: hidden;
      opacity: 0;
      transition: 0.5s ease-in-out;
}

Using jQuery, you could do something like:

if (swiper.activeIndex===0) {
  $('.left-slide').hide()
  $('.right-slide').show()
} else if (swiper.activeIndex === swiper.slides.length-1) {
  $('.left-slide').show()
  $('.right-slide').hide()
}

First condition is the first slide

As stated in previous answers you can do it by adding styles to .swiper-button-disabled default class

&.leftIcon, &.rightIcon, &.topIcon, &.bottomIcon{
 opacity: 1;
 visibility: visible;
 transition: all 0.4s ease;
 &.swiper-button-disabled {
  opacity: 0;
  visibility: hidden;
 }
}

Using Swiper in React

if you want to do additional stuff like hide fading effect, you can use onActiveIndexChange prop. The e param has the isBeginning and isEnd boolean values that return true if you're in the beginning or end of the carousel.

const [disableFading, setDisableFading] = React.useState<'isBeginning'|'isEnd'|null>('isBeginning');

 <Swiper
  className={`swiperComponent ${disableFading === "isEnd" ? 'disabledFading' : ''}`}
  onActiveIndexChange={(e) => {
   if(e.isBeginning){
     setDisableFading("isBeginning");
   } else if(e.isEnd){
     setDisableFading("isEnd");
   } else {
     setDisableFading(null)
   }
  }}
/>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论