最开始,还没有使用better-scroll插件的时候,直接在class中设定了一定的position为sticky,设置一定的top达成了效果。但是,使用better-scroll组件后,这些属性就没有用了。那么,为了还能达成这个效果,按照以下方法。

在实现这个效果之前,必须先知道滚动到多少位置时,开始有吸顶效果。

首先我先尝试了,在home.vue中的mounted里面直接

 console.log(this.$refs.tabcontrol.$el.offsetTop);

但是这个打印出来的结果明显是不正确的,因为在mounted里面拿到的是,很多图片还没有加载完毕的,因此这里所得出来的值是不正确的。经过测试,发现tabcontrol上面的三个组件中,轮播图的加载是最为影响的,因此我就监听homeswiper中的图片加载,使用@load来完成。加载完成后,发出事件后,再home.vue中获取正确的值。

homeswiper.vue中:

<template>
<swiper>
<swiperitem v-for="(item,index) in banners" :key="index">
<a :href="item.link">
<img :src="item.image" @load="imageload">
</a>
</swiperitem>
</swiper>
</template>
<script>
import swiperitem from "../../../src/components/common/swiper/swiperitem";
import swiper from "../../../src/components/common/swiper/swiper";
export default {
name: "homeswiper",
props:{
banners:{
type:Array,
default(){
return []
}
}
},
components:{swiperitem,swiper},
data(){
return{
isload:false }
},
methods:{
imageload(){
// console.log("111");
if (!this.isload){
this.$emit("swiperimageload")
this.isload = true
} }
}
}
</script> <style scoped> </style>

在home.vue中:

<tabcontrol :titles="['流行','新款','精选']"   @tabclick="tabclick"
ref="tabcontrol2"> </tabcontrol>

<homeswiper :banners="banners" @swiperimageload="swiperimageload"></homeswiper>
methods:{
swiperimageload(){
// console.log(this.$refs.tabcontrol.$el.offsetTop);
this.tabOffsetTop = this.$refs.tabcontrol.$el.offsetTop
}
}
data(){
return{
tabOffsetTop:0
}
}

解析:

首先,现在homeswiper中对其图片进行监听,并定义一个方法为imageload。这个方法中,将swiperimageload发出。由于,homeswiper与home为子与父组件关系。然后,在home中,先设置taboffsetTop为0,之后再在swiperimageload当中将其赋值给tabOffsetTop。

在这里,我为了不让homeswiper多次发出事件,我在homeswiper中的data加了一个isload,之后我使用isload的变量进行状态的记录。

之后,我们就可以在之前做backtop组件中,所使用到的contentscroll方法中,将当前position与tabOffsetTop进行一个判断。这边我们可以再回顾下原来在scroll中写的:

mounted(){
//1.创建BScroll对象
this.scroll = new BScroll(this.$refs.wrapper,{
click:true,
probeType:this.probeType,
pullUpLoad:this.pullUpLoad
//监听滚动到底部
})
// this.scroll.scrollTo(0,0)
//2.监听滚动的位置
this.scroll.on("scroll",(position)=>{
// console.log(position);
this.$emit("scroll",position)
})
}

在home.vue中写的:

<scroll class="content" ref="scroll" :probe-type="3" @scroll="contentscroll" :pull-up-load="true" @pullingUp="loadmore">
data(){
return{
banners:[],
recommends:[],
goods:{
'pop':{page:0,list:[]},
'new':{page:0,list:[]},
'sell':{page:0,list:[]}
},
currenttype:'pop',
isshow:false,
tabOffsetTop:0,
istabfixed:false
}
}
contentscroll(position){
//1.判断backtop是否显示
this.isshow= (-position.y) > 1000
//2.决定tabcontrol是否吸顶
this.istabfixed = (-position.y) > this.tabOffsetTop
}

最开始,我是按照上述写之后,直接在tabcontrol组件中利用istabfixed的值,然后用v-bind:class="{active:istabfixed}",再在active中设置,position为fixed,以及一定的top值,但是实际执行后,发现,在此处position为fixed时,并不生效。显示出来的结果为,下面的商品内容会往上移,且tabcontrol随着滚动会滑出去。达不到吸顶效果,因此为了达到效果,我们在上面多复制了一份PlaceHoldertabcontrol组件对象,利用他来实现停留效果。当用户滚动到一定位置时,PlaceHoldertabcontrol显示出来,而当用户未滚动到一定位置时,被隐藏起来。

<template>
<div class="homie">
<navbar class="home-nav"><div slot="center">购物街</div> </navbar>
<tabcontrol :titles="['流行','新款','精选']" @tabclick="tabclick"
ref="tabcontrol1" class="tab" v-show="istabfixed"> </tabcontrol>
<scroll class="content" ref="scroll" :probe-type="3" @scroll="contentscroll" :pull-up-load="true" @pullingUp="loadmore">
<homeswiper :banners="banners" @swiperimageload="swiperimageload"></homeswiper>
<reco :recommends="recommends"></reco>
<featureview></featureview>
<tabcontrol :titles="['流行','新款','精选']" @tabclick="tabclick"
ref="tabcontrol2"> </tabcontrol>
<goodslist :goods="showgoods"></goodslist>
</scroll>
</div>
</template>
 contentscroll(position){
this.isshow= (-position.y) > 1000
this.istabfixed = (-position.y) > this.tabOffsetTop
}
 tabclick(index){
switch (index){
case 0:
this.currenttype ="pop"
break
case 1:
this.currenttype = 'new'
break
case 2:
this.currenttype="sell"
break
}
this.$refs.tabcontrol1.currentIndex = index;
this.$refs.tabcontrol2.currentIndex = index;
}

