swiper实现点击和滑动切换导航

我与影子孤独终老i 提交于 2020-01-25 00:24:38

1.https://segmentfault.com/q/1010000008716893/a-1020000008717388

注意 经测版本不能超过4.0

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <title></title>
        <!--<link rel="stylesheet" type="text/css" href="swiper-3.3.1.min.css"/>-->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.0.2/css/swiper.css">
        <style type="text/css">
            .swiper-container{
                width: 100%;
                height: 300px;
            }
            .swiper-wrapper{
                height: 100%;
            }
            .swiper-slide{
                height: 100%;
            }
            *{
                margin: 0;
                padding: 0;
            }
            ul,li{
                list-style: none;
            }
            ul{
                width: 100%;
                overflow: hidden;
                position: relative;
            }
            li{
                line-height: 40px;
            }
            li{
              float: left;
              text-align: center;
              width: 33.3%;
              box-sizing: border-box;
            }
            li.on{
                background-color: greenyellow;
                color: #fff;
            }
            .more{
                background: transparent;
                height: 2px;
                position: absolute;
                left: 0;
                bottom: 0;
                width: 33.3%;
                transition: all .2s  linear;
            }
            .more span{
                background: blue;
                height: 2px;
                position: absolute;
                left: 50%;
                bottom: 50%;
                transform: translate(-50%,-50%);
                width: 22px;
            }
        </style>
        <!--<script type="text/javascript" src="swiper.min.js"></script>-->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.0.2/js/swiper.js"></script>
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    </head>
    <body>
        <ul>
            <li>首页</li>
            <li>我们</li>
            <li>联系</li>
            <div class="more">
                <span></span>
            </div>
        </ul>
        <div class="swiper-container">
          <div class="swiper-wrapper">
            <div class="swiper-slide" style="background-color: red;">首页内容1</div>
            <div class="swiper-slide" style="background-color: yellow;">首页内容2</div>
            <div class="swiper-slide" style="background-color: green;">首页内容3</div>
          </div>
        </div>
        <script> 
        $(function(){
            var mySwiper = new Swiper('.swiper-container', {
                onTransitionEnd:function(swiper){
//                  $('li').eq(mySwiper.activeIndex).addClass('on').siblings().removeClass('on');
                    $('.more').css('left',mySwiper.activeIndex*33.3+'%')    
                }
            })
            $('li').click(function(){
//              $(this).addClass('on').siblings().removeClass('on');
                $(this).parent().find('.more').css('left',$(this).index()*33.3+'%')    
                mySwiper.slideTo($(this).index(), 1000)
            });
        })        
        </script>
    </body>
</html>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!