当前位置:   article > 正文

React Swiper.js使用(详细版)3D聚焦特效,自定义导航按钮等_swiper/react delay

swiper/react delay

共用代码

  1. import 'swiper/css'
  2. import 'swiper/css/navigation'
  3. import 'swiper/css/pagination'
  4. import { Navigation, Pagination, Scrollbar, A11y, Autoplay, EffectCreative } from 'swiper/modules';
  5. import { Swiper, SwiperSlide, } from 'swiper/react';

普通版本

重点:  modules={[Navigation, Pagination]}   Swiper, SwiperSlide  没什么好说的

  1. <Swiper
  2. className="house"
  3. lazy={true}
  4. modules={[Navigation, Pagination]}
  5. spaceBetween={60}
  6. slidesPerView={3}
  7. watchSlidesProgress
  8. navigation={{
  9. nextEl: '.swiper-button-next',
  10. prevEl: '.swiper-button-prev',
  11. disabledClass: 'disable',
  12. }}
  13. pagination={{
  14. el: '.swiper-pagination-house',
  15. type: 'progressbar',
  16. }}
  17. >
  18. {data.map((item: any) => {
  19. return (
  20. <SwiperSlide key={item.id} style={{ width: 440 }}>
  21. <div className={styles.bannerContainer_box}>
  22. <img src={item.projectListPic} alt="暂无图片" width="360" height="360" loading="lazy" />
  23. <div className={styles.textContainer}>
  24. <p className={styles.title}>{item.projectName}</p>
  25. <p className={styles.detail}>{item.fullAddress}</p>
  26. </div>
  27. </div>
  28. </SwiperSlide>
  29. )
  30. })}
  31. </Swiper>

自定义导航按钮 / 进度条 + 解决同一页面,多个swiper.js 导航切换按钮冲突问题

这里的重点是: 

1、 通过直接引入less文件 重新定义 swiper-button-prev等类 或 :global ,自定义导航按钮 / 进度条样式 

2、  防止冲突,swiper2-button-prev swiper-button-prev 通过在前端定义前缀,避免多个swiper.js使用 navigation={{  nextEl: '.swiper-button-next',

  1. .swiper-pagination {
  2. bottom: 0px;
  3. top: unset;
  4. width: 120px;
  5. height: 11px;
  6. left: 50%;
  7. transform: translateX(-50%);
  8. z-index: 1000;
  9. color: antiquewhite;
  10. }
  11. .swiper-button-prev {
  12. position: absolute;
  13. left: 0px;
  14. top: 50%;
  15. width: 50px;
  16. height: 50px;
  17. border-radius: 50%;
  18. background: linear-gradient(131deg, rgba(33, 75, 148, 0.62) 0%, #001335 107%);
  19. &::after {
  20. display: none;
  21. }
  22. &.disable {
  23. background: #D8D8D8;
  24. }
  25. }
  26. .......

  1. <div className="swiper2-button-prev swiper-button-prev">
  2. <img src={leftArrow} alt="上一张" />
  3. </div>
  4. <Swiper
  5. navigation={{
  6. nextEl: '.swiper2-button-next',
  7. prevEl: '.swiper2-button-prev',
  8. disabledClass: 'disable',
  9. }}
  10. pagination={{
  11. el: '.swiper-pagination-house',
  12. type: 'progressbar',
  13. }}
  14. >
  15. .......
  16. </Swiper>
  17. <div className="swiper2-button-next swiper-button-next">
  18. <img src={rightArrow} alt="下一个" />
  19. </div>

3D聚焦特效版本

重点: 使用 creativeEffect= {{ ... }}   modules={[Autoplay, EffectCreative]}  effect={'creative'}

  1. <Swiper
  2. slidesPerView={3}
  3. loopedSlides={2}
  4. loop={true}
  5. watchSlidesProgress={true}
  6. centeredSlides={true}
  7. initialSlide={1}
  8. spaceBetween={-60}
  9. autoplay={{
  10. delay: 2500,
  11. disableOnInteraction: false,
  12. }}
  13. effect={'creative'}
  14. creativeEffect={{
  15. prev: {
  16. translate: [-396, 0, 0],
  17. scale: 0.9295,
  18. },
  19. next: {
  20. translate: [396, 0, 0],
  21. scale: 0.9295,
  22. },
  23. shadowPerProgress: true,
  24. }}
  25. modules={[Autoplay, EffectCreative]}
  26. >
  27. {mockModuleBannerData.map((item: any) => {
  28. return (
  29. <SwiperSlide key={item.id} style={{ width: 440 }}>
  30. <img src={item.img} alt="暂无图片" width="440" height="440" />
  31. </SwiperSlide>
  32. )
  33. })}
  34. </Swiper>

获取swiper的实例

  1. <Swiper
  2. className="swiperRe"
  3. lazy={true}
  4. modules={[Navigation, EffectCreative]}
  5. spaceBetween={32}
  6. slidesPerView={2}
  7. effect={'creative'}
  8. onProgress={onProgress}
  9. ......
  10. const onProgress = (swiper: any) => {
  11. // 原谅我的黑魔法,太过黑盒了,都不知道从哪里获取swiper的实例。
  12. // eslint-disable-next-line curly
  13. if (ref.current) return
  14. ref.current = swiper
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/133930
推荐阅读
相关标签
  

闽ICP备14008679号