vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作


Posted in Javascript onJuly 22, 2020

template里面:

<!-- tab切换star -->
   <ul class="tab-list" :class="{fixTitle:whether}">
    <li @click="curId=0" :class="{'cur':curId===0}">产品特点</li>
    <li @click="curId=1" :class="{'cur':curId===1}">投保须知</li>
    <li @click="curId=2" :class="{'cur':curId===2}">理赔流程</li>
   </ul>
  <!-- 切换内容star -->

设置fixTitle的样式,固定在顶部,cur是当前tab点击的颜色

<div class="tab-con">
 <div v-show="curId===0">
    第一部分内容
  </div>
 <div v-show="curId===1">
    第二部分内容
  </div>
 <div v-show="curId===2">
    第三部分内容
  </div>
</div>

当点击第一个产品特点的时候,对应下面的第一部分内容,点击投保须知对应第二部分内容,点击理赔流程对应第三部分内容

data里面:

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

curId默认为0,展示的是产品特点的内容,也就是第一部分内容

methods里面:

设置滚动效果:

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可视区域后,Tab拦当前选项主动滑动到该DOM上。且点击tab拦会定位到对应的dom位置。

vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作

实现效果动图

vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作

实现目的——html结构

<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>

实现目的——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;
  }
}

实现目的——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的状态
      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()钩子函数中获取到的dom位置不对,在滑动中获取dom的页面位置
        this.getDomsObj()
        this.scrolltop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset
        // 滑动根据滚动距离来计算当前可是区域的选中项
        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切换不同的内容操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
UI Events 用户界面事件
Jun 27 Javascript
只需20行代码就可以写出CSS覆盖率测试脚本
Apr 24 Javascript
为开发者准备的10款最好的jQuery日历插件
Feb 04 Javascript
javascript获取dom的下一个节点方法
Sep 05 Javascript
javascript实现状态栏文字首尾相接循环滚动的方法
Jul 22 Javascript
ES6中非常实用的新特性介绍
Mar 10 Javascript
AngularJS 入门教程之事件处理器详解
Aug 19 Javascript
JavaScript实现的斑马线表格效果【隔行变色】
Sep 18 Javascript
ejsExcel模板在Vue.js项目中的实际运用
Jan 27 Javascript
一个Vue视频媒体多段裁剪组件的实现示例
Aug 09 Javascript
axios的拦截请求与响应方法
Aug 11 Javascript
使用p5.js实现动态GIF图片临摹重现
Oct 23 Javascript
vue页面跳转实现页面缓存操作
Jul 22 #Javascript
ES2020系列之空值合并运算符 '??'
Jul 22 #Javascript
解决vue单页面 回退页面 keeplive 缓存问题
Jul 22 #Javascript
vue移动端弹起蒙层滑动禁止底部滑动操作
Jul 22 #Javascript
在vue中实现禁止屏幕滚动,禁止屏幕滑动
Jul 22 #Javascript
vue 弹出遮罩层样式实例
Jul 22 #Javascript
vue中解决拖拽改变存在iframe的div大小时卡顿问题
Jul 22 #Javascript
You might like
php xml文件操作代码(一)
2009/03/20 PHP
PHP生成excel时单元格内换行问题的解决方法
2010/08/26 PHP
zend api扩展的php对象的autoload工具
2011/04/18 PHP
php获取当月最后一天函数分享
2015/02/02 PHP
浅谈PHP中单引号和双引号到底有啥区别呢?
2015/03/04 PHP
PHP使用Memcache时模拟命名空间及缓存失效问题的解决
2016/02/27 PHP
windows下的WAMP环境搭建图文教程(推荐)
2017/07/27 PHP
PHP调用其他文件中的类
2018/04/02 PHP
JQuery鼠标移到小图显示大图效果的方法
2015/06/10 Javascript
js代码实现随机颜色的小方块
2015/07/30 Javascript
基于JavaScript操作DOM常用的API小结
2015/12/01 Javascript
jQuery实现下拉框功能实例代码
2016/05/06 Javascript
jQuery源码解读之extend()与工具方法、实例方法详解
2017/03/30 jQuery
bootstrap modal+gridview实现弹出框效果
2017/08/15 Javascript
vue2.0获取鼠标位置的方法
2018/09/13 Javascript
vue-cli项目使用mock数据的方法(借助express)
2019/04/15 Javascript
Vue源码之关于vm.$delete()/Vue.use()内部原理详解
2019/05/01 Javascript
JavaScript 俄罗斯方块游戏实现方法与代码解释
2020/04/08 Javascript
[03:03]2014DOTA2西雅图国际邀请赛 Alliance战队巡礼
2014/07/07 DOTA
Python中常用信号signal类型实例
2018/01/25 Python
pygame游戏之旅 载入小车图片、更新窗口
2018/11/20 Python
使用Flask-Cache缓存实现给Flask提速的方法详解
2019/06/11 Python
Reebonz中国官网:新加坡奢侈品购物网站
2017/03/17 全球购物
Crocs美国官方网站:卡骆驰洞洞鞋
2017/08/04 全球购物
巴西最大的家具及装饰用品店:Mobly
2017/10/11 全球购物
学年自我鉴定范文
2013/10/01 职场文书
三好学生个人先进事迹材料
2014/05/17 职场文书
骨干教师考核评语
2014/12/31 职场文书
升职感谢信
2015/01/22 职场文书
房屋认购协议书
2015/01/29 职场文书
小学教师年度个人总结
2015/02/05 职场文书
二胎满月酒致辞
2015/07/29 职场文书
史上最全书信经典范文大全(建议收藏)
2019/07/10 职场文书
javascript拖曳互换div的位置实现示例
2021/06/28 Javascript
JavaScript文档对象模型DOM
2021/11/20 Javascript
工厂无线对讲系统解决方案
2022/02/18 无线电