最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue tab滾動到一定高度,固定在頂部,點擊tab切換不同的內(nèi)容操作

 更新時間:2020年07月22日 11:54:08   作者:M_SSY  
這篇文章主要介紹了vue tab滾動到一定高度,固定在頂部,點擊tab切換不同的內(nèi)容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

template里面:

  <!-- tab切換star -->
   <ul class="tab-list" :class="{fixTitle:whether}">
    <li @click="curId=0" :class="{'cur':curId===0}">產(chǎn)品特點</li>
    <li @click="curId=1" :class="{'cur':curId===1}">投保須知</li>
    <li @click="curId=2" :class="{'cur':curId===2}">理賠流程</li>
   </ul>
  <!-- 切換內(nèi)容star -->

設(shè)置fixTitle的樣式,固定在頂部,cur是當前tab點擊的顏色

<div class="tab-con">
 <div v-show="curId===0">
    第一部分內(nèi)容
  </div>
 <div v-show="curId===1">
    第二部分內(nèi)容
  </div>
 <div v-show="curId===2">
    第三部分內(nèi)容
  </div>
</div>

當點擊第一個產(chǎn)品特點的時候,對應(yīng)下面的第一部分內(nèi)容,點擊投保須知對應(yīng)第二部分內(nèi)容,點擊理賠流程對應(yīng)第三部分內(nèi)容

data里面:

data(){
  return:{
    whether:false,
    curId:0
  }
}

curId默認為0,展示的是產(chǎn)品特點的內(nèi)容,也就是第一部分內(nèi)容

methods里面:

設(shè)置滾動效果:

  handleScroll() {
   var scrollTop =
    window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
   // console.log(scrollTop)
   if(scrollTop>329){
    this.whether = true;
   }else{
    this.whether = false;
   }
  },

在mounted里面:

window.addEventListener("scroll", this.handleScroll);

補充知識:vue組件之自定義tab切換組件(吸頂、滾動定位)等效果

目標問題

進入頁面后,滾動一定距離使其吸頂。滾動到某DOM可視區(qū)域后,Tab攔當前選項主動滑動到該DOM上。且點擊tab攔會定位到對應(yīng)的dom位置。

實現(xiàn)效果動圖

實現(xiàn)目的——html結(jié)構(gòu)

<template>
  <div class="tab__main" :style="prop.box_style ? prop.box_style : ''">
    <div class="tab__div">
      <ul class="tab__list" :style="prop.attr.tab_style ? prop.attr.tab_style : ''">
        <li class="tab__item" v-for="(tabs, index) in prop.attr.tab_content" :key="tabs.tab_menu" @click="onClickTabs(index)"
        >
          <span :style="tabStyle(index)">{{ tabs.tab_menu }}</span>
        </li>
      </ul>
      <div class="active__line" :style="prop.attr.cur_slide_style ? prop.attr.cur_slide_style : ''"></div>
    </div>
  </div>
</template>

實現(xiàn)目的——less樣式

.tab__div {
  width: 100%;
  height: 102px;
  position: relative;
  padding-top: 36px;
  background-color: #fff;
  .tab__list {
    display: flex;
    .tab__item {
      flex: 1;
      font-size: 32px;
    }
    .tab__item-current {
      font-weight: 700;
    }
  }
  .active__line {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 187.5px;
    height: 5px;
    background-color: #4B8FFB;
    transition: all 300ms;
  }
}

實現(xiàn)目的——js部分

