Smooth Video & Image Parallax With jQuery and CSS3 – jarallax

Looking for a affordable website?

GET STARTED
www.codingexpertz.com
Smooth Video & Image Parallax With jQuery and CSS3 – jarallax

File Size: 69562KB

Total Views: 1277

Date Created:

Last Modified Date:

Official Website: Go to website

License: MIT

Jarallax is a dependency-free smooth video & image parallax scrolling. It is built with pure JavaScript and CSS3 so you can modify the styling and appearance of the plugin according to your requirements. There are various types of predefined options and you can apply these easily to modify the plugin.

Features:

  • Supports image and video.
  • YouTube, Vimeo and Self-hosted videos parallax supported.
  • Easy to use, no extra effort is needed.
  • Supported on all modern browsers and devices.
  • Works smoothly with CSS3 transitions.
  • Can disable the parallax effect on mobile devices.

Install with npm

npm install jarallax --save
import {
    jarallax,
    jarallaxVideo
} from 'jarallax';

jarallaxVideo();

How to use it:

1. Include the Javascript jarallax.min.js at the bottom of the web page.

<script src="path/to/jarallax.min.js"></script>

2. Include the CSS jarallax.css in the header of the page.

<link rel="stylesheet" href="path/to/jarallax.css">

Or you can alternatively add the CSS to your existing .css file.

.jarallax {
    position: relative;
    z-index: 0;
}
.jarallax > .jarallax-img {
    position: absolute;
    object-fit: cover;
    /* support for plugin https://github.com/bfred-it/object-fit-images */
    font-family: 'object-fit: cover;';
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
}

3. Add the basic HTML to the page.

<!-- Background Image Parallax -->
<div class="jarallax">
    <img class="jarallax-img" src="<background_image_url_here>" alt="">
    Your content here...
</div>

<!-- Background Image Parallax with <picture> tag -->
<div class="jarallax">
    <picture class="jarallax-img">
        <source media="..." srcset="<alternative_background_image_url_here>">
        <img src="<background_image_url_here>" alt="">
    </picture>
    Your content here...
</div>

<!-- Alternate: Background Image Parallax -->
<div class="jarallax" style="background-image: url('<background_image_url_here>');">
    Your content here...
</div>

4. Initialize the plugin and we’re ready to go.

$('.jarallax').jarallax({
   speed: 0.2
});

Background video with data attribute

<!-- Background YouTube Parallax -->
<div class="jarallax" data-jarallax-video="https://www.youtube.com/watch?v=ab0TSkLe-E0">
    Your content here...
</div>

<!-- Background Vimeo Parallax -->
<div class="jarallax" data-jarallax-video="https://vimeo.com/110138539">
    Your content here...
</div>

<!-- Background Self-Hosted Video Parallax -->
<div class="jarallax" data-jarallax-video="mp4:./video/local-video.mp4,webm:./video/local-video.webm,ogv:./video/local-video.ogv">
    Your content here...
</div>

Background video with JavaScript

import { jarallax, jarallaxVideo } from 'jarallax';
jarallaxVideo();

jarallax(document.querySelectorAll('.jarallax'), {
    speed: 0.2,
    videoSrc: 'https://www.youtube.com/watch?v=ab0TSkLe-E0'
});
<div class="jarallax"></div>

Plugin’s default options:

Name Type Default Description
type string scroll scroll, scale, opacity, scroll-opacity, scale-opacity.
speed float 0.5 Parallax effect speed. Provide numbers from -1.0 to 2.0.
imgSrc path null Image url. By default used image from background.
imgElement dom / selector .jarallax-img Image tag that will be used as background.
imgSize string cover Image size. If you use <img> tag for background, you should add object-fit values, else use background-size values.
imgPosition string 50% 50% Image position. If you use <img> tag for background, you should add object-position values, else use background-position values.
imgRepeat string no-repeat Image repeat. Supported only background-position values.
keepImg boolean false Keep <img> tag in it’s default place after Jarallax inited.
elementInViewport dom null Use custom DOM / jQuery element to check if parallax block in viewport. More info here – Issue 13.
zIndex number -100 z-index of parallax container.
disableParallax RegExp / function Disable parallax on specific user agents (using regular expression) or with function return value. The image will be set on the background.
disableVideo RegExp / function Disable video load on specific user agents (using regular expression) or with function return value. The image will be set on the background.

Disable on mobile devices

jarallax(document.querySelectorAll('.jarallax'), {
    disableParallax: /iPad|iPhone|iPod|Android/,
    disableVideo: /iPad|iPhone|iPod|Android/
});

Or using function. Example:

jarallax(document.querySelectorAll('.jarallax'), {
    disableParallax: function () {
        return /iPad|iPhone|iPod|Android/.test(navigator.userAgent);
    },
    disableVideo: function () {
        return /iPad|iPhone|iPod|Android/.test(navigator.userAgent);
    }
});

Options For Video

Required jarallax/jarallax-video.js file.

Name Type Default Description
videoSrc string null You can use Youtube, Vimeo or Self-Hosted videos. Also you can use data attribute data-jarallax-video.
videoStartTime float 0 Start time in seconds when video will be started (this value will be applied also after loop).
videoEndTime float 0 End time in seconds when video will be ended.
videoVolume float 0 Video volume from 0 to 100.
videoLoop boolean true Loop video to play infinitely.
videoPlayOnlyVisible boolean true Play video only when it is visible on the screen.
videoLazyLoading boolean true Preload videos only when it is visible on the screen.

Options For Element Parallax

Required jarallax/jarallax-element.js file.

Name Type Default Description
type string element Will only work with element value.
speed mixed 0 0 Parallax distance in pixels. Supported Y and X axis. Example: 100 200. Also you can use data attribute data-jarallax-element.
threshold mixed null null Specify threshold for the parallax effect to kick in. For example, if you pass 0 0, the element will start to move only after it has been scrolled to the middle of the viewport.

Events

Name Description
onScroll Called when parallax working. Use first argument with calculations. More info see below.
onInit Called after init end.
onDestroy Called after destroy.
onCoverImage Called after cover image.

onScroll event

jarallax(document.querySelectorAll('.jarallax'), {
    onScroll: function(calculations) {
        console.log(calculations);
    }
});

Methods

Name Result Description
destroy Destroy Jarallax and set block as it was before plugin init.
isVisible boolean Check if parallax block is in viewport.
onResize Fit image and clip parallax container. Called on window resize and load.
onScroll Calculate parallax image position. Called on window scroll.

Call methods

jarallax(document.querySelectorAll('.jarallax'), 'destroy');
$('.jarallax').jarallax('destroy');

Leave a comment

Your email address will not be published. Required fields are marked *

STAYUPDATED

Receive all our future posts instantly in your inbox. Enter your email to enroll.