Giter Site home page Giter Site logo

ustbhuangyi / better-scroll Goto Github PK

View Code? Open in Web Editor NEW
16.4K 259.0 2.6K 18.24 MB

:scroll: inspired by iscroll, and it supports more features and has a better scroll perfermance

Home Page: https://better-scroll.github.io/docs/en-US/

License: MIT License

JavaScript 4.70% Vue 21.76% CSS 2.43% HTML 0.32% Shell 0.05% TypeScript 66.20% Stylus 4.53%
iscroll mobile-web better-performance ios vue more-features

better-scroll's Introduction

better-scroll

npm version downloads Build Status Package Quality codecov.io Gitpod Ready-to-Code

中文文档

1.x Docs

2.x Docs

2.x Demo

Note: 1.x is not maintained. please migrate your version as soon as possible

Install

npm install better-scroll -S # install 2.x,with full-featured plugin.

npm install @better-scroll/core # only CoreScroll
import BetterScroll from 'better-scroll'

let bs = new BetterScroll('.wrapper', {
  movable: true,
  zoom: true
})

import BScroll from '@better-scroll/core'

let bs = new BScroll('.wrapper', {})

CDN

BetterScroll with full-featured plugin.

<script src="https://unpkg.com/better-scroll@latest/dist/better-scroll.js"></script>

<!-- minify -->
<script src="https://unpkg.com/better-scroll@latest/dist/better-scroll.min.js"></script>
let wrapper = document.getElementById("wrapper")
let bs = BetterScroll.createBScroll(wrapper, {})

Only CoreScroll

<script src="https://unpkg.com/@better-scroll/core@latest/dist/core.js"></script>

<!-- minify -->
<script src="https://unpkg.com/@better-scroll/core@latest/dist/core.min.js"></script>
let wrapper = document.getElementById("wrapper")
let bs = new BScroll(wrapper, {})

What is BetterScroll ?

BetterScroll is a plugin which is aimed at solving scrolling circumstances on the mobile side (PC supported already). The core is inspired by the implementation of iscroll, so the APIs of BetterScroll are compatible with iscroll on the whole. What's more, BetterScroll also extends some features and optimizes for performance based on iscroll.

BetterScroll is implemented with plain JavaScript, which means it's dependency free.

Getting started

The most common application scenario of BetterScroll is list scrolling. Let's see its HTML:

<div class="wrapper">
  <ul class="content">
    <li>...</li>
    <li>...</li>
    ...
  </ul>
  <!-- you can put some other DOMs here, it won't affect the scrolling
</div>

In the code above, BetterScroll is applied to the outer wrapper container, and the scrolling part is content element. Pay attention that BetterScroll handles the scroll of the first child element (content) of the container (wrapper) by default, which means other elements will be ignored. However, for BetterScroll v2.0.4, content can be specified through the specifiedIndexAsContent option. Please refer to the docs for details.

The simplest initialization code is as follow:

import BScroll from '@better-scroll/core'
let wrapper = document.querySelector('.wrapper')
let scroll = new BScroll(wrapper)

BetterScroll provides a class whose first parameter is a plain DOM object when instantiated. Certainly, BetterScroll inside would try to use querySelector to get the DOM object.

The principle of scrolling

Many developers have used BetterScroll, but the most common problem they have met is:

I have initiated BetterScroll, but the content can't scroll.

The phenomenon is 'the content can't scroll' and we need to figure out the root cause. Before that, let's take a look at the browser's scrolling principle: everyone can see the browser's scroll bar. When the height of the page content exceeds the viewport height, the vertical scroll bar will appear; When the width of page content exceeds the viewport width, the horizontal bar will appear. That is to say, when the viewport can't display all the content, the browser would guide the user to scroll the screen with scroll bar to see the rest of content.

The principle of BetterScroll is samed as the browser. We can feel about this more obviously using a picture:

布局

The green part is the wrapper, also known as the parent container, which has fixed height. The yellow part is the content, which is the first child element of the parent container and whose height would grow with the size of its content. Then, when the height of the content doesn't exceed the height of the parent container, the content would not scroll. Once exceeded, the content can be scrolled. That is the principle of BetterScroll.

Plugins

Enhance the ability of BetterScroll core scroll through plugins, such as

import BScroll from '@better-scroll/core'
import PullUp from '@better-scroll/pull-up'

let bs = new BScroll('.wrapper', {
  pullUpLoad: true
})

Please see for details, Plugins.

Using BetterScroll with MVVM frameworks

I wrote an article When BetterScroll meets Vue (in Chinese). I also hope that developers can contribute to share the experience of using BetterScroll with other frameworks.

A fantastic mobile ui lib implement by Vue: cube-ui

Contributing

Online one-click setup

You can use Gitpod(An Online Open Source VS Code like IDE which is free for Open Source) for contributing. With a single click it will launch a workspace and automatically:

  • clone the better-scroll repo.
  • install all of the dependencies.
  • run yarn vue:dev,
  • run yarn docs:build and yarn docs:dev.

