电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:24日期:2022-12-30 16:12:33

template里面:

<!-- tab切換star --> <ul :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是當(dāng)前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>

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

data里面:

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

curId默認(rèn)為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切換組件(吸頂、滾動定位)等效果

目標(biāo)問題

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

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

實現(xiàn)效果動圖

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

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

<template> <div :style='prop.box_style ? prop.box_style : ’’'> <div class='tab__div'> <ul :style='prop.attr.tab_style ? prop.attr.tab_style : ’’'><li 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 :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() // 這里首次進(jìn)入頁面當(dāng)前選中項為第一個 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 }, // 計算當(dāng)前選中滑動塊兒的滑動距離和當(dāng)前選中項 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)` // 當(dāng)前選中的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ù)滾動距離來計算當(dāng)前可是區(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 : 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)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
主站蜘蛛池模板: 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 杭州门窗厂家_阳光房_包阳台安装电话-杭州窗猫铝合金门窗 | 公交驾校-北京公交驾校欢迎您! 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 卷筒电缆-拖链电缆-特种柔性扁平电缆定制厂家「上海缆胜」 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 塑料薄膜_PP薄膜_聚乙烯薄膜-常州市鑫美新材料包装厂 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 飞利浦LED体育场灯具-吸顶式油站灯-飞利浦LED罩棚灯-佛山嘉耀照明有限公司 | 江西自考网-江西自学考试网 | 月嫂_保姆_育婴_催乳_母婴护理_产后康复_养老护理-吉祥到家家政 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 阻垢剂-反渗透缓蚀阻垢剂厂家-山东鲁东环保科技有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 | 无锡网站建设-做网站-建网站-网页设计制作-阿凡达建站公司 | 防火门-专业生产甲级不锈钢钢质防火门厂家资质齐全-广东恒磊安防设备有限公司 | 收录网| 对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | b2b网站大全,b2b网站排名,找b2b网站就上地球网 | QQ房产导航-免费收录优秀房地产网站_房地产信息网 | 探鸣起名网-品牌起名-英文商标起名-公司命名-企业取名包满意 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 微学堂-电动能源汽车评测_电动车性能分享网 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 防潮防水通风密闭门源头实力厂家 - 北京酷思帝克门窗 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 广州市哲铭油墨涂料有限公司,水性漆生产研发基地 | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 冷水机-冰水机-冷冻机-冷风机-本森智能装备(深圳)有限公司 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 礼堂椅厂家|佛山市艺典家具有限公司 |