这样的设置为,于第一个tabcontrol中加一个v-show,当位置的绝对值大于tabOffsetTop时,istabfixed为true,而使得第一个tabcontrol显示出来。且为了保持数据一致,设置两个tabcontrol值,之后便在tabclick方法中,将index赋值于它们,最后设置下,第一个tabcontrol的样式。

.tab{
position: relative;
z-index: 9;
}

这样便实现了tabcontrol的吸顶效果。

tabControl组件的吸顶效果的更多相关文章

  1. [RN] React Native 头部 滑动吸顶效果的实现

    React Native 头部 滑动吸顶效果的实现 效果如下图所示: 实现方法: 一.吸顶组件封装 StickyHeader .js import * as React from 'react'; i ...

  2. 自定义tab吸顶效果一(原理)

    PS:问题:什么是吸顶,吸顶有什么作用,吸顶怎么使用? 在很多app商城中,介绍软件的时候就会使用吸顶效果, 吸顶有很多作用,一个最简单粗暴的作用就是,让用户知道此刻在浏览哪个模块,并可以选择另外的模 ...

  3. Html吸顶效果

    Html吸顶效果 一.HTML HTML中需要给div一个id <!DOCTYPE html> <html lang="en"> <head> ...

  4. 基于scroll的吸顶效果

    本次要实现的是一种常见的网页效果,如下: 页面由头部,导航,主体内容三部分组成,当页面发生滚动时,头部逐渐隐藏,导航部分向上移动,直到导航部分距离浏览器顶部为零时,导航部分固定不动,保持吸顶效果,如下 ...

  5. better-scroll之吸顶效果巨坑挣扎中

    今天和大家分享下better-scroll这款移动端用来解决各种滚动需求的插件(目前已经支持PC) 关于其中的API大家可以去官网看下  这里就给大家介绍几种常用的以及需要注意的点是什么 首先说一下b ...

  6. js之吸顶效果

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. react.js中实现tab吸顶效果问题

    在react项目开发中有一个需求是,页面滚动到tab所在位置时,tab要固定在顶部. 实现的思路其实很简单,就是判断当滚动距离scrollTop大于tab距离页面顶部距离offsetTop时,将tab ...

  8. Vue 事件监听实现导航栏吸顶效果(页面滚动后定位)

    Vue 事件监听实现导航栏吸顶效果(页面滚动后定位) Howie126313 关注 2017.11.19 15:05* 字数 100 阅读 3154评论 0喜欢 0 所说的吸顶效果就是在页面没有滑动之 ...

  9. js 实现吸顶效果 || 小程序的吸顶效果

    小程序吸顶效果 <!--index.wxml--> <view class="container"> <view class='outside-img ...

随机推荐

  1. Nacos windows下 ERROR Nacos failed to start, please see

    如果是windows 本地启动 直接到你本地\nacos\bin下   用cmd 启动即可 startup.cmd -m standalone 看见它你就算是 完成 了   直接访问下本的的端口  h ...

  2. charles功能(二)修改response请求参数

    1.接口处 鼠标右击,选择breakpoints(允许本接口使用breakpionts功能) 2.开始设置断点值 4.重新请求接口(charles的界面变为可编辑状态),修改请求参数,执行请求 5.最 ...

  3. springmvc<三> 异常解析链与视图解析链

    1.1.7. Exceptions    - 如果异常被Controller抛出,则DispatchServlet委托异常解析链来处理异常并提供处理方案(通常是一个错误的响应)        spri ...

  4. 老猿学5G:融合计费场景的离线计费会话的Nchf_OfflineOnlyCharging_Release释放操作

    ☞ ░ 前往老猿Python博文目录 ░ 一.Nchf_OfflineOnlyCharging_Release消息交互流程 Nchf_OfflineOnlyCharging_Release是CHF提供 ...

  5. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButtons标准按钮

    在Qt Designer中,可以在界面中使用QDialogButtonBox来配置一组按钮进行操作,Qt中为QDialogButtonBox定义了一组常用的标准按钮,可以在Designer中直接在St ...

  6. PyQt(Python+Qt)学习随笔:布局控件layoutSpacing属性

    在Qt Designer中布局控件有4个,分别是Vertical Layout(垂直布局).Horizontal Layout(水平布局).Grid Layout(网格布局).Form Layout( ...

  7. BJOI2017 喷式水战改

    题目链接. Description 维护一个序列,支持操作: 每次在 \(P_i\) 位置后插入一段 \(X_i\) 单位的燃料,这一段有三个模式,对应的能量分别是 \(A_i, B_i, C_i\) ...

  8. basic english

    color/visual see look color dark light beautiful shade black blue brown clear gray green orange red ...

  9. nginx学习之——信号控制和配置

    一.信号控制 1)TERM, INT   Quick shutdown  \\麻溜停掉(暴力停止),一般不常用 // 启动和停止nginx 当前目录:/usr/local/bin/nginx 启动: ...

  10. 终于不再对transition和animation,傻傻分不清楚了 --vue中使用transition和animation

    以前写页面注重在功能上,对于transition和animation是只闻其声,不见其人,对于页面动画效果心理一直痒痒的.最近做活动页面,要求页面比较酷炫,终于有机会认真了解了. transition ...