export default {
  name: 'TabModule',
  props: {
    prop: {
      type: Object
    }
  },
  data () {
    return {
      scrolltop: null,
      dom: null,
      domobj: {} // 各個dom的頁面位置
    }
  },
  computed: {
    tabStyle () {
      return function (params) {
        return this.prop.attr.tab_content[params].tab_style || ''
      }
    }
  },
  mounted () {
    this.onWatchScroll()
    // 這里首次進入頁面當前選中項為第一個
    document.querySelectorAll('.tab__item')[0].style = this.prop.attr.cur_tab_style ? this.prop.attr.cur_tab_style : ''
  },
  methods: {
    // 獲取各個dom的頁面位置
    getDomsObj () {
      this.domobj.dom1 = document.querySelectorAll(`#${this.prop.attr.tab_content[0].anchor_point}`)[0].offsetTop
      this.domobj.dom2 = document.querySelectorAll(`#${this.prop.attr.tab_content[1].anchor_point}`)[0].offsetTop
      this.domobj.dom3 = document.querySelectorAll(`#${this.prop.attr.tab_content[2].anchor_point}`)[0].offsetTop
      this.domobj.dom4 = document.querySelectorAll(`#${this.prop.attr.tab_content[3].anchor_point}`)[0].offsetTop
    },
    // 計算當前選中滑動塊兒的滑動距離和當前選中項
    computerSlide (val) {
      let tablist = document.querySelectorAll('.tab__item')
      for (let i = 0; i < tablist.length; i++) {
        tablist[i].style = ''
      }
      document.querySelector('.active__line').style.transform = `translateX(${(val * document.querySelector('.active__line').offsetWidth)}px)`
      // 當前選中的tab_item的狀態(tài)
      tablist[val].style = this.prop.attr.cur_tab_style ? this.prop.attr.cur_tab_style : ''
    },
    onClickTabs (index) {
      this.computerSlide(index)
      // 計算點擊后滑動到DOM的位置
      this.dom = document.querySelectorAll(`#${this.prop.attr.tab_content[index].anchor_point}`)[0].offsetTop
      let tabbox = document.querySelectorAll('.tab__div')[0]
      if (index === 0) {
        if (tabbox.style.position === 'relative') {
          window.scrollTo(0, this.dom - tabbox.clientHeight)
          return
        }
        window.scrollTo(0, this.dom)
        return
      }
      if (index === 3) {
        window.scrollTo(0, this.dom)
        return
      }
      if (tabbox.style.position === 'relative') {
        window.scrollTo(0, this.dom - (tabbox.clientHeight * 2))
      } else {
        window.scrollTo(0, this.dom - tabbox.clientHeight)
      }
    },
    onWatchScroll () {
      let tabmain = document.querySelectorAll('.tab__main')[0]
      let tabdiv = document.querySelectorAll('.tab__div')[0]
      tabdiv.style.top = 0
      window.onscroll = () => {
        // 由于在created()或者mounted()鉤子函數(shù)中獲取到的dom位置不對,在滑動中獲取dom的頁面位置
        this.getDomsObj()
        this.scrolltop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset
        // 滑動根據(jù)滾動距離來計算當前可是區(qū)域的選中項
        this.onScrollPage()
        // 自定義吸頂效果
        if (this.scrolltop > tabmain.offsetTop) {
          tabdiv.style.position = 'fixed'
          tabdiv.style['z-index'] = 99
        } else {
          tabdiv.style.position = 'relative'
          tabdiv.style['z-index'] = 0
        }
      }
    },
    onScrollPage () {
      let tab = document.querySelectorAll('.tab__div')[0]
      if (this.scrolltop > this.domobj.dom1 && this.scrolltop < (this.domobj.dom2 - tab.offsetHeight * 2)) {
        this.computerSlide(0)
      } else if (this.scrolltop > (this.domobj.dom2 - tab.offsetHeight * 2) && this.scrolltop < this.domobj.dom3 - tab.offsetHeight * 2) {
        this.computerSlide(1)
      } else if (this.scrolltop > (this.domobj.dom3 - tab.offsetHeight * 2) && this.scrolltop < (this.domobj.dom3 + tab.offsetHeight * 2)) {
        this.computerSlide(2)
      } else if (this.scrolltop > (this.domobj.dom4 - tab.offsetHeight * 10) && this.scrolltop < this.domobj.dom4) {
        this.computerSlide(3)
      }
    }
  }
}

完成目的——頁面引用組件

<template>
  <div>
    <div class="div__header"></div>
    <tab-module :prop="tabattributes"></tab-module>
    <div :id="tabattributes.attr.tab_content[index].anchor_point" class="div__item" v-for="(item, index) in fordiv" :key="item">{{ item }}</div>
    <div class="div__footer">我是有底線的</div>
  </div>
</template>

import TabModule from '../../components/TabModule.vue'
export default {
  components: {
    TabModule
  },
  data () {
    return {
      tabattributes: {
        box_style: "margin-top: 10px;",         
        attr: {      
          cur_tab_style: "font-weight: 700;", 
          cur_slide_style: "",
          tab_content: [{
            tab_menu: "控件1",       
            anchor_point: "DomPoint1",
            tab_style: "" 
          }, {
            tab_menu: "控件2",       
            anchor_point: "DomPoint2",  
            tab_style: "" 
          }, {
            tab_menu: "控件3",       
            anchor_point: "DomPoint3",  
            tab_style: "" 
          }, {
            tab_menu: "控件4",       
            anchor_point: "DomPoint4", 
            tab_style: "" 
          }]
        }
      },
      fordiv: ['頁面控件1', '頁面控件2', '頁面控件3', '頁面控件4']
    }
  }
}

