• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

vue 搞代码 4年前 (2022-01-08) 15次浏览 已收录 0个评论

这篇文章主要介绍了vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

template里面:

 <!-- tab切换star --><ul class="tab-list"> <li>产品特点</li><li>投保须知</li><li>理赔流程</li></ul><!-- 切换内容star -->

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

 <div class="tab-con"> <div> 第一部分内容 </div><div> 第二部分内容 </div><div> 第三部分内容 </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位置。

实现效果动图

实现目的――html结构

  <div class="tab__main"> <div class="tab__div"> <ul class="tab__list"> <li class="tab__item"> <span>{{ tabs.tab_menu }}</span></li></ul><div class="active__line"></div></div></div>

实现目的――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  { // 由于在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.scrolltop  (this.domobj.dom3 - tab.offsetHeight * 2) && this.scrolltop  (this.domobj.dom4 - tab.offsetHeight * 10) && this.scrolltop <this.domobj.dom4) { this.computerSlide(3) } } } }

完成目的――页面引用组件

  <div> <div class="div__header"></div><div class="div__item">{{ item }}</div><div class="div__footer">我是有底线的</div></div> 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'] } } }  .div_<span style="color:transparent">来1源gaodai#ma#com搞*代#码1网</span>_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; } 

以上就是vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址