• This repository has been archived on 09/Jun/2022
  • Stars
    star
    1,186
  • Rank 39,441 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

FTScroller is a cross-browser Javascript/CSS library to allow touch, mouse or scrollwheel scrolling within specified elements, with pagination, snapping and bouncing support.

FTScroller

FTScroller is a library for adding momentum scrolling to web content on devices with a touch interface, compatible across most modern platforms including desktop browsers. Although recently support for overflow: scroll (or touch equivalents) has increased, this is often still not implemented in a cross-platform or backwards-compatible way, and with no support for features like snapping.

FTScroller is developed by FT Labs, part of the Financial Times. It is inspired by Touchscroll and Zynga Scroller, but is a complete rewrite. It is extensively used in the FT Web App, and was developed to achieve better performance and compatibility, including mouse and touch input.

Usage

Include ftscroller.js in your JavaScript bundle or add it to your HTML page like this:

<script type='text/javascript' src='/path/to/ftscroller.js'></script>

The script must be loaded prior to instantiating a scroller on any element of the page.

To create a scroller, with a few minimal options:

var containerElement, scroller;

containerElement = document.getElementById('scrollcontainer');

scroller = new FTScroller(containerElement, {
	scrollbars: false,
	scrollingX: false
});

Examples

FTScroller is designed to accommodate a range of use cases. Here are some examples - feel free to copy the code and use as the basis for your own projects.

Options

Options must be specified at create-time by passing a JSON object as the second argument to the FTScroller constructor.

  • alwaysScroll Whether to always enable scrolling, even if the content of the scroller is not large enough to spill out of the container. This makes the scroller behave more like an element set to "overflow: scroll", with bouncing always occurring if enabled (boolean, default false)
  • baseAlignments Determines where to anchor the content when the scroller is set up, specified individually for each axis using a JSON object with the keys x and y. Valid alignments for each axis are -1 (top or left), 0 (center), and 1 (bottom or right). For example, the default baseAlignments of {x:-1,y:-1} will display the scroller initially scrolled to the top left of its range. This also affects the alignment of content where the content is smaller than the scroller.
  • bouncing Allow scroll bouncing and elasticity near the ends of the range and at snap points (boolean, default true)
  • contentWidth Define the scrollable width; if not defined, this will match the content width (numeric, default undefined)
  • contentHeight Define the scrollable height; if not defined, this will match the content height (numeric, default undefined)
  • disabledInputMethods Define any input methods to disable; on some multi-input devices custom behaviour may be desired for some scrollers. No inputs methods are disabled by default. (object, default { mouse: false, touch: false, scroll: false, pointer: false, focus: false })
  • enableRequestAnimationFrameSupport FTScroller will use requestAnimationFrame on platforms which support it, which is highly recommended; however this can result in the animation being a further half-frame behind the input method, increasing perceived lag slightly. To disable this, set this property to false. (boolean, default true)
  • flinging Allow a fast scroll to continue with momentum when released (boolean, default true)
  • hwAccelerationClass FTScroller uses normal translate properties rather than translate3d to position content when scrolling, and triggers hardware acceleration by adding CSS properties (specifically backface-visibility) to this class on platforms that support it. Adjusting this class allows for negotiating complex CSS inheritance to override the default behaviour of FTScroller if you want to change or disable backing layers/3D acceleration. (string, default an internal class which triggers backing layers)
  • maxFlingDuration Set the maximum time (ms) that a fling can take to complete once the input has ended (numeric, default 1000ms)
  • scrollbars Whether to display iOS-style scrollbars (which you can style yourself using .ftscroller_scrollbar and .ftscroller_scrollbarx/.ftscroller_scrollbary) while the content is animating (boolean, default true)
  • scrollBoundary The initial movement required to trigger a full scroll, in pixels; this is the point at which the scroll is exclusive to this particular FTScroller instance and flings become active (integer, default 1)
  • scrollingClassName The classname to add to the scroller container when it is being actively scrolled. This is disabled by default as it can cause a CSS relayout if enabled, but allows custom styling in response to scrolls (string, default not set)
  • scrollResponseBoundary The initial movement required to trigger a visual scroll update, in pixels (integer, default 1)
  • scrollingX Enable scrolling on the X axis if content is available (boolean, default true)
  • scrollingY Enable scrolling on the Y axis if content is available (boolean, default true)
  • singlePageScrolls (was paginatedSnap) If snapping is enabled, restricts each scroll movement to one 'page' span. That is, if set to true, it will not be possible to move more than one page in a single movement. (boolean, default false)
  • snapping Enable snapping of content to defined 'pages' or segments (boolean, default false)
  • snapSizeX Define the horizontal interval content should snap to, in pixels. If this is not set, snapping will be based on pages corresponding to the container size. (numeric, default undefined)
  • snapSizeY Define the vertical interval content should snap to, in pixels. If this is not set, snapping will be based on pages corresponding to the container size. (numeric, default undefined)
  • updateOnChanges Automatically detect changes to the content of the scrollable element and update the scroller dimensions whenever the content changes. This is set to false automatically if contentWidth and contentHeight are specified (boolean, default true)
  • updateOnWindowResize Automatically catch changes to the window size and update the dimensions of the scroller. It's advisable to set this to true if the scroller has a flexible width or height based on the viewport size. (boolean, default false)
  • windowScrollingActiveFlag Whether to use a global property of the window object to control whether to allow scrolling to start or not. If the specified window property is set to a truthy value, the scroller will not react to input. If the property is not truthy, the scroller will set it to itself and will scroll. Where multiple scrollers exist on the same page, this ensures that only one can be used at a time, which is particularly useful for nested scrollers (see Multiple vertical scrollers in a horizontally paged scroller). Note that FTScroller automatically allows only one scroller instance to be scrolled at once; use this flag to coordinate input with other parts of your code. (string, default not set)
  • flingBezier The bezier curve to use for momentum-like flings. (CubicBezier, default CubicBezier(0.103, 0.389, 0.307, 0.966))
  • bounceDecelerationBezier The bezier curve to use for deceleration when a fling hits the bounds. (CubicBezier, default CubicBezier(0, 0.5, 0.5, 1))
  • bounceBezier The bezier curve to use for bouncebacks when the scroll exceeds the bounds. (CubicBezier, default CubicBezier(0.7, 0, 0.9, 0.6))
  • invertScrollWheel If the scroller is constrained to an x axis, convert y scroll to allow single-axis scroll wheels to scroll constrained content. (boolean, default true)

