第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Vue造輪子-Tabs測試(上)

標簽:
JavaScript

1. 点击出现下划线的问题

    // tabs-item.vue
    methods: {
      xxx() {
        this.eventBus.$emit('update:selected', this.name, this) 
      }
    }
    // tabs.vue
    mounted(){
      this.eventBus.$emit('update:selected', this.selected) 
    }
    // tabs-head
    created(){
      this.eventBus.$on('update:selected', (item, vm) => {
        console.log(item)
        console.log(vm) // 初始化的时候打印出的值为undefined 
      })
    }
    
    // 为了解决初始化打印出为undefined的问题
    // tabs.vue 通过两层循环解决找到item
    mounted(){
      this.$children.forEach((vm)=>{
        if(vm.$options.name === 'GuluTabsHead'){ // 找儿子
          vm.$children.forEach((childVm)=>{ // 找孙子
            if(childVm.$options.name === 'GuluTabsItem' && childVm.name === this.selected){
              this.eventBus.$emit('update:selected', this.selected,childVm)
            }
          })
        }
      })
    }

2. 找到item之后开始做动画

 mounted(){
      this.eventBus.$on('update:selected', (item, vm) => {
        let {width, height, top, left} = vm.$el.getBoundingClientRect()
        this.$refs.line.style.width = `${width}px` // 宽度就会跟着我们点的元素的宽度变,
        // this.$refs.line.style.left = `${left}px` // 宽度变了我们再修改位置
        this.$refs.line.style.transform = `translateX(${left}px)` 
        // 可以这样优化写来实现硬件3d加速
        // 但是这样写有个bug一开始会从左往右滑
      })
    }

3. 尝试解决一开始从走往右滑动的bug

  // tabs-head.vue
    <template>
        <div class="tabs-head">
            <slot></slot>
                <div class="line" ref="line" v-if="x"></div>
            </div>
        </div>
    </template>

    data(){
      return {
        x: false
      }
    },
    mounted(){
      this.eventBus.$on('update:selected', (item, vm) => {
        this.x = true
        // 新增一个[更新UI任务]到任务队列里面,
        // 会先把下面js执行完了再更新,所以 this.$refs.line.style.width会报错
        // 我们只要把代码放到[更新UI任务]的后面就能解决
        this.$nextTick(()=>{
          // 新增一个函数,放到任务队列里面,由于[更新UI任务]是先放进去那么就会先执行
          // 但是这样做还是没解决从左往右滑的bug,所以还是改回left
          let {width, height, top, left} = vm.$el.getBoundingClientRect()
          this.$refs.line.style.width = `${width}px`
          this.$refs.line.style.transform = `translateX(${left}px)`
        })
      })
    }

4.增加禁用功能和禁用样式

    // 增加disabled样式
    computed: {
      classes() { // classes是一个计算属性
        return {
          active: this.active,
          disabled: this.disabled,
        }
      }
    },
    // 增加disabled行为
    methods: {
      onClick() {
        if (this.disabled) {
          return
        }
        this.eventBus.$emit('update:selected', this.name, this)
      }
    }
點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消