Open in Gitpod

Using BetterScroll in the real project

If you want to learn how to use BetterScroll in the real project,you can learn my two practical courses(in Chinese)。

High imitating starvation takeout practical course base on Vue.js

Project demo address

QR Code

Music App advanced practical course base on Vue.js

Project demo address

QR Code

better-scroll's People

Contributors

aladdin-add avatar amyfoxfn avatar commanderxl avatar cuijing1031 avatar cym2050 avatar dolymood avatar hevgin avatar hezhongfeng avatar hu0950 avatar hubvue avatar ilexwg avatar iulo avatar kentonyu avatar linxiaowu66 avatar maomao1996 avatar minikey avatar mondogao avatar ouxuwen avatar peterschmidler avatar rubyyc avatar stonechen avatar taoqf avatar theniceangel avatar ustbhuangyi avatar wind-stone avatar wjq990112 avatar wn7067509 avatar wrxpowered avatar ytudt avatar yuler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

better-scroll's Issues

横向滚动问题

首先非常感谢老师的better-scroll库,它的表现非常棒!

最近测试了下,发现需要横向滚动的话,option的设置必须为scrollY: false而且scrollX: true.缺一不可.文档中没有明确说明,可能会导致不必要的调试困难.希望这里能给大家提个醒.

vue.js2.0

Hello, is your better-scroll is not supported by vue.js2.0? I file a report on the wrong, can not find name

不放在第一个tab时滚动失效Bug

image
通过这个tab控件加载插件分类组件,分类组件没有滚动效果,如果直接加载这个界面或者跳转到这个界面就 可以滚动
这个该如何设置啊?
mint-ui+vue2.0

image

正常a标签跳转会产生2次请求,第一次canceled

chrome中调试, initlator 显示行号1328

function click(e) {
    var target = e.target;
    if (!/(SELECT|INPUT|TEXTAREA)/i.test(target.tagName)) {
	 var ev = document.createEvent(window.MouseEvent ? 'MouseEvents' : 'Event');
	 ev.initEvent('click', true, true);
	 ev._constructed = true;
	 target.dispatchEvent(ev);
    }
};

wheel 参数启用bug

如下两张图,加了wheel:true后,不仅无效,界面上出现元素被加上rotateX属性
true

false

在Vue中使用时

当页面刷新时无法滚动,但是若修改JS代码后就可以滚动,请问一下这个怎么解决呢?是否是因为Vue生命周期的问题

设置了wheel:true后点击事件失效

黄老师你好,我现在试用那个wheel:true,设置了之后,点击事件就不能触发,但滚动时,会按li的高度,刚刚到滚动对应的位置。而去掉wheel:true后,点击事件就能触发,现在不知道为什么会出现这种情况,希望有试过的同学看一下,谢谢。现在做的效果就像滴滴打车网页版的,那个时间选择控件一样

<div class="sel-wrapper">
        <div class="sel-div" id="sel-div">
            <ul>
                <li>1座位</li>
                <li>2座位</li>
                <li>3座位</li>
                <li>4座位</li>
                <li>5座位</li>
                <li>6座位</li>
                <li>7座位</li>
                <li>8座位</li>
                <li onclick="aa(this)">9座位</li>
            </ul>
        </div>
    </div>

<script>
var mySelBscroll = new BScroll(document.getElementById("sel-div"),{
    wheel: true, //picker组件要配置
    preventDefault: false,
    selectedIndex: 3, //默认选中的索引
    rotate: 0, //每一层旋转角度
    click: true, //可点击
});
$(document).on('click',".my-sel .sel-wrapper .sel-div ul li",function(){
    if(!event._constructed){//如果不存在这个属性,则不执行下面的函数
        return;
    }
    console.log('点击了');
    mySelBscroll.scrollToElement($(this)[0], 300, 0, -72);
    //console.log(mySelBscroll.selectedIndex);
});
<script>

实例化后无法滚动

this.$nextTick(() => { if (!this.cScroll) { this.cScroll = new BetterScroll(this.$refs.content, { click: true }); } else { this.cScroll.refresh(); } });

在目标dom有
style="transition-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1); transition-duration: 0ms; transform: translate(0px, 0px) translateZ(0px);"

但是无法滚动,不知道什么原因。

怎么开启横向滚动?

我写了一个div下面是ul li

li是横向排列的 div宽度100%,怎么当li标签过多时超出div的宽度可以横向滚动?而不是往下掉

details.vue有少少问题

在瀑布流每次打开详情页的时候,详情页容器还没等图片加载完就new BScroll(_)导致高度不准确,页面最后面有一些内容滑动不到,希望修复一下,哈哈;

better-scroll can not touch bottom

I have refresh the better-scroll in vue2, but when div'height changed . XXX.scroll.scroller.clientHeight has change, but I can not touch the div bottom. Can you help me , thanks!!