Public interface

Once the scroller has been applied to an element, the return value from the constructor is an object that offers a number of public properties, methods and events.

Properties

  • scrollHeight Gets the scrollable height of the contained content. Read only
  • scrollLeft Gets or sets the current left scroll offset of the scroller in pixels. When set, will cause the scroller to jump to that position (without animating)
  • scrollTop Gets or sets the current top scroll offset of the scroller in pixels. When set, will cause the scroller to jump to that position (without animating)
  • scrollWidth Gets the scrollable width of the contained content. Read only
  • segmentCount When snapping is enabled, returns the number of snap segments (which in many use cases can be considered 'pages'). Read only.
  • currentSegment Returns the index of the current segment, starting from 0. Updated when the scroller comes to rest on a new segment. Applies only when snapping is set to true. Read only.
  • contentContainerNode Returns the DOM node that contains the scroller contents, for use if you want to amend the contents. Read only.

Methods

  • addEventListener(eventname, callback) Attaches a specified function to an FTScroller custom event. Available events are listed in the events section below. The function will be called by FTScroller when the event occurs.
  • destroy(removeElements) Unbinds all event listeners to prevent circular references preventing items from being deallocated, and clean up references to dom elements. Does not remove scroller markup from the DOM unless the optional removeElements argument is set to a truthy value.
  • removeEventListener(eventname, callback) Removes a previously bound function from an event. The function specified in the mathod must be the same object reference passed in the addEventListener call (not a redefinition of the same function code)
  • scrollBy(horizontal, vertical[, duration]) Similar to scrollTo but allows scrolling relative to the current position rather than to an absolute offset.
  • scrollTo(left, top[, duration]) Scroll to a specified left and top offet over a specified duration. If duration is zero, no animation will occur. If duration is true FTScroller will itself choose a duration based on the distance to be scrolled. The left and top inputs will be constrained by the size of the content and the snap points. If false is supplied for either left or top, that axis will not be scrolled and will retain its current offset.
  • setSnapSize(width, height) Configures the snapping boundaries within the scrolling element if snapping is active. If this is never called, segment size defaults to the width and height of the scroller, ie. page-at-a-time.
  • updateDimensions(width, height[, nosnap]) Sets the dimensions of the scrollable content. If snapping is enabled, and you wish to disable updates of the snapping grid and prevent the current position from being updated, set nosnap to true; it defaults to false if not supplied.
  • setDisabledInputMethods(disabledInputMethods) Set the input methods to disable. No inputs methods are disabled by default. (object, default { mouse: false, touch: false, scroll: false, pointer: false, focus: false })

Prototype methods

  • getPrependedHTML([excludeXAxis, excludeYAxis, hwAccelerationClass]) - Provides half of the HTML that is used to construct the scroller DOM, for use to save a DOM manipulation on Scroller initialisation (see Tips and tricks below). Optionally the x and y axes can be excluded, or a custom layer backing triggering class can be supplied (see the hwAccelerationClass option for the constructor).
  • getAppendedHTML([excludeXAxis, excludeYAxis, hwAccelerationClass, scrollbars]) - Provides the second half of the HTML that is used to construct the scroller DOM, for use to save a DOM manipulation on Scroller initialisation (see Tips and tricks below). Optionally the x and y axes can be excluded, or a custom layer backing triggering class can be supplied (see the hwAccelerationClass option for the constructor). Pass a truthy value in for the scrollbars parameter if you are enabling scrolling. Any parameters should match those passed in to getPrependedHTML.

