taro scroll tabs

滚动标签 切换

https://www.cnblogs.com/lml-lml/p/10954069.html

https://developers.weixin.qq.com/community/develop/doc/000cc0b94ac5f8dcf4e7666475b804

https://blog.csdn.net/weixin_42860683/article/details/83817925

https://juejin.im/post/5d563b5e6fb9a06b317b5d20

taro


import Taro, {
Component,
} from '@tarojs/taro'
import {
View,
Text,
ScrollView,
} from '@tarojs/components' import classnames from 'classnames'
import './TabList.scss' export default class TabList extends Component {
state = {
active: 0,
} componentWillMount() {
this.setState({
active: this.props.current || 0,
})
} componentDidMount() { } componentWillReceiveProps(nextProps) {
const { current } = nextProps
if (current !== this.props.current) {
this.setState({
active: current,
})
}
} onSelectTab(index) {
this.setState({
active: index,
})
this.props.onSelected && this.props.onSelected(index)
} render() {
let { tabs, tabItemStyle, scrollWithAnimation } = this.props
let { active } = this.state
return (
tabs.length && <ScrollView className="tab-list" scrollX scrollWithAnimation={scrollWithAnimation}
scrollIntoView={`tab-${active >= 2 ? (active - 2) : 0}`}
>
<View className="tab-list__container">
{
tabs.map((item, index) => {
return (
<View style={tabItemStyle} className={classnames([
'tab-item',
{
active: active === index,
},
])} onClick={this.onSelectTab.bind(this, index)} key={index} id={`tab-` + index}
>
<Text>{item.name}</Text>
<View className="tab-line"></View>
</View>
)
})
}
</View> </ScrollView>
)
}
}
TabList.defaultProps = {
tabs: [],
current: 0,
tabItemStyle: 'height:78rpx',
scrollWithAnimation: process.env.TARO_ENV === 'weapp',
}

scroll tab 组件

开发一个可以滚动的tab组件

https://github.com/ScoutYin/ly-tab#readme

https://github.com/ScoutYin/ly-tab/tree/master/src

https://www.cnblogs.com/lml-lml/p/10954069.html

window.requestAnimationFrame

animation


export function windowInit () {
var lastTime = 0
var vendors = ['webkit', 'moz']
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || // name has changed in Webkit
window[vendors[x] + 'CancelRequestAnimationFrame']
} if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime()
var timeToCall = Math.max(0, 16.7 - (currTime - lastTime))
var interval = currTime - lastTime
var id = window.setTimeout(function () {
callback(interval)
}, timeToCall)
lastTime = currTime + timeToCall
return id
}
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function (id) {
clearTimeout(id)
}
}
}

touch event