关于better-scroll计算高度的问题

#遇到的问题
页面第一次加载进来better-scroll 计算不到图片的高度导致页面拉到一半就卡住了,再刷新一次就正常。(ps:图片是一张高度好大的商品详情图片)
初始化better-scroll 已经是在
mounted () {
this.$nextTick(() => {
this._initScroll();
});}
请问老师有什么办法可以解决吗?谢谢!!!

刷新后无法滚动的问题

在created函数里new 了Bscroll ,是在nextTick中运行的 但是刷新后无法滚动,把BScroll打印出来发现,刷新后scrollerHeight 的高度和外层容器的高度是一样的,因此无法滚动(实际上内层元素比外层容器要高)。不知道为啥会出现这样的问题,求大大指点

黄老师,我做外卖app用better-scroll的时候遇到一个是否需要阻止默认事件的问题,期望您能帮我解惑

最近在看那个外卖app视频学vue,我发现里边的加减按钮的那个组件里边,为了防止在pc端触发两次,里边用event._constructed做了一个判断,如果不是派发的点击事件就直接return掉,但是这样的话,如果这个组件没有用在better-scroll上的话,点击事件没办法触发,没有想到什么好的解决办法,所以我就将preventDefault改为false了,把派发的click事件去掉了,测了测是没有问题的,我想问问您这样做会不会有什么隐藏的隐患,为什么初始化的时候要将默认事件阻止掉,通过派发来实现点击事件(前端小白一枚,问的问题可能不太成熟,您勿怪)

黄老师,能否给我详细解释下这个momentum方法是干啥的,尤其是里面这些算法为得出最后的结果起到了啥作用~

function momentum(current, start, time, lowerMargin, wrapperSize, options) {
let distance = current - start;
let speed = Math.abs(distance) / time;

let {deceleration, itemHeight, swipeBounceTime, bounceTime} = options;
let duration = options.swipeTime;
let rate = options.wheel ? 4 : 15;

let destination = current + speed / deceleration * (distance < 0 ? -1 : 1);

if (options.wheel && itemHeight) {
	destination = Math.round(destination / itemHeight) * itemHeight;
}

if (destination < lowerMargin) {
	destination = wrapperSize ? lowerMargin - (wrapperSize / rate * speed) : lowerMargin;
	duration = swipeBounceTime - bounceTime;
} else if (destination > 0) {
	destination = wrapperSize ? wrapperSize / rate * speed : 0;
	duration = swipeBounceTime - bounceTime;
}

return {
	destination: Math.round(destination),
	duration
};

};

绑定时机问题

感谢老师这么好用的插件,提一个issue,当父节点display为none的时候,此时给其子节点绑定BScroll,把父节点display为block后,滚动不生效。必须在父节点display不为none的时候,绑定BScroll才有效。

使用better-scroll影响v-if、v-show

Bug出现场景:

btn
content

mounted() {
this.initScroll();
},
methods: {
initScroll() {
new BScroll(document.getElementById('box'), {});
},
toggle(){
this.show =! this.show;
}
}

以上代码,使用better-scroll影响v-if、v-show,PC浏览器可以,手机上切换失效

报错信息:
Ignored attempt to cancel a touchend event with cancelable=false, for example because scrolling is in progress and cannot be interrupted

已经解决:
new BScroll(document.getElementById('user'), {
click:true
});

滚动区域含有input元素,点击之后无法失焦

我的better-scroll滚动区域中有input元素,在点击input输入之后就无法失焦了

(用的最新版本的better-scroll -- v 0.1.10 ,, 测试机:只有安卓上有这个问题,ios没问题的)

请问这个能如何解决呢?

使用better-scroll影响@click

使用better-scroll影响Vue2.0的点击事件@click
问题:scroll容器内的点击事件,在使用了this.$nextTick()的情况下,第1次点击执行1次,第2次点击执行2次,第3次点击执行3次...

加上如下代码:
if(event._constructed){
return;
};
PC端正常,手机端直接失效了。。。

在绑定了better-scroll的div上点击会触发两次vue的click事件

var eventType = exports.eventType = {
	  touchstart: TOUCH_EVENT,
	  touchmove: TOUCH_EVENT,
	  touchend: TOUCH_EVENT,

	  mousedown: MOUSE_EVENT,
	  mousemove: MOUSE_EVENT,
	  mouseup: MOUSE_EVENT
};

跟踪了一下,还发现这里少了一些比如 touchcancel 的事件,不知道跟上述问题有没有关系

better-scroll里面的refresh功能该如何使用

老师,我看了您的饿了么视频。视频里面您在初始化生成一个bs实例后,通过实例是否存在,而选择执行bs.refresh()方法。但是实际上并没有什么效果。但是看你的这个readme文档,上面说的是refresh是一个事件。那应该如何才能执行呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.