Events

Events can be bound with the addEventListener method. Events are fired syncronously. Regardless of the event, the listener function will receive a single argument.

  • scroll Fired whenever the scroll position changes, continuously if an input type is modifying the position. Passes an object containing scrollLeft and scrollTop properties matching the new scroll position, eg {scrollLeft:10, scrollTop:45}
  • scrollstart Fired when a scroll movement starts. Passes an object with the same characteristics as scroll.
  • scrollend Fired when a scroll movement ends. Passes an object with the same characteristics as scroll.
  • segmentdidchange Fires on completion of a scroll movement, if the scroller is on a different segment to the one it was on at the start of the movement. Passes an object with segmentX and segmentY properties.
  • segmentwillchange Fires as soon as the scroll position crosses a segment boundary, during a scroll movement. Passes an object with segmentX and segmentY properties.
  • reachedstart Fires when the scroll position reaches the top or left of a scroller. Passes an object with an axis property indicating whether the x or y axis reached its start position.
  • reachedend Fires when the scroll position reaches the bottom or right of a scroller. Passes an object with an axis property indicating whether the x or y axis reached its end position.

Compatibility

FTScroller supports input via mouse or touch on the following browsers:

  • Apple Mobile Safari (iOS 3.2+ confirmed, best after 4.0+)
  • Google Android Browser (Android 2.2+ confirmed)
  • Microsoft Internet Explorer 10 (Windows 8 RTM confirmed)
  • RIM BlackBerry PlayBook (2.0 confirmed, 1.0 should be fine)
  • Most modern desktop browsers (IE 9+, FF 4+, Safari 3+, Chrome 1+, Opera 11.5+ - ECMAScript 5 getters and setters are currently used)

Tips and tricks

  • If you are putting together the DOM in JavaScript using HTML or a template, it's advantageous to use the prototype methods for getPrependedHTML and getAppendedHTML to add the FTScroller HTML at the same time. If the elements are added to the very start and very end of the content within the element passed in to the constructor (ignoring whitespace), the Scroller instantiation will detect this and skip the DOM manipulation step, leading to faster instantiation without an additional layout.
  • If scrolling is going to only occur along one axis, setting scrollingX or scrollingY to false as part of the construction options skips creating of the corresponding DOM element, saving memory (particularly when using hardware acceleration).
  • Depending on your CSS, you may find that you can't scroll quite to the bottom of your content. This is typically a collapsing margin issue — the contents are measured, then wrapped in additional elements, but the margins spilling out of the contents can't be measured. If you see this, an easy fix is typically to put overflow: hidden either your scrollable element or its contents; that will act as a boundary for margins so everything can be measured, and typically has no effect on appearance when applied to the content as the scrollable content won't have dimensional limits.

Credits and collaboration

The lead developer of FTScroller is Rowan Beentje at FT Labs. All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request. Enjoy.

More Repositories

1

fastclick

Polyfill to remove click delays on browsers with touch UIs
HTML
18,668
star
2

ftcolumnflow

A polyfill that fixes the inadequacies of CSS column layouts
JavaScript
633
star
3

ftellipsis

Multi-line ellipsis made possible
JavaScript
486
star
4

fruitmachine

View rendering engine
JavaScript
247
star
5

transcription-service

A Node.js server that accepts audio/video files and transcribes the content
JavaScript
61
star
6

ftdatasquasher

Data compression and decompression support, packing base64 into UTF8 high and low bytes
JavaScript
39
star
7

big-ft

A view of the FT homepage designed for non-interactive video walls and billboards
JavaScript
23
star
8

perf-widget

JavaScript
22
star
9

Hancock

A Node.js module for determining a similarity between handwritten signatures.
JavaScript
17
star
10

text-summarization-experiment

Experiment on text summarization techniques and exploring Tensorflow.
Jupyter Notebook
15
star
11

three-sixty-video

Experiment with converting 360videos to VR.
JavaScript
14
star
12

screens

A way to distribute websites to multiple displays
JavaScript
12
star
13

single-frame-video

Simple service to convert mp3s to mp4s with images for social networks
Handlebars
8
star
14

alignment

exploring Go, aligning some text, looking for poetry, for faux-artistic reasons
Roff
8
star
15

tech-radar

Generate tech-radars based off of Google Spreadsheets
JavaScript
7
star
16

autovoice-podcast

