欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現(xiàn)頂部左右滑動導航

 更新時間:2021年06月29日 10:39:10   作者:Geraint_z  
這篇文章主要為大家詳細介紹了vue實現(xiàn)頂部左右滑動導航,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

日常開發(fā)中經(jīng)常用到導航這些東西,寫篇文章記錄下。該導航實現(xiàn)為點擊末尾/起首位置,導航自動滑動出下一項的效果。

思路:判斷當前點擊項,相對與屏幕的位置,若點擊的位置,滿足可移動的限制,進行自動滑動處理。

實現(xiàn)如下:

vue

<template>
  <div class="debug-index-page">
    <div class="tab-layout" id="scroller">
      <ul v-for="(tab, idx) in tabList" :key="idx">
        <li
          :id="`tab-${tab.id}`"
          class="tab-item"
          @click="onClickTab(tab)"
          :style="`background:${tab.select ? 'red' : 'none'}`"
        >
          {{ tab.text }}
        </li>
      </ul>
    </div>
  </div>
</template>

JS

export default {

    data() {
        return {
            tabList: [],
        }
    },

    created() {
        let list = [
            "我的貴族",
            "貴族1",
            "我的貴族2",
            "貴族3",
            "貴族4",
            "貴族5",
            "我的貴族6",
            "我的貴族7",
        ];

        list.forEach((text, idx) => {
            this.tabList.push({
                text,
                id: idx, // tab標識
                select: idx == 0, // 是否被選擇
                index: idx // 處于顯示的位置
            });
        });
    },

    computed: {
        curTab() {
            return this.tabList.find(v => v.select);
        }
    },

    methods: {

        onClickTab(tabInfo) {
            let curTab = this.curTab;
            if (curTab.id == tabInfo.id) return;
            let { index, id } = tabInfo;
            // 滑動控件
            let scroller = document.getElementById("scroller");
            let speed = scroller.scrollWidth / this.tabList.length;
            let tab = document.getElementById(`tab-${id}`);
            let bWidth = document.body.clientWidth;
            // 點擊右邊
            if (curTab.index < index && tab.clientWidth * index >= bWidth - speed) {
            // 滑動的距離
                scroller.scrollLeft = (index + 2) * speed - bWidth;
            } else if (curTab.index > index && (tab.clientWidth * index - (scroller.scrollLeft + bWidth) < speed)) {
            // 滑動的距離
                scroller.scrollLeft = (index - 1) * speed;
            }

            curTab.select = false;
            this.tabList[index].select = true;
        }
    }
}

less

.debug-index-page {
    width: 100%;
    overflow:hidden;


  .tab-layout {
    width: 100%;
    overflow-x: scroll;
    display: flex;

    .tab-item {
      width: 1rem;
      text-align: center;
    }
  }
}

以上就是導航的顯示了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論