• Stars
    star
    276
  • Rank 144,196 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

ES6/ES2015 HW accelerated scrollable images parallax

Build Status NPM version NPM downloads MIT License Coverage Status Code Quality

Why

Oh no, why another parallax script? Do we really need it?

There are many parallax scripts but none of them was satisfying my personal needs:

  • No dependencies
  • No background positioning and heavy obtrusive DOM manipulations
  • Build only for modern devices without internal hacks
  • Modern and flexible api being thought mainly for ajax applications
  • Modern and clean ES6/2015 source code

So I decided to make my own and you can be free to use it or simply ignore it and move forward to the next one!

Demos

Usage

Installation

$ npm install scroll-parallax --save
# or
$ bower install scroll-parallax --save

Markup and initialization

Once you have included the script in your page, you should wrap your parallax elements in a wrapper having an height, position:relative or absolute and overflow: hidden The elements will be stretched to fit always the whole wrapper size

<figure style="position: relative; height: 300px; overflow: hidden;">
  <img class="parallax" src="path/to/the/image.jpg" />
</figure>

<figure style="position: relative; height: 300px; overflow: hidden;">
  <video class="parallax" src="path/to/the/video.mp4" />
</figure>

The Parallax api is really simple and the following snippet should be enough:

var p = new Parallax('.parallax').init()

Options

The options available are only 4 at moment:

Type Name Default Value Description
Number offsetYBounds 50 the offset top and bottom boundaries in pixels used by the parallax to consider an element in the viewport
Number intensity 30 the intensity of the parallax effect
Number center 0.5 the vertical center of the parallax. If you increase this value the element will be centered more on the top of the screen reducing it will look centered at bottom this value should be between 0 and 1
Number safeHeight 0.15 the safe element height gap value in percentage that ensures it can always properly parallax. Any element should be (by default) at least 15% higher than their DOM wrappers (7.5% bottom + 7.5% top)

You can set the Parallax options in this way:

var p = new Parallax('.parallax', {
  offsetYBounds: 50,
  intensity: 30,
  center: 0.5,
  safeHeight: 0.15
}).init()

Each element could be configured using custom Parallax options (except for the offsetYBounds) overriding the defaults:

<figure>
  <img class="parallax" data-center="0.8" data-intensity="50" src="path/to/the/image.jpg" />
</figure>
<figure>
  <video class="parallax" data-center="0.2" data-intensity="10" data-safe-height="0.2" src="path/to/the/video.mp4" />
</figure>

API

Each Parallax instance has some useful methods that could be used to adapt it to your application needs

Parallax.init

Initialize the parallax internal event listeners. The listeners to element:loaded and elements:loaded should be set before this method gets called

Parallax.on

The on method allows you to listen the internal Parallax events from the outside.
Currently it supports:

  • element:loaded: when a parallax elment gets completely loaded
  • elements:loaded: when all the elements to parallax get loaded
  • draw: when a parallax element becomes visible in the the viewport and starts getting moved
  • resize: when the window will be resized
  • update: when the page is scrolling and the script has updated all the visible elemens
p.on('element:loaded', function(element){
  // do something with the element
})

Parallax.off

Stop listening an internal Parallax event

var fn = function (element) {
    // do something with the elemnt just drawn
    p.off('draw', fn) // stop listening the draw event
  }
p.on('draw', fn)

Parallax.refresh

Refresh the position of the elements visible in the viewport

// do extremely heavy dom updates
p.refresh()

Parallax.add

Add new element to the parallax instance

// inject new elements
p.add('.parallax-2')

Parallax.remove

Remove element from the parallax instance

p.remove('.parallax-2') // remove the element from the parallax
// and also from the DOM...

Parallax.destroy

Destroy the parallax instance removing all the internal and external callbacks to its internal events

p.destroy() // the parallax is dead!

Contributing

Available tasks

Build and test

$ ./make # or also `$ npm run default`

Convert the ES6 code into valid ES5 combining all the modules into one single file

$ ./make build # or also `$ npm run build`

Run all the tests

$ ./make test # or also `$ npm run test`

Start a nodejs static server

$ ./make serve # or also `$ npm run serve`

To compile and/or test the project anytime a file gets changed

$ ./make watch # or also `$ npm run watch`

More Repositories

1

allora

Promisify using es6 Proxies every javascript API with less than 50 lines of code
JavaScript
679
star
2

Tocca.js

Super lightweight script (~1kb) to detect via Javascript events like 'tap' 'dbltap' 'swipeup' 'swipedown' 'swipeleft' 'swiperight' on any kind of device.
JavaScript
659
star
3

Vague.js

Vague.js is an experimental script that allows you to blur any kind of html element thanks to the SVG filters
JavaScript
604
star
4

icaro

