React横向滚动计算

class Footer extends React.Component {
handleClick(e) {
const offset = 150; // 指定偏移量
this.scroller.scrollLeft = e.currentTarget.offsetLeft - offset;
} render() {
return <section className="m-tab">
<ul className="tab" ref={node => this.scroller = node}>
<li className="tab-item checked" onClick={e => this.handleClick(e)}>
<p className="day">DAY1</p>
<p className="content">抵达曼谷</p>
</li>
<li className="tab-item" onClick={e => this.handleClick(e)}>
<p className="day">DAY2</p>
<p className="content">抵达曼谷</p>
</li>
<li className="tab-item" onClick={e => this.handleClick(e)}>
<p className="day">DAY3</p>
<p className="content">抵达曼谷</p>
</li>
<li className="tab-item" onClick={e => this.handleClick(e)}>
<p className="day">DAY4</p>
<p className="content">抵达曼谷</p>
</li>
<li className="tab-item" onClick={e => this.handleClick(e)}>
<p className="day">DAY5</p>
<p className="content">抵达曼谷</p>
</li>
<li className="tab-item" onClick={e => this.handleClick(e)}>
<p className="day">DAY6</p>
<p className="content">抵达曼谷</p>
</li>
<li className="tab-item" onClick={e => this.handleClick(e)}>
<p className="day">DAY7</p>
<p className="content">抵达曼谷</p>
</li>
</ul>
</section>;
}
}

React Native横向滚动计算

import {View, Text, ScrollView, TouchableOpacity} from 'react-native';
import {_container, _inner, _item, _text} from './index.style'; const offsetX = 50; // tab默认偏移量 export default class FixedTab extends QComponent {
static reduxPlugin = {
mapStateToProps: state => state
} constructor(props) {
super(props);
this.scrollX = 0; // scroller的动态x偏移量
this.scrollWidth = 0; // scroller容器的宽度
this.contentWidth = 0; // scroller内部元素的宽度
this.itemLayout = {}; // scroller初始化每项的x偏移量
} onScroll(e) {
this.scrollX = e.nativeEvent.contentOffset.x;
} onLayout(e) {
this.scrollWidth = e.nativeEvent.layout.width;
} onContentLayout(e) {
this.contentWidth = e.nativeEvent.layout.width;
} onItemLayout(e, index) {
this.itemLayout[index] = e.nativeEvent.layout.x;
} changeType(index) {
// 滚动逻辑
const itemOffsetX = this.itemLayout[index] - this.scrollX;
if(itemOffsetX > offsetX) { // 右侧超出固定偏移量
if(this.itemLayout[index] + this.scrollWidth - offsetX < this.contentWidth) {
this.scroller.scrollTo({x: this.itemLayout[index] - offsetX});
} else {
this.scroller.scrollTo({x: this.contentWidth - this.scrollWidth});
}
} else {
if(this.itemLayout[index] > offsetX) { // 默认当前元素偏移量大于固定偏移量
this.scroller.scrollTo({x: this.itemLayout[index] - offsetX});
} else {
this.scroller.scrollTo({x: 0});
}
}
} render() {
return <ScrollView
ref={node => this.scroller = node}
style={_container}
horizontal={true}
showsHorizontalScrollIndicator={false}
onLayout={e => this.onLayout(e)}
onScroll={e => this.onScroll(e)}
scrollEventThrottle={16}
>
<View style={_inner} onLayout={e => this.onContentLayout(e)}>
{list.map((item, i) =>
<View key={i} onLayout={e => this.onItemLayout(e, i)}>
<TouchableOpacity style={_item} onPress={() => this.changeType(i)}>
<Text style={_text}>{item.name}</Text>
</TouchableOpacity>
</View>
)}
</View>
</ScrollView>;
}
}