<style lang="less" scoped>
.div__header {
  width: 100%;
  height: 200px;
  background-color: #909;
}
.div__item {
  width: 100%;
  height: 400px;
  background-color: rgb(78, 215, 224);
  text-align: center;
  font-size: 26px;
}
.div__footer {
  width: 100%;
  height: 200px;
  background-color: #090;
}
</style>

以上這篇vue tab滾動到一定高度,固定在頂部,點擊tab切換不同的內(nèi)容操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 拖拽插件sortable.js實現(xiàn)el-table表格拖拽效果

    拖拽插件sortable.js實現(xiàn)el-table表格拖拽效果

    本文主要介紹了拖拽插件sortable.js實現(xiàn)el-table表格拖拽效果,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • vue-editor-bridge報錯的解決方案

    vue-editor-bridge報錯的解決方案

    這篇文章主要介紹了vue-editor-bridge報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 聊一聊Vue.js過渡效果

    聊一聊Vue.js過渡效果

    這篇文章主要和大家一起聊一聊Vue.js過渡效果、CSS 過渡效果、純 JavaScript過渡效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • VUE 3D輪播圖封裝實現(xiàn)方法

    VUE 3D輪播圖封裝實現(xiàn)方法

    這篇文章主要為大家詳細介紹了VUE 3D輪播圖封裝實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Vue.js的Mixins使用方式

    Vue.js的Mixins使用方式

    Vue.js的Mixins功能允許封裝可復(fù)用的組件選項,實現(xiàn)代碼復(fù)用和模塊化,Mixins可以包含數(shù)據(jù)、方法、生命周期鉤子等組件選項,使用時,Mixins中的選項會被混入組件中,優(yōu)先級低于組件自身選項,優(yōu)點包括代碼復(fù)用、高靈活性和簡單易用
    2024-09-09
  • vue 項目引入echarts 添加點擊事件操作

    vue 項目引入echarts 添加點擊事件操作

    這篇文章主要介紹了vue 項目引入echarts 添加點擊事件操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vuex存儲復(fù)雜參數(shù)(如對象數(shù)組等)刷新數(shù)據(jù)丟失的解決方法

    vuex存儲復(fù)雜參數(shù)(如對象數(shù)組等)刷新數(shù)據(jù)丟失的解決方法

    今天小編就為大家分享一篇vuex存儲復(fù)雜參數(shù)(如對象數(shù)組等)刷新數(shù)據(jù)丟失的解決方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue自定義指令簡介和基本使用示例

    Vue自定義指令簡介和基本使用示例

    同時Vue也支持讓開發(fā)者,自己注冊一些指令,這些指令被稱為自定義指令,每個指令都有自己各自獨立的功能,這篇文章主要介紹了Vue自定義指令簡介和基本使用,需要的朋友可以參考
    2024-03-03
  • 一文帶你搞懂Vue3?defineModel中的雙向綁定

    一文帶你搞懂Vue3?defineModel中的雙向綁定

    隨著vue3.4版本的發(fā)布,defineModel也正式轉(zhuǎn)正了,它可以簡化父子組件之間的雙向綁定,是目前官方推薦的雙向綁定實現(xiàn)方式,下面就跟隨小編一起深入了解一下defineModel的使用吧
    2024-02-02
  • Vue3?封裝一個支持輸入和單/多選InputSelect組件-Antd詳解

    Vue3?封裝一個支持輸入和單/多選InputSelect組件-Antd詳解

    Antd的Select組件默認不支持作為輸入框使用或手動添加選項,為了實現(xiàn)這一功能,我們封裝了一個通用組件,支持單選和多選模式,并允許用戶在組件失焦時手動輸入選項,主要通過定義searchText存儲輸入數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧
    2024-09-09

最新評論

鸡西市| 阿坝县| 延庆县| 云南省| 涟水县| 调兵山市| 玉屏| 迭部县| 浙江省| 福州市| 辽宁省| 泽普县| 鹤壁市| 改则县| 甘洛县| 曲阳县| 云浮市| 社会| 田阳县| 隆昌县| 正阳县| 偏关县| 日喀则市| 榆树市| 忻城县| 来安县| 长顺县| 宣化县| 北宁市| 吉木萨尔县| 诸城市| 黑河市| 江源县| 邓州市| 紫云| 永平县| 富源县| 兴业县| 开封县| 新干县| 方正县|