学习笔记——swiper插件实现滑动导航栏并自适应

本文介绍了如何利用Swiper插件创建一个响应式的导航栏,根据屏幕宽度动态调整显示的导航项数量。在不同屏幕尺寸下,导航栏能优雅地展示内容,避免文本换行和布局拥挤。通过监听窗口大小变化,实时更新Swiper参数,确保最佳的用户体验。
摘要由CSDN通过智能技术生成

在做水平或垂直方向导航栏时,有时导航栏中导航项非常多,显示在同一行或列显得十分拥挤,并且文本可能会换行。而将导航栏显示成两行或者两列又十分难看。这时我们可以将导航栏的部分内容隐藏,并且根据文本宽度,一次只显示几个。

 


 1.代码:

        <div class="swiper-nav">
            <div class="page-tab swiper-container" id="swiper">
                <ul class="tab-wrapper swiper-wrapper">
                    <li class="tab-item swiper-slide">
                        <div class="tab-item-wrapper">
                            <span class="tab-name fontSize18">选项卡一</span>
                        </div>
                    </li>
                    <li class="tab-item swiper-slide">
                        <div class="tab-item-wrapper">
                            <span class="tab-name fontSize18">选项卡二</span>
                        </div>
                    </li>
                    <li class="tab-item swiper-slide">
                        <div class="tab-item-wrapper">
                            <span class="tab-name fontSize18">选项卡三</span>
                        </div>
                    </li>
                    <li class="tab-item swiper-slide">
                        <div class="tab-item-wrapper">
                            <span class="tab-name fontSize18">选项卡四</span>
                        </div>
                    </li>
                    <li class="tab-item swiper-slide">
                        <div class="tab-item-wrapper">
                            <span class="tab-name fontSize18">选项卡五</span>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
        <div class="product-wrapper"></div>
            * { margin: 0; padding: 0; }
            ul { list-style: none; }
            fontSize18 { font-size: 18!important; }
            /* 选项卡 */
            .swiper-nav {
                width: 100%;
                height: 5em;
                box-sizing: border-box;
                border-bottom: 1px solid rgba(0,0,0,0.3);
                background-color: #FAFAFA;
                display: flex;
                justify-content: center;
                align-items: flex-end;
            }
            .page-tab {
                width: 80%;
                height: 90%;
            }
            .tab-wrapper {
                margin: 0 auto;
                height: 100%;
                display: flex;
                align-items: flex-end; 
            }
            .tab-item {
                flex-shrink: 0;
                height: 80%;
                padding: 0 1.5em;
                border-right: 1px solid #CACECA;
                box-sizing: border-box;
                position: relative;
                color: black;
            }
            .tab-item:last-child { border-right: none; }
            .tab-item-wrapper {
                width: auto;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                cursor: pointer;
            }
            .underline { color: #006722!important; }
            .underline::before {
                content: '';
                position: absolute;
                left: 50%;
                bottom: 0;
                width: 60%;
                height: 3px;
                background-color: #006722;
                transform: translate(-50%, 0);
            }
            .tab-name {
                flex-shrink: 0;
                font-weight: 300;
                word-break: break-word;
            }
            .product-wrapper { 
                width: 100%;
                text-align: center;
                margin-top: 20px;
            }
            var nowWindowWith = $(window).width()
            var nowNav = '#swiper'
            var $TabNum = 0;
            
            if(nowWindowWith <= 850)  $TabNum = 3 
            else if(nowWindowWith <= 1520)  $TabNum = 4 
            else $TabNum = 5 
            
            function initSwiper(tabNum) {
                var mySwiper = new Swiper ('#swiper', {
                    slidesPerView: tabNum,
                    spaceBetween: 0
                })   
            }
            initSwiper($TabNum);
            // 选项卡切换
            $('.tab-item').on('click', function() { 
                if($(this).hasClass('underline')) return
                $(this).addClass('underline').siblings().removeClass('underline')
                $('.product-wrapper').empty().text($(this).find('.tab-name').text() + '内容')
            })
            $(`.tab-item`).eq(0).trigger('click')
            
            $(window).resize(function() {
                var newTabNum = 0;
                nowWindowWith = $(window).width()
                if(nowWindowWith <= 850)  newTabNum = 3
                else if(nowWindowWith <= 1520)  newTabNum = 4 
                else newTabNum = 5 
                if($TabNum == newTabNum) return;
                $TabNum = newTabNum;
                setTimeout(() => { initSwiper($TabNum) }, 20)
            })

2.实现效果:

当屏幕宽度为1520px以上时:

当屏幕宽度为850至1520px时:

当屏幕宽度为850px以下时:

3.要点

这里主要使用了swiper插件,该插件功能强大。

使用要点:初始化Swiper时,需要动态地给new Swiper内传递参数,来决定一次要显示几个导航项。

并且通过$(window).resize(function() { $(window).width() })实时获取当前屏幕地宽度,从而决定要显示几个导航项。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一GG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值