移动端滚动方案:better-scroll
BetterScroll 是什么
BetterScroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件。它的核心是借鉴的 iscroll (opens new window) 的实现,它的 API 设计基本兼容 iscroll,在 iscroll 的基础上又扩展了一些 feature 以及做了一些性能优化。
BetterScroll 是使用纯 JavaScript 实现的,这意味着它是无依赖的。
官方文档:BetterScroll
官方 Demo:示例
为什么不能滚动:滚动原理

绿色部分为 wrapper,也就是父容器,它会有固定的高度。黄色部分为 content,它是父容器的第一个子元素,它的高度会随着内容的大小而撑高。那么,当 content 的高度不超过父容器的高度,是不能滚动的,而它一旦超过了父容器的高度,我们就可以滚动内容区了,这就是 BetterScroll 的滚动原理。
在项目中使用
基本 HTML 结构
<div class="container">
<header class="header-wrapper">
<div class="header-content">
<div class="header-content-item">动画</div>
<div class="header-content-item">漫画</div>
<div class="header-content-item">游戏</div>
<div class="header-content-item">文学</div>
</div>
</header>
<div class="wrapper">
<div class="content">
<span class="pullup-txt">Loading...</span>
</div>
</div>
</div>
样式
html,body
margin: 0
padding: 0
background: #ccc
.container
position: relative
.header-wrapper
header-wrapper-height = 40px
background: red
height: header-wrapper-height
width: 100%
white-space: nowrap
overflow: hidden
.header-content
display: inline-block
padding: 0 10px
box-sizing: border-box
.header-content-item
display: inline-block
width: 125px
height: 100%
line-height: header-wrapper-height
.wrapper
position: fixed
top: 45px
bottom: 10px
height: auto
width: 100%
// background: green
overflow: hidden
.content
// position: absolute
padding: 0 10px
box-sizing: border-box
.pullup-txt
display: none
text-align: center
display: block
font-size: 17px
color: white
.content-item
width: 100%
background: white
height: 100px
display: flex
align-items: center
padding: 0 10px
box-sizing: border-box
margin-bottom: 10px
border-radius: 10px
.content-item-avatar
width: 50px
height: 50px
img
width: 50px
height: 50px
border-radius: 50%
.content-item-body
margin-left: 10px
JavaScript 逻辑
import '../style/index/index.styl'
import 'lib-flexible'
import $ from 'jquery'
// 导入better-scroll核心组件
import BScroll from '@better-scroll/core'
import Pullup from '@better-scroll/pull-up'
import ObserveDOM from '@better-scroll/observe-dom'
BScroll.use(ObserveDOM)
BScroll.use(Pullup)
let bodyScroll
let timer
let number = 0
const maxNumber = 10
$(function () {
// 头部滑动
let headerWrapper = document.querySelector('.header-wrapper')
let headerScroll = new BScroll(headerWrapper, {
scrollX: true,
scrollY: false,
probeType: 3 // listening scroll event
})
// 内容区滑动
let wrapper = document.querySelector('.wrapper')
bodyScroll = new BScroll(wrapper, {
scrollY: true,
probeType: 3, // listening scroll event
tap: 'tap',
// 上拉加载更多示例
pullUpLoad: true,
ObserveDOM: true
})
bodyScroll.on('pullingUp', pullingUpHandler)
bodyScroll.autoPullUpLoad()
})
function pullingUpHandler() {
$('.pullup-txt').show()
clearTimeout(timer)
timer = setTimeout(async () => {
number++
if (number >= maxNumber) {
// 假设没有数据了
$('.pullup-txt').text('没有数据了!')
bodyScroll.refresh()
bodyScroll.closePullUp()
} else {
const result = await getSaid()
$('.pullup-txt').before(render(result))
bodyScroll.refresh()
bodyScroll.finishPullUp()
$('.pullup-txt').hide()
}
}, 1000)
}
async function getSaid(c) {
const result = await $.get('https://v1.hitokoto.cn/', {
c
})
return result.hitokoto
}
function render(text) {
return `<div class="content-item">
<div class="content-item-avatar">
<img
src="https://cdn.jsdelivr.net/npm/kang-static@latest/avatar.jpg"
/>
</div>
<div class="content-item-body">${text}</div>
</div>`
}
在线 Demo
使用注意点
- 父容器需小于滚动容器才可以滚动
- 下拉刷新,上拉加载等需要安装对应插件
移动端滚动方案:better-scroll的更多相关文章
- 【JS】341- 移动端滚动穿透的6种解决方案
前言 相信能看到这篇文章的你,已经是遇到了这个问题.我就不gif展示问题效果了. 鉴于此问题是面试的常客,故特地针对滚动穿透这个疑难杂症,整理了六个解决方案. 各方法操作难易不同,分别针对弹层和bod ...
- 有了这套flexible.js 移动端自适应方案,你就能在移动端的来去自如, (*^__^*)
flexible.js 移动端自适应方案 一,flexible.js 的使用方式: github地址:https://github.com/amfe/lib-flexible 官方文档地址:https ...
- 基于jquery鼠标或者移动端滚动加载数据
基于jquery鼠标或者移动端滚动加载数据 var stop = true; // 防止重复请求数据 $(window).scroll(function () { totalheight = pars ...
- finereport普通报表的移动端自适应方案
移动端报表呈现,首先要求的是页面随手机屏幕大小自动放缩(自适应),下面给出一个普通报表中的finereport移动端自适应方案,适用于finereport 7.1之前的版本. 首先,了解一下当前我们可 ...
- 滚动锚定(Scroll Anchoring)- 让视口内容不再因视口上方 DOM 元素的高度变化而产生跳动
不知道你有没有经历过这样的场景:当你打开一张“多图杀猫”的页面后,正一张图一张图边滚边看,在你刚准备定睛看某一张图的时候,这张图突然被它上面的内容挤到了视口下方,然后你赶紧把滚动条往下拉,试图追赶这张 ...
- 最火移动端跨平台方案盘点:React Native、weex、Flutter
1.前言 跨平台一直是老生常谈的话题,cordova.ionic.react-native.weex.kotlin-native.flutter等跨平台框架的百花齐放,颇有一股推倒原生开发者的势头. ...
- rem移动端适配方案
一. rem vs em 单位 定义 特点 rem font size of the root element 以根元素字体大小为基准 em font size of the element 以父元素 ...
- Java 服务端监控方案(四. Java 篇)
http://jerrypeng.me/2014/08/08/server-side-java-monitoring-java/ 这个漫长的系列文章今天要迎来最后一篇了,也是真正与 Java 有关的部 ...
- 一款多功能的移动端滚动选择器,支持单选到多选、支持多级级联、提供自定义回调函数、提供update函数二次渲染、重定位函数、兼容pc端拖拽等等..
https://github.com/onlyhom/mobileSelect.js/blob/master/docs/README-CN.md mobileSelect.js 一款多功能的移动端滚动 ...
随机推荐
- HCNP Routing&Switching之组播技术-组播地址
前文我们聊到了组播技术背景,单播.广播在点到多点应用中的问题,以及组播对比单播.广播在点到多点的网络环境中的优势.劣势,相关回顾请参考https://www.cnblogs.com/qiuhom-18 ...
- CF1368F Lamps on a Circle
思考我们一定有最后一个状态是空着的灯是按照一个间隔\(k\) 只要将原来\(n\)个灯,每\(k\)个分一组,强制将最后一盏灯不选,并且第n盏灯不选,需要注意的是某一组一定会被第二个人全部关掉,那么可 ...
- 【2020五校联考NOIP #7】伟大的卫国战争
题面传送门 题意: 数轴上有 \(n\) 个点,现在要在它们之间连 \(m\) 条边,第 \(i\) 条边连接 \(a_i,b_i\) 两个点. 现在你要钦定每条边连在数轴的上方还是下方,使得任意两条 ...
- Codeforces 809E - Surprise me!(虚树+莫比乌斯反演)
Codeforces 题目传送门 & 洛谷题目传送门 1A,就 nm 爽( 首先此题一个很棘手的地方在于贡献的计算式中涉及 \(\varphi(a_ia_j)\),而这东西与 \(i,j\) ...
- FVCOM编译过程详解
本文目的旨在介绍fvcom编译的全过程,顺便介绍linux中make命令的文件写法和一般的编程过程简述一下. 1.编程过程 编程,一般就是编写可执行程序过程.这个过程主要是源文件生成中间代码文件,再到 ...
- CSS浮动效果
#div1{ background-color: yellow; width: 150px; height:150px; position: absolute; top:150px; left: 15 ...
- R绘图布局包 customLayout
今天介绍一个R画图布局的包,地址如下: https://github.com/zzawadz/customLayout https://www.customlayout.zstat.pl/index. ...
- 15.Pow(x, n)
Pow(x, n) Total Accepted: 88351 Total Submissions: 317095 Difficulty: Medium Implement pow(x, n). 思路 ...
- xmake v2.6.1 发布,使用 Lua5.4 运行时,Rust 和 C++ 混合编译支持
xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...
- JS去除对象或数组中的空值('',null,undefined,[],{})
javascript去掉对象或数组中的'',null,undefined,[],{}.思路就是创建一个新的空对象,然后对传入的对象进行遍历,只把符合条件的属性返回,保留有效值,然后就相当于把空值去掉了 ...