vue + swiper 實現(xiàn)今日頭條觸摸滑動tab欄

1、引入swiper.js
2、html結(jié)構(gòu)為

<div id="topNav" class="swiper-container">
    <div class="swiper-wrapper">
         <div class="swiper-slide" v-for="(item,index) in depList" :class="{active:dep_id==item.department_id}">
            <span>{{item.department_name}}</span>  
         </div>
     </div>
</div>

3、定義變量

export default{
        name:"find_doc",
        data(){
            return {
                mySwiper:false,
                swiperWidth:'',
                maxTranslate:'',
                maxWidth:'',
                depList:[
                          {department_id: 1, department_name: "內(nèi)科"},
                          {department_id: 5, department_name: "外科"}
                         ] ,
            }
        },

4、具體代碼

   mounted(){
       var that = this;
       that.mySwiper = new Swiper('#topNav', {
                       freeMode: true,
                       freeModeMomentumRatio: 0.5,
                       slidesPerView: 'auto',
                       resistanceRatio:0.7,
                   });
                   that.swiperWidth =that.mySwiper.container['0'].clientWidth;
                   that.maxTranslate = that.mySwiper.maxTranslate();
                   that.maxWidth = -that.maxTranslate + that.swiperWidth / 2
                   that.mySwiper.on('tap', function(swiper, e) {
                       //swiper.clickedIndex(獲取到當(dāng)前點擊的索引)
                       if(that.dep_id==that.depList[swiper.clickedIndex].department_id){
                           return;
                       }
                       that.loading_show=true;
                       that.dep_name=that.depList[swiper.clickedIndex].department_name;
                       that.dep_id=that.depList[swiper.clickedIndex].department_id;
                       var slide,slideLeft,slideWidth,slideCenter,nowTlanslate;
                       slide = swiper.slides[swiper.clickedIndex]
                       slideLeft = slide.offsetLeft
                       slideWidth = slide.clientWidth
                       slideCenter = slideLeft + slideWidth / 2
                       that.mySwiper.setWrapperTransition(300)
                       if (slideCenter < that.swiperWidth / 2) {
                           that.mySwiper.setWrapperTranslate(0)
                   
                       } else if (slideCenter > that.maxWidth) {
                           that.mySwiper.setWrapperTranslate(that.maxTranslate)
                   
                       } else {
                           nowTlanslate = slideCenter - that.swiperWidth / 2
                           that.mySwiper.setWrapperTranslate(-nowTlanslate)                    
                       }
     }

效果見:http://www.17sucai.com/preview/702590/2017-06-08/toutiaoNav/index.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容