昨天看虎牙直播,发现导航栏挺有意思,自己也做个玩玩

<view class="tab_list row">
<view class="tab_item center" wx:for='{{tab_list}}' data-index="{{index}}" bindtap="tab">{{item}}</view>
<view class="border_line" style="left: {{border_left}}px; width: {{border_width}}px;"></view>
</view>
.tab_list{
position: relative
} .tab_item{
width: 20%;
height: 40px;
font-size: 14px;
} .border_line{
position: absolute;
bottom: 2px;
height: 6px;
background: linear-gradient(90deg, #FFA500, #FFD700 );
border-radius: 10px;
transition: all .2s;
}
// pages/tab/tab.js
Page({ /**
* 页面的初始数据
*/
data: {
tab_list: ['全部', '推荐', 'LOL', '户外', '一起看'],
tab_index: 0,
last_index: 0,
pos_list: [],
border_width: 16,
border_left: 0,
is_disable:false //禁止点击
}, /**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getPosition();
},
getPosition() {
let tab_index = this.data.tab_index, border_width = this.data.border_width, last_index = this.data.last_index;
let tab_width2 = border_width / 2;
let pos_list = this.data.pos_list;
if (!pos_list.length) {
const query = wx.createSelectorQuery()
query.selectAll('.tab_item').boundingClientRect()
query.exec(res => {
let { width } = res[0][0];
this.setData({
border_left: width / 2 - tab_width2,
pos_list: res[0]
})
})
} else {
let current, last;
let tab_left = pos_list[tab_index].left, tab_width = pos_list[tab_index].width;
let last_left = pos_list[last_index].left, last_width = pos_list[last_index].width;
if (tab_index < last_index) {//向左
current = tab_left + tab_width / 2 - tab_width2;
last = last_left + last_width / 2 + tab_width2;
let width = Math.abs(current - last); //当前和上次的宽度
this.setData({
border_width: width,
border_left: tab_left + tab_width / 2 - tab_width2, //向左需要动画过渡
}, () => {
setTimeout(() => {
this.setData({
border_width,
is_disable:false
})
}, 200)
})
} else { //向右
current = tab_left + tab_width / 2 + tab_width2;
last = last_left + last_width / 2 - tab_width2;
let width = Math.abs(current - last);
this.setData({
border_width: width,
}, () => {
setTimeout(() => {
this.setData({
border_width,
border_left: tab_left + tab_width / 2 - tab_width2,
is_disable: false
})
}, 300)
})
}
}
},
tab(e) {
if (this.data.is_disable) return; let index = e.currentTarget.dataset.index;
let tab_index = this.data.tab_index;
if (index != tab_index) {
this.setData({
last_index:tab_index,
tab_index: index,
is_disable:true
})
this.getPosition();
}
},
})

模仿虎牙App 导航栏切换的更多相关文章

  1. 极致精简的fragment实现导航栏切换demo

    一个小demo.用button+fragment实现导航栏切换界面,适合刚接触的新手看一下. 效果图 点击第二个后 源码: 主界面 <span style="font-size:18p ...

  2. mui底部导航栏切换分页

    使用Hbuilder的mui框架开发移动端非常便利.高效: 底部导航栏切换功能也是移动APP开发中必须实现的: 引入mui文件.下面会用到jquery,同时引进 <link href=" ...

  3. Flutter - TabBar导航栏切换后,状态丢失

    上一篇讲到了 Flutter - BottomNavigationBar底部导航栏切换后,状态丢失 里面提到了TabBar,这儿专门再写一下吧,具体怎么操作,来不让TabBar的状态丢失.毕竟大家99 ...

  4. Flutter - BottomNavigationBar底部导航栏切换后,状态丢失

    如果你用过BottomNavigationBar.TabBar.还有Drawer,你就会发现,在切换页面之后,原来的页面状态就会丢失. 要是上一页有一个数据列表,很多数据,你滚动到了下头,切换页面后, ...

  5. Flutter实战视频-移动电商-04.底部导航栏切换效果

    04.底部导航栏切换效果 博客地址: https://jspang.com/post/FlutterShop.html#toc-291 我们要做的效果图: 新建四个页面 home_page.dart ...

  6. html5 导航栏切换效果

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

  7. MUI底部导航栏切换效果

    首先是html代码: <nav class="mui-bar mui-bar-tab"> <a href="view/templates/home/ho ...

  8. react-native-tab-view 导航栏切换插件讲解

    首先引入插件 yarn add react-native-tab-view 如果用的原生环境要安装另外几个插件 yarn add react-native-reanimated react-nativ ...

  9. vue 导航栏切换

    <template> <footer class="menu"> <router-link to="/" class=" ...

随机推荐

  1. scrapy 框架基本使用

    scrapy简介: Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 ...

  2. Seekbar扩大点击区域

    //扩大点击区域private void enlargeSeekBar() { mContentView.setOnTouchListener(new OnTouchListener() {//mCo ...

  3. android WebView缩放时卡顿问题

    问题描述: WebView 在双指缩放页面时会卡顿 解决过程:有试过打开硬件加速android:hardwareAccelerated="true" 还是没用,纠结了一天,最后屏蔽 ...

  4. Common Subsequence Gym - 102307C 公共序列

    2019 ICPC Universidad Nacional de Colombia Programming Contest C D J   C. Common Subsequence 题意:给出长度 ...

  5. stl队列

    队列(Queue)也是一种运算受限的线性表,它的运算限制与栈不同,是两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插 ...

  6. 1.什么是Mybatis?

    在学习之前我们要回顾以下知识 JDBC Mysql java基础 Maven Junit 什么是 MyBatis? MyBatis 是一款优秀的持久层框架MyBatis 避免了几乎所有的 JDBC 代 ...

  7. 什么是this指针?this的几种指向

    在JavaScript中,this指针是在创建时,由系统默认生成的两个隐式参数之一(另一个是arguments). this指针指向与该函数调用进行隐式关联的一个对象,该对象被称为“函数上下文”. t ...

  8. nginx配置指令auth_basic、auth_basic_user_file及相关知识

    参考链接 https://www.jianshu.com/p/1c0691c9ad3c auth_basic_user_file 所需要的密码,可以通过如下方法生成 1)安装htpasswd (yum ...

  9. 【C语言】scanf()输入浮点型数据

    #include<stdio.h> int main() { double x1, x2, x3, x4; printf("输入2个浮点数x1,x2:\n"); sca ...

  10. Vacation

    题目看上去倒是蛮复杂的样子嘞!