methods: {
// start
handleTouchStart (event) {
event.stopPropagation()
cancelAnimationFrame(this.inertiaFrame)
this.lastX = event.touches[0].clientX
},
// move
handleTouchMove (event) {
if (this.listWidth <= 0) return
event.preventDefault()
event.stopPropagation()
this.touching = true
this.startMoveTime = this.endMoveTime
this.startX = this.lastX
this.currentX = event.touches[0].clientX
this.moveFollowTouch()
this.endMoveTime = event.timeStamp // 每次触发touchmove事件的时间戳;
},
// end
handleTouchEnd (event) {
this.touching = false
if (this.checkReboundX()) {
cancelAnimationFrame(this.inertiaFrame)
} else {
let silenceTime = event.timeStamp - this.endMoveTime
let timeStamp = this.endMoveTime - this.startMoveTime
timeStamp = timeStamp > 0 ? timeStamp : 8
if (silenceTime > 100) return // 停顿时间超过100ms不产生惯性滑动;
this.speed = (this.lastX - this.startX) / timeStamp
this.acceleration = this.speed / this.sensitivity
this.frameStartTime = new Date().getTime()
this.inertiaFrame = requestAnimationFrame(this.moveByInertia)
}
},
// 如果需要回弹则进行回弹操作并返回true;
checkReboundX () {
this.reBounding = false
if (this.translateX > 0) {
this.reBounding = true
this.translateX = 0
} else if (this.translateX < -this.listWidth) {
this.reBounding = true
this.translateX = -this.listWidth
}
return this.translateX === 0 || this.translateX === -this.listWidth
},
bindEvent () {
this.$el.addEventListener('touchstart', this.handleTouchStart, false)
this.$el.addEventListener('touchmove', this.handleTouchMove, false)
this.$el.addEventListener('touchend', this.handleTouchEnd, false)
},
removeEvent () {
this.$el.removeEventListener('touchstart', this.handleTouchStart)
this.$el.removeEventListener('touchmove', this.handleTouchMove)
this.$el.removeEventListener('touchend', this.handleTouchEnd)
},
// touch拖动
moveFollowTouch () {
if (this.isMoveLeft) { // 向左拖动
if (this.translateX <= 0 && this.translateX + this.listWidth > 0 || this.translateX > 0) {
this.translateX += this.currentX - this.lastX
} else if (this.translateX + this.listWidth <= 0) {
this.translateX += this.additionalX * (this.currentX - this.lastX)
/ (this.viewAreaWidth + Math.abs(this.translateX + this.listWidth))
}
} else { // 向右拖动
if (this.translateX >= 0) {
this.translateX += this.additionalX * (this.currentX - this.lastX)
/ (this.viewAreaWidth + this.translateX)
} else if ((this.translateX <= 0 && this.translateX + this.listWidth >= 0) ||
this.translateX + this.listWidth <= 0) {
this.translateX += this.currentX - this.lastX
}
}
this.lastX = this.currentX
},
// 惯性滑动
moveByInertia () {
this.frameEndTime = new Date().getTime()
this.frameTime = this.frameEndTime - this.frameStartTime
if (this.isMoveLeft) { // 向左惯性滑动;
if (this.translateX <= -this.listWidth) { // 超出边界的过程;
// 加速度指数变化;
this.acceleration *= (this.reBoundExponent +
Math.abs(this.translateX + this.listWidth)) /
this.reBoundExponent
this.speed = Math.min(this.speed - this.acceleration, 0) // 为避免减速过程过短,此处加速度没有乘上frameTime;
} else {
this.speed = Math.min(this.speed - this.acceleration * this.frameTime, 0)
}
} else if (this.isMoveRight) { // 向右惯性滑动;
if (this.translateX >= 0) {
this.acceleration *= (this.reBoundExponent + this.translateX) / this.reBoundExponent
this.speed = Math.max(this.speed - this.acceleration, 0)
} else {
this.speed = Math.max(this.speed - this.acceleration * this.frameTime, 0)
}
}
this.translateX += this.speed * this.frameTime / 2
if (Math.abs(this.speed) <= this.zeroSpeed) {
this.checkReboundX()
return
}
this.frameStartTime = this.frameEndTime
this.inertiaFrame = requestAnimationFrame(this.moveByInertia)
},
// 计算activeBar的translateX
calcBarPosX () {
if (this.fixBottom || !this.$children.length) return
if (this.$children.length <= this.value) return
const item = this.$children[this.value].$el
const itemWidth = item.offsetWidth
const itemLeft = item.offsetLeft
this.activeBarWidth = Math.max(itemWidth * 0.6, 14)
this.activeBarX = itemLeft + (itemWidth - this.activeBarWidth) / 2
},
// 点击切换item时,调整位置使当前item尽可能往中间显示
checkPosition () {
if (this.fixBottom || !this.$children.length) return
if (this.$children.length <= this.value) return
const activeItem = this.$children[this.value].$el
const offsetLeft = activeItem.offsetLeft
const half = (this.viewAreaWidth - activeItem.offsetWidth) / 2
let changeX = 0
const absTransX = Math.abs(this.translateX)
if (offsetLeft <= absTransX + half) { // item偏左,需要往右移
let pageX = offsetLeft + this.translateX
changeX = half - pageX
} else { // item偏右,需要往左移
changeX = -(offsetLeft - absTransX - half)
}
let lastX = changeX + this.translateX
// 两种边界情况
lastX > 0 && (lastX = 0)
lastX < -this.listWidth && (lastX = -this.listWidth)
this.reBounding = true
this.translateX = lastX
}
}

xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