Smart and efficient javascript object observer, ideal for batching DOM updates (~1kb)
JavaScript
572
star
5

es6-project-starter-kit

Universal Starter Kit to build any javascript ES6 project/library runnable in nodejs and on any browser.
JavaScript
460
star
6

jQuery.BlackAndWhite

Clientside grayscale images on any browser
JavaScript
311
star
7

jquery.html5loader

jQuery.html5Loader can preload images, SVGs, html5 video and audio sources, scripts, css, and text files.
JavaScript
302
star
8

go-observable

It allows you to send and receive events with a tiny simple API
Go
240
star
9

nodejs-MySQL-push-notifications-demo

Node server that is able to read from a MySql database and then stream those data via websocket to many client connected on the same page
JavaScript
106
star
10

nodejs-push-notification-server

Simple nodejs push notification server using socket.io
JavaScript
76
star
11

riot-app-example

Simple riotjs isomorphic app
JavaScript
76
star
12

animore

1kb script that will make your DOM state transitions smoother & easier
JavaScript
72
star
13

erre

Modern, performant and tiny (~0.5kb) streams script using generators for browsers and node.js
JavaScript
30
star
14

nodejs-arduino-example

Simple example to show how to use arduino to communicate digital signals to a nodejs server
JavaScript
25
star
15

cmt

Get rid of all the boring git commit messages like 'small fix'. cmt is an automatic git commit generator
JavaScript
18
star
16

site-under-construction

Simple splash page to use while a site is still under construction
CSS
17
star
17

fcf

Monadic Functional Control Flow Micro-Library for Javascript/Typescript
TypeScript
14
star
18

rawth

Pure functional isomorphic router based on streams
JavaScript
14
star
19

fortytwo

List of interactive prompt helpers to pimp the UIs of your haskell programs
Haskell
12
star
20

reactive-libs-bench

Benchmark tests to check some popular reactive libraries - raw tests no DOM involved
JavaScript
11
star
21

days-since-last-javascript-drama

Timer to track how many days passed since the latest Javascript drama
HTML
9
star
22

riot-preset-babel-test

JavaScript
8
star
23

threejs-amd-bootstrap

Simple and lite threejs bootstrap project build using requirejs
JavaScript
7
star
24

nisiu

Password manager written in Riot.js
HTML
6
star
25

i-got-ya

Stop fucking with my machine when I am not here :)
Go
6
star
26

the-cost-of-native-javascript-modules

The performance cost of native javascript modules
HTML
5
star
27

immutable-dom

Lock any HTML DOM Element 🔐
JavaScript
4
star
28

jquery-fitToParent

A jQuery plugin for proportionally scaling an element to its parent element. Useful for thumbnails and avatars.
JavaScript
4
star
29

ruit

Functional tasks serialization mini script (0.3kb)
JavaScript
4
star
30

package-requirejs

Generate a valid requirejs-config.js file automatically detecting all the package.json dependencies
JavaScript
4
star
31

riot-now-demo

Simple now riot component
3
star
32

Caronte.js

Simple javascript ajax file uploader script made for the modern browsers
JavaScript
3
star
33

EasyAnimationFrame

Using EasyAnimationFrame.js you can make html5 canvas or html animations having the complete control over the framerate.
JavaScript
3
star
34

riot-chrome-extension

riojs chrome extension example
JavaScript
3
star
35

rob

Personal projects generator written in haskell
Haskell
3
star
36

smell-of-funtional-programming

Demo code for the "Smell Of Functional Programming" presenation
JavaScript
3
star
37

SublimeText2-Parallel-Builder-Plugin

Sublime Text2 Plugin - It allows you to run more than one build command in Parallel
Python
3
star
38

gianlucaguarini.github.io

gianlucaguarini.github.io original source
HTML
3
star
39

cumpa

Minimal function composition implementation
JavaScript
2
star
40

riot-ssr-now

Simple riot example to demonstrate how to deploy on zeit.co
JavaScript
2
star
41

curri

Minimal curry implementation
JavaScript
2
star
42

mail-client-ui-demo

Simple Mail Client Demo with ~20 Javascript code
JavaScript
2
star
43

grunt-testardo

grunt plugin to test your project using testardo
JavaScript
2
star
44

html5-semantic

Meaningful HTML5 Designs - webzurich - 25.05.2020
2
star
45

the-colour-of-the-moment

Random colored image generator service written with go
Riot
2
star
46

executor

Haskell module to execute single or multiple shell commands
Haskell
1
star
47

amp-vs-pwa-vs-HTML

Simple Benchmark to check the speed of 3 different website types
HTML
1
star
48

the-fly

JavaScript
1
star
49

OSXKeyboard

Go helper script to detect the global keypress events on OSX
C
1
star