• swiper3 无缝滚动 + 鼠标悬停停止/继续


    html结构:

    1. <div class="peopleSwiper">
    2. <div class="swiper-container">
    3. <div class="swiper-wrapper">
    4. <div class="swiper-slide"><img src="images/people01.png">div>
    5. <div class="swiper-slide"><img src="images/people02.png">div>
    6. <div class="swiper-slide"><img src="images/people03.png">div>
    7. <div class="swiper-slide"><img src="images/people04.png">div>
    8. <div class="swiper-slide"><img src="images/people01.png">div>
    9. <div class="swiper-slide"><img src="images/people02.png">div>
    10. <div class="swiper-slide"><img src="images/people03.png">div>
    11. <div class="swiper-slide"><img src="images/people04.png">div>
    12. <div class="swiper-slide"><img src="images/people01.png">div>
    13. <div class="swiper-slide"><img src="images/people02.png">div>
    14. <div class="swiper-slide"><img src="images/people03.png">div>
    15. <div class="swiper-slide"><img src="images/people04.png">div>
    16. div>
    17. div>
    18. div>

     

    无缝滚动(修改css和autoplay属性)

    .swiper-container-free-mode > .swiper-wrapper{transition-timing-function:linear;}
    1. //初始化一个Swiper为peopleSwiper
    2. var peopleSwiper = new Swiper('.peopleSwiper .swiper-container', {
    3. slidesPerView: 8,
    4. paginationClickable: true,
    5. spaceBetween: 5,
    6. autoplay: true, //autoplay设置为true或者1
    7. speed: 1000,
    8. disableOnInteraction: true,
    9. loop:true,
    10. freeMode: true
    11. });

     

    鼠标悬停控制

    mySwiper.stopAutoplay() 和 mySwiper.startAutoplay()是控制autoplay属性,无缝滚动是speed属性,mySwiper.stopAutoplay() 和 mySwiper.startAutoplay()可以实现鼠标悬停暂停和继续的效果,但是需swiper执行完speed规定值后再执行暂停,如果需要鼠标悬停立刻暂停,移开后继续需调整mySwiper.stopAutoplay() 和 mySwiper.startAutoplay()控制

    swiper3 官方

    1. //鼠标覆盖停止自动切换
    2. mySwiper.container[0].onmouseover=function(){
    3. mySwiper.stopAutoplay();
    4. }
    5. //鼠标移出开始自动切换
    6. mySwiper.container[0].onmouseout=function(){
    7. mySwiper.startAutoplay();
    8. }
    9. /*
    10. 或者
    11. mouseenter / mouseleave 和 hover
    12. ".peopleSwiper"为html外层类名 peopleSwiper为初始化swiper名
    13. */
    14. $('.peopleSwiper').mouseenter(function() {
    15. peopleSwiper.stopAutoplay();
    16. }).mouseleave(function() {
    17. peopleSwiper.startAutoplay();
    18. })
    19. $('.peopleSwiper').hover(function() {
    20. peopleSwiper.stopAutoplay();
    21. },function(){
    22. peopleSwiper.startAutoplay();
    23. })

    mouseenter / mouseleave 或者 hover

    1. let speedNum = 3000 //swiper滑动速度
    2. let slideViewNum = 8 //swiper容器能够同时显示的slides数量
    3. let slideWidth = $(window).width()*0.9/slideViewNum //每个swiper-slide宽度 0.9是外层容器的宽度是屏幕90%
    4. let transformSlide = "" //鼠标悬停时的transform属性
    5. let moveTime = 0 //轮播图从暂停位置移动到原本应到的位置用时
    6. //初始化swiper
    7. var peopleSwiper = new Swiper('.peopleSwiper .swiper-container', {
    8. slidesPerView: slideViewNum,
    9. paginationClickable: true,
    10. spaceBetween: 5,
    11. autoplay: true,
    12. speed: speedNum,
    13. disableOnInteraction: true,
    14. loop:true,
    15. freeMode: true
    16. });
    17. //鼠标悬停停止离开继续
    18. $('.peopleSwiper').hover(function(){
    19. transformSlide = document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0].style.transform;
    20. //轮播图原本应移动到的位置
    21. let oldPosition = -1 *parseInt(document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0].style.transform.split("translate3d(")[1].split("px")[0]);
    22. //鼠标悬停时时轮播图位置
    23. let newPosition = -1 *parseInt(window.getComputedStyle(document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0],false)["transform"].split("1, ")[2].split(",")[0]);
    24. //鼠标悬停时轮播图的真实transform属性
    25. let nowTarnsform = window.getComputedStyle(document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0],false)["transform"];
    26. //修改轮播图从暂停位置移动到原本应到的位置用时
    27. moveTime = speedNum * ((oldPosition - newPosition) / slideWidth);
    28. //改变transform属性
    29. document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0].style.transform = nowTarnsform;
    30. peopleSwiper.stopAutoplay();
    31. },function(){
    32. document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0].style.transform = transformSlide;
    33. document.getElementsByClassName("swiper-container")[0].getElementsByClassName("swiper-wrapper")[0].style.transitionDuration = moveTime+"ms";
    34. peopleSwiper.startAutoplay();
    35. })

  • 相关阅读:
    断言语句 assertion
    Docker镜像创建的方法及dockerfile详解
    Flume配置4——自定义Source+Sink
    SkeyeVSS实现消防安全通道智能识别监控解决方案
    【面试:并发篇19:多线程:Park&Unpark】
    Spring FactoryBean 源码讲解
    feign 和 openFeign 的区别
    Centos7安装go解释器
    filebeat新filestream类型是否支持tail_files类似功能探究
    作为一名后端工程师,你知道Redis为什么这么快吗?
  • 原文地址:https://blog.csdn.net/millia/article/details/134004937