taro scroll tabs 滚动标签 切换的更多相关文章

  1. 一个页面tab标签切换,都有scroll事件的解决办法

    当前页有多个tab,如果都有scroll事件, 先解绑$(window).off('scroll') 再执行scroll就不可以了,多个标签就不会互相干扰: 给你们个例子: //标签切换    $(' ...

  2. taro swiper & scroll tabs

    taro swiper & scroll tabs https://taro-docs.jd.com/taro/docs/components/viewContainer/swiper.htm ...

  3. vant中tab标签切换时会改变内容滚动高度

    vant的tabs标签页,标签切换时会改变内容区的滚动高度,这是因为内容区共用同一个父元素为滚动区域引起的,解决办法:在tabs的内容区域嵌套一层滚动区域,让每个内容区域使用单独的滚动元素就行了.   ...

  4. 网站开发,推荐使用SuperSlide 插件-Tab标签切换,图片滚动,无缝滚动,焦点图

    SuperSlide 致力于解决网站大部分特效展示问题,使网站代码规范整洁,方便维护更新.网站上常用的“焦点图/幻灯片”“Tab标签切换”“图片滚动”“无缝滚动”等只需要一个SuperSlide即可解 ...

  5. “焦点图/幻灯片”“Tab标签切换”“图片滚动”“无缝滚动”仅需一个SuperSlidev2.1

    官网:http://www.superslide2.com/index.html 1. 标签切换 / 书签切换 / 默认效果 2. 焦点图 / 幻灯片 3. 图片滚动-左 4. 图片滚动-上 5. 图 ...

  6. scroll tabs

    scroll tabs https://github.com/NervJS/taro-ui/blob/dev/src/components/tabs/index.tsx https://github. ...

  7. js鼠标滚轮滚动图片切换效果

    效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...

  8. JavaScript----marquee滚动标签 图片无缝滚动 插入百度地图

    页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...

  9. 很好用的Tab标签切换功能,延迟Tab切换。

    一个网页,Tab标签的切换是常见的功能,但我发现很少有前端工程师在做该功能的时候,会为用户多想想,如果你觉得鼠标hover到标签上,然后切换到相应的内容,就那么简单的话,你将是一个不合格的前端工程师啊 ...

随机推荐

  1. WPF Selector、SelectIndex、SelectedValue、SelectedValuePath、SelectedItem这几兄弟你分的清楚嘛?

    Selector Selector是一个抽象类,继承ItemsControl类(包含任何类型的对象(例如字符串,图像或面板)的集合),而本文的4个兄弟都是Selector类下的4个属性. Select ...

  2. codevs1700 施工方案第二季

    题目描述 Description c国边防军在边境某处的阵地是由n个地堡组成的.工兵连受命来到阵地要进行两期施工. 第一期的任务是挖掘暗道让所有地堡互联互通.现已勘测设计了m条互不相交的暗道挖掘方案, ...

  3. mysql数据恢复:.frm和.ibd,恢复表结构和数据

    mysql数据恢复:.frm和.ibd,恢复表结构和数据 一.恢复表结构 二.恢复表数据 相关内容原文地址: CSDN:她说巷尾的樱花开了:mysql根据.frm和.ibd文件恢复表结构和数据 博客园 ...

  4. SpringBoot-Maven打包压缩瘦身

    SpringBoot-Maven打包压缩瘦身 一.Spring Boot 可执行 jar 分析 1.1 打包 1.2 两种 jar 的比较 1.3 一次打包两个 jar 二.SpringBoot迭代发 ...

  5. Java——I/O,字节流与字符流,BufferedOutputStream,InputStream等(附相关练习代码)

    I/O: I/O是什么? 在程序中,所有的数据都是以流的形式进行传输或者保存. 程序需要数据的时候,就要使用输入流读取数据. 程序需要保存数据的时候,就要使用输出流来完成. 程序的输入以及输出都是以流 ...

  6. 手机用itunes安装更新系统

    1.[Shift+更新]:仅对固件进行更新,保留现有资料和已经安装的程序.但是已经越狱的iPhone或iPad禁止使用此方法!否则有后遗症!没有越狱的iPhone或iPad则可以直接使用此方式.2.[ ...

  7. (3)UNIX/Linux系统结构

    UNIX/Linux 系统可以粗糙地抽象为 3 个层次,如图所示.底层是 UNIX/Linux 操作系统,即系统内核(Kernel):中间层是Shell层,即命令解释层:高层则是应用层. 1) 内核层 ...

  8. pytest测试框架+jenkins结合pytest+jenkins邮件通知配置

    刚刚做完一个项目,由于这是一个方案项目,而不是产品,所以各种准备很不充分,很多公司的能力不能复用,整个团队又都是新员工,而且有部分实习生,匆忙上马,今天对我的自动化框架做一个回溯 自动化测试框架的选择 ...

  9. dedecms文章页的上下篇颠倒的问题

    dedecms的文章页底下的上下篇,如果按照时间排序的话,最新的一篇应该是最上了,但是底下还是会显示上一篇文章还有,然后下一篇文章没有了,就是颠倒了.如何修改呢. 1.修改include目录下arc. ...

  10. 90% 的 Java 程序员都说不上来的为何 Java 代码越执行越快(2)- TLAB预热

    经常听到 Java 性能不如 C/C++ 的言论,也经常听说 Java 程序需要预热,那么其中主要原因是啥呢? 面试的时候谈到 JVM,也有很多面试官喜欢问,为啥 Java 程序越执行越快呢? 一般人 ...