Skip to content

Repository files navigation

MurphyJS

MurphyJS Logo

npm versionnpm downloadslicense

A lightweight JavaScript library for creating smooth animations with a simple API.

Features

  • 🚀 Lightweight and fast (only 3.7KB)
  • 🎨 Simple and intuitive API
  • 🌈 Beautiful animations
  • 📱 Mobile-friendly
  • 🎯 No dependencies
  • 🎮 Total control of IntersectionObserver parameters
  • 🎁 Some animations implemented by default
  • 🏝 Plug and play solution to landing pages and simple projects
  • ❎ Native fallback to not supported browsers
  • 🛎️ Built-in event system for animation lifecycle (in, out, finish, cancel, reset, cleanup)
  • 🔄 Mirror animations for smooth scroll transitions
  • 📏 Viewport position control with predefined aliases
  • 📱 Mobile optimization with device detection

Installation

Using npm:

npm install murphyjs

Using yarn:

yarn add murphyjs

Using CDN:

<scriptsrc="https://unpkg.com/murphyjs@2.5.1/dist/index.js"></script><script>// Call play() after the script is loadedif(typeofwindow!=="undefined"&&window.murphy){window.murphy.play();}</script>

For detailed documentation and examples, visit our documentation site.

Quick Start

import{Murphy}from'murphyjs';// Create a new instanceconstmurphy=newMurphy();// Animate elementsmurphy.animate('.box',{opacity: [0,1],y: [20,0],duration: 1000});

Usage

1. Tag your HTML

In your markup, decorate your element with attribute data-murphy:

<divdata-murphy="left-to-right">Any content here</div>

The default effect of murphy is bottom-to-top, but you can also use:

  • top-to-bottom
  • left-to-right
  • right-to-left

2. Reset your CSS

In your CSS, reset all the tagged elements:

*[data-murphy] {
opacity:0;
}

3. Start murphy

In JavaScript, import and run play when your page is completely loaded:

importmurphyfrom"murphyjs";murphy.play();

Or if you're using the script tag:

<scriptsrc="./murphy/index.js"></script><script>murphy.play();</script>

Configuration

You can configure the animation of each decorated element individually using these attributes:

AttributeTypeDefaultDescription
data-murphyString'bottom-to-top'Animation direction
data-murphy-appearance-distanceInt50pxDistance from viewport edge to trigger animation
data-murphy-element-distanceInt30pxDistance the element moves during animation
data-murphy-easeString'ease'Animation easing function (can be a cubic-bezier)
data-murphy-animation-delayInt300msDelay before animation starts
data-murphy-element-thresholdFloat1.0How much of the element needs to be visible to trigger (0-1)
data-murphy-animation-durationInt300msDuration of the animation
data-murphy-root-marginString'0px 0px -50px 0px'Custom root margin for the Intersection Observer. Use this to control when animations trigger based on viewport position. You can use predefined aliases: 'top', 'middle', 'bottom', 'quarter', 'three-quarters'
data-murphy-groupStringundefinedGroup identifier for controlling animations for specific groups of elements
data-murphy-mirrorBooleanfalseWhether to play the animation in reverse when the element leaves the viewport
data-murphy-disable-mobileBooleanfalseWhether to disable animations on mobile devices (screen width <= 768px or mobile user agent)

Advanced Features

Mirror Animations

Enable mirror animations to create smooth transitions when elements leave the viewport:

<divdata-murphy="bottom-to-top" data-murphy-mirror="true">
This element will animate in when scrolling down and animate out when scrolling up
</div>

Viewport Position Control

Control when animations trigger based on the element's position in the viewport using the data-murphy-root-margin attribute. For convenience, we provide several aliases:

<!-- Animate when element reaches middle of viewport --><divdata-murphy="bottom-to-top" data-murphy-root-margin="middle">
This will animate when it reaches the middle of the viewport
</div><!-- Animate when element reaches bottom of viewport --><divdata-murphy="bottom-to-top" data-murphy-root-margin="bottom">
This will animate when it reaches the bottom of the viewport
</div><!-- Animate when element is 25% from bottom of viewport --><divdata-murphy="bottom-to-top" data-murphy-root-margin="quarter">
This will animate when it's 25% from the bottom of the viewport
</div><!-- Animate when element is 75% from bottom of viewport --><divdata-murphy="bottom-to-top" data-murphy-root-margin="three-quarters">
This will animate when it's 75% from the bottom of the viewport
</div>