横向tab计算滚动位置的更多相关文章

  1. vue-scroller记录滚动位置

    问题描述: 列表页进入详情页,或者tab页切换,然后再返回列表页,希望能切换到之前滚动位置 解决问题思路: 切换到其他页面前记录位置,返回列表页的时候返回位置.这就需要借助vue-router的bef ...

  2. jQuery内容横向拖拽滚动

    如果有业务需求:使用横向滚动,而又不想用滚动条,可以使用横向拖拽滚动,主要是利用元素的scrollLeft特性: 废话不多说直接上代码: css: .box{ width:100%; height:3 ...

  3. React+React Router+React-Transition-Group实现页面左右滑动+滚动位置记忆

    2018年12月17日更新: 修复在qq浏览器下执行pop跳转时页面错位问题 本文的代码已封装为npm包发布:react-slide-animation-router 在React Router中,想 ...

  4. 横向不间断滚动DIV

    横向不间断滚动DIV,5个一组,js控制,自动生成任意组显示 <!DOCTYPE html> <html> <head> <meta http-equiv=& ...

  5. “Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置

    这个扩展属性从WP8.1就开始用了,主要是为了解决MVVM模式中无法直接控制ListView滚动位置的问题.比如在VM中刷新了数据,需要将View中的ListView滚动到顶部,ListView只有一 ...

  6. html--offsetLeft,Left,clientLeft的关键--动态获取计算元素位置关系

    动态计算元素位置关系的时候,必备... http://www.cnblogs.com/panjun-Donet/articles/1294033.html

  7. android开发之记录ListView滚动位置

    这个问题本身不难,但是由于项目中的需求太过于复杂,结果导致这个问题变得不是那么容易实现.在网上一搜,结果如下: 我不知道是who copy who?反正介绍的所谓的三种方法,第一种都是无法运行的,很明 ...

  8. javascript-图片横向无缝隙滚动(可在服务器运行)

    前两次弄'图片横向滚动'javascript,在本地上运行得很美,可是一上到我们学校后台的服务器,就有很多问题,这个算是行的了. css代码: <style type="text/cs ...

  9. Android listView scroll 恢复滚动位置

    相信大家尝试过许多方法恢复滚动位置,本人也找了许多方法,唯有这个方法好用,下面把代码贴出来 声明两个变量 private int mPosition; private int lvChildTop; ...

随机推荐

  1. web前端学习(三)css学习笔记部分(3)-- css常用操作

    5.  CSS常用操作 5.1  对齐 使用margin属性进行水平对齐 <!DOCTYPE html> <html lang="en"> <head ...

  2. web前端学习(二)html学习笔记部分(3)--range对象

    1.2.8  html5编辑api之range对象(一) 1.2.8.1  Range 对象基本概念 Range 对象的基本概念,通过使用 Range 对象所提供的方法实现一个鼠标选取内容,通过点击按 ...

  3. js面向对象开发基础

    js的面向对象开发能力较弱,基本是以prototype为核心的面向对象,虽然现在出了个class这玩意,但本文还是先不做探讨. 面向对象基础——构造函数方法 var Fly = function (s ...

  4. FileIntputStream / FileOutputStream 类

    FileInputStream类(重点)     (1)基本概念 java.io.FileInputStream类用于读取诸如图像之类的原始字节流.   (2)常用的方法 FileInputStrea ...

  5. Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】

    A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. Direct2D 第6篇 绘制多种风格的线条

    原文:Direct2D 第6篇 绘制多种风格的线条 上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的"慢", ...

  7. java 日期合法

    try { String date_str = "5555-22-33"; SimpleDateFormat format=new SimpleDateFormat("y ...

  8. bnd -buildpath指令的用法

    -buildpath的作用是为项目添加运行时依赖.这个依赖可以是workspace中的另一个项目或者是仓库中的另一个bundle. -buildpath指令只会在编译和构建时起作用,它从来不会被用来运 ...

  9. 自定义注解--Annotation

    Annotation 概念:注解 原理 是一种接口,通过反射机制中的相关API来访问annotation信息 常见的标准Annotation @Override   方法重写 @Deprecated  ...

  10. CSS权值比较(读书笔记)

    1.继承0.1    标签1    类选择符10    ID选择符100 2.层叠:后面的样式会覆盖前面的样式. 3.内联样式表(标签内部) > 嵌入样式表(当前文件中) > 外部样式(外 ...