take in an rss feed of articles, return a podcast of those articles with automated voices
JavaScript
7
star
17

inklink

Image recognition tool
JavaScript
6
star
18

email-signatures

A Chrome Extension that automagically adds links from RSS feeds to the end of emails sent in the Gmail web client
JavaScript
6
star
19

Signature-Signatures

A small exploration to try to match handwritten signatures by generating a fingerprint out of the number of black pixels along the Y Axis of the image
JavaScript
6
star
20

fruitmachine-media

FruitMachine helper that allows different setup and teardown callbacks to be called based on media queries
JavaScript
6
star
21

googlehome

Experimenting with what is possible with via voice commands on the Google Home
JavaScript
4
star
22

textrequireify

Browserify transform that includes text files using `requireText('module/filename')` syntax.
JavaScript
4
star
23

gpt2-gui

GPT-2 prototype/investigation
Python
3
star
24

Screens-Chrome-App

A kiosk-enabled app for Chrome to use with FT Labs Screens project
JavaScript
3
star
25

chatbot

An FT bot
JavaScript
3
star
26

o-three-sixty

Three Sixty Video Player powered by Brightcove
JavaScript
3
star
27

overview

Exploration(s) of summarising and displaying news
JavaScript
3
star
28

cite-n-snap

JavaScript
3
star
29

ftlabs.github.io

porting the team blog to github pages
HTML
2
star
30

idiom-explorer

whimsical investigation into use of idioms in news articles
JavaScript
2
star
31

suggestr

Recommendation algorithm(s)
JavaScript
2
star
32

Listen-to-the-FT

Make podcasts great again.
JavaScript
2
star
33

correlations-game-test

Trying out Alexa testing via Mocha
JavaScript
2
star
34

fruitmachine-ftdomdelegate

An ftdomdelegate plugin for FruitMachine
JavaScript
2
star
35

audio-search

Want to find audio content on the FT? No problem!
JavaScript
2
star
36

Twitter-digest

Chrome extension for Twitter filtering
JavaScript
2
star
37

webtorrent-audio

Where possible, deliver audio files to clients using WebTorrent
JavaScript
2
star
38

fruitmachine-bindall

A FruitMachine helper that binds all the methods in FruitMachine modules to each instance
JavaScript
2
star
39

barcode

An API endpoint to gather all of the main images used in FT articles from a provided date range, the last 24 hours for example, and squash them (width wise) to give one condensed image that represents the news. The final result looks similar to a coloured barcode.
JavaScript
2
star
40

ft-quiz

Automated quiz api for news in specified date range.
JavaScript
1
star
41

key-fetch

Get keys for your project
JavaScript
1
star
42

ft-dash

A minimal view of FT content designed for as a HUD for car dashboards
JavaScript
1
star
43

Absorber

The (newly separated) absorber component of the Ingestion service.
JavaScript
1
star
44

PPS

Product Project Summariser
JavaScript
1
star
45

left

Handlebars
1
star
46

inshort

JavaScript
1
star
47

email-signatures-server

JavaScript
1
star
48

summarize

1
star
49

assistant-authenticator

A service for ephemerally linking an FT user to a Google Home device
JavaScript
1
star
50

fruitmachine-fastdom

A fastdom helper for FruitMachine
JavaScript
1
star
51

touchbar-news

It puts the news on your shiny Macbook's Touch Bar
JavaScript
1
star
52

correlations-game

A Node.js app for playing the FT Labs Correlation game
HTML
1
star
53

Interrogable-Articles-Demo

A PWA to demonstrate the concept of 'interrogable' articles
JavaScript
1
star
54

audio-available

A small Node.js app for checking whether or not an audio version of an FT article exists.
JavaScript
1
star
55

correlations-service

exploring a node version of slurp which was a ruby version of a graph db
JavaScript
1
star
56

time-to-read

A lambda that provides the reading time from a text and image input.
JavaScript
1
star
57

FT-Labs-AWS-Archives

A PoC for having AWS OCR the entire FT Digital Archive
JavaScript
1
star
58

Falcon-Snippets

Content scripts for manipulating FT content on the Falon ft.com site
JavaScript
1
star
59

chars-to-wav

Letters in, sounds out
JavaScript
1
star
60

audio-management

A place for managing audio assets at the FT
Handlebars
1
star
61

js-abbreviate

Takes *any* JavaScript object and returns a JSON-serializable finite-sized copy of it.
JavaScript
1
star
62

order-order

A swiss army knife of various forms of structured comments.
JavaScript
1
star
63

pages

experiments with simple static pages
JavaScript
1
star
64

fruitmachine-boundarize

FruitMachine helper to isolate components from the rest of the DOM tree
JavaScript
1
star
65

boilerplates

some simple, no-nonsense examples of things
HTML
1
star