You can also use raw CSS margin values if you need more precise control:

<divdata-murphy="bottom-to-top" data-murphy-root-margin="0px 0px -50% 0px">
This will animate when it reaches the middle of the viewport
</div>

The root margin follows the CSS margin syntax: top right bottom left. Negative values create an inset margin, which means the animation will trigger when the element reaches that point in the viewport.

Available Viewport Position Aliases

AliasDescriptionRaw Value
topTriggers at top of viewport'0px 0px 0px 0px'
middleTriggers at middle of viewport'0px 0px -50% 0px'
bottomTriggers at bottom of viewport'0px 0px 0px 0px'
quarterTriggers at 25% from bottom'0px 0px -25% 0px'
three-quartersTriggers at 75% from bottom'0px 0px -75% 0px'

Group-based Animations

You can group elements using the data-murphy-group attribute. This allows you to control animations for specific groups of elements. For example, you can play or reset animations for only a subset of elements by specifying a group name:

<divdata-murphy="bottom-to-top" data-murphy-group="group1">Group 1</div><divdata-murphy="top-to-bottom" data-murphy-group="group1">Group 1</div><divdata-murphy="left-to-right" data-murphy-group="group2">Group 2</div><divdata-murphy="right-to-left" data-murphy-group="group2">Group 2</div>

You can then control animations for a specific group using the API:

// Play animations for group1 onlymurphy.play('group1');// Reset animations for group2 onlymurphy.reset('group2');

API

Global Methods

MethodDescription
play(group?: string)Start monitoring elements in DOM tagged with data-murphy attribute. Optionally specify a group to animate only elements in that group.
cancel()Cancel all animations and reset elements to their final state.
reset(group?: string)Reset all animations to their initial state. Optionally specify a group to reset only elements in that group.
cleanup()Disconnect all Intersection Observers and clean up resources.

Murphy Class

The Murphy class provides a programmatic way to create animations:

import{Murphy}from'murphyjs';// Create a new instanceconstmurphy=newMurphy();// Animate elementsmurphy.animate('.box',{opacity: [0,1],y: [20,0],duration: 1000});

animate(selector, options)

Animates elements matching the selector with the specified options.

Parameters
ParameterTypeDescription
selectorStringCSS selector for target elements
optionsObjectAnimation configuration
Options
OptionTypeDefaultDescription
opacityArray[0, 1]Start and end opacity values
xArray[0, 0]Start and end x translation in pixels
yArray[0, 0]Start and end y translation in pixels
durationNumber1000Animation duration in milliseconds
delayNumber0Delay before animation starts in milliseconds
easeString'ease'Easing function name

Events

MurphyJS provides a set of events that you can listen to for better control and integration:

EventDescription
murphy:inFired when an element enters the viewport
murphy:outFired when an element leaves the viewport
murphy:finishFired when an animation completes
murphy:cancelFired when an animation is cancelled
murphy:resetFired when an element is reset
murphy:cleanupFired when observers are cleaned up

Event Example

document.addEventListener('murphy:in',(event)=>{const{ element }=event.detail;console.log('Element entered viewport:',element);});document.addEventListener('murphy:finish',(event)=>{const{ element }=event.detail;console.log('Animation finished:',element);});

Available Animations

MurphyJS comes with several built-in animations that you can use with the data-murphy attribute:

Basic Animations

  • bottom-to-top
  • top-to-bottom
  • left-to-right
  • right-to-left

Flip Animations

  • flip-left
  • flip-right
  • flip-up
  • flip-down

Zoom Animations

  • zoom-in
  • zoom-out

Fade Animations

  • fade
  • fade-up
  • fade-down
  • fade-left
  • fade-right

Rotate Animations

  • rotate-left
  • rotate-right

Scale Animations

  • scale-up
  • scale-down

Slide Animations

  • slide-up
  • slide-down
  • slide-left
  • slide-right

Bounce Animations

  • bounce-in
  • bounce-out

About

A JavaScript vanilla library to scroll based reveal animations. The murphy.js is a lightweight JavaScript animation library with a simple implementation way. All this works by joining of data-attributes, Web animate API and Intersection Observer API.

Topics

Resources

Stars

112 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages