Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ukiyojs

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ukiyojs

Simple and lightweight JavaScript library for background parallax with support for picture/img elements.

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source


Ukiyo.js

Simple and lightweight JavaScript library for modern background parallax,
with support for picture elements and any images.

⛰️
DEMO


Installation

You can install the library via npm/yarn:

npm install ukiyojs
yarn add ukiyojs

or via CDN:

<script src="https://cdn.jsdelivr.net/npm/ukiyojs@1.0.1/dist/ukiyo.min.js"></script>

Import Ukiyo.js:

import Ukiyo from "ukiyojs";

Usage

HTML

Give the element you want to parallax a cool name to call it in JavaScript.

<img> element
<img class="ukiyo" src="image.jpg">
<picture> element
<picture>
  <source srcset="img.webp" type="image/webp">
  <img class="ukiyo" src="img.png">
<picture>
<picture>
  <source srcset="~" media="(min-width: 1000px)" />
  <source srcset="~" media="(min-width: 700px)" />
  <img class="ukiyo" src="~">
</picture>

picture tag element is also supported: set the parallax to the img tag inside the picture tag.

→ Other Element
<div class="ukiyo"></div>

Don't forget to set the background-image to the style when you set it in the img tag element.

JavaScript

const image = document.querySelector('.ukiyo');

new Ukiyo(image)

You are now ready to go.


If you want to apply it to more than one element, you need to loop through them as follows:

const images = document.querySelectorAll(".ukiyo");
// You can do the loop in any way you like.
images.forEach(image => {
  new Ukiyo(image, {
    speed: 2,
    scale: 1.25
  });
});

Options

OptionTypeDefaultDescription
scalenumber1.5Parallax image scaling factor.
speednumber1.5Parallax speed.
willChangebooleanfalseIf true, the element will be given a will-change: transform when Parallax is active.
wrapperClassstringnullClass name of the automatically generated wrapper element.

These can be configured with the following JS code:

const parallax = document.querySelector('.image');

new BgParallax(parallax, {
    scale: 1.5, // 1~2 is recommended
    speed: 1.5, // 1~2 is recommended
    willChange: true, // This may not be valid in all cases
    wrapperClass: "ukiyo-wrapper"
})

Element Options

These options can be set individually for an element using the data-u-* attribute:

<img
  data-u-scale="2"
  data-u-speed="1.7"
  data-u-wrapper-class="wrapper-name"
  data-u-willchange
>
OptionDescription
data-u-scale=""scale option.
data-u-speed=""speed option.
data-u-willchangewillChange option. Simply attach it to the element to make it valid.
data-u-wrapper-class=""wrapperClass option.

Option names start with data-u-*. Don't forget to prefix the option name with a "u", if u do.

Methods

reset()

Reset the instance and recalculate the size and position of the elements etc :

const image = document.querySelector('.image');
const instance = new Ukiyo(image);

instance.reset();

destroy()

Destroy instance:

const image = document.querySelector('.image');
const instance = new Ukiyo(image);

instance.destroy();

Browser Support

IEEdgeFirefoxChromeOperaSafariiOS Safari
❌No Support✅Latest✅Latest✅Latest✅Latest✅Latest✅Latest

To support older browsers such as IE, you will need to use the Intersection Observer API, Promise and closest() polyfills. If you use img tag, you will also need to use the object-fit polyfill.

However, please note that even with polyfill, the Parallax animation does not run smoothly in IE.

License

MIT License

Keywords

FAQs

Package last updated on 24 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc