1.0.11 • Published 2 years ago

magical-scroll v1.0.11

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Magical Scroll

Basic Usage

MagicalScroll.getInstance().addElement({
  target: "#scroll",
  animations: {
    opacity: {
      positions: [0, 100],
      values: [0, 1],
    },
  },
});

Magic Slugs

SlugDescription
#screenWidthwidth of the screen
#screenHeightheight of the screen
#scrollWidthwidth of the scroll container
#scrollHeightheight of the scroll container
#elementWidthwidth of the element
#elementHeightheight of the element
#elementInwhen the element appear at the bottom of the container
#elementOutwhen the element disappear at the top of the container
#elementCenterwhen the element is placed center of the container
#parentElementWidthwidth of the parent element (node)
#parentElementHeightheight of the parent element (node)
#parentElementInwhen the parent element (node) appear at the bottom of the container
#parentElementOutwhen the parent element (node) disappear at the top of the container
#parentElementCenterwhen the parent element (node) is placed center of the container
#ancestorElementWidth{1}width of the ancestor element (node), where {1} means the generation(s)
#ancestorElementHeight{1}height of the ancestor element (node), where {1} means the generation(s)
#ancestorElementIn{1}when the ancestor element (node) appear at the bottom of the container, where {1} means the generation(s)
#ancestorElementOut{1}when the ancestor element (node) disappear at the top of the container, where {1} means the generation(s)
#ancestorElementCenter{1}when the ancestor element (node) is placed center of the container, where {1} means the generation(s)

Built-in Properties

NameDescription
opacity
scale
rotate
translateX
translateY
displaynone or just nothing
visibilityvisible or hidden
backgroundColorcalculating within values
colorcalculating within values

Register/Replace your own property

Register/Replace the value populating callback

const magicalScroll = MagicalScroll.getInstance();
const animationName = "myScrollAnimation";
magicalScroll.registerPropertyPopulateCallback(
  animationName,
  (currentPosition, positions, values) => {
    const index = positions.findIndex((position) => position > currentPosition);
    return index > -1 ? values[index] : values[values.length - 1];
  },
);

Explanation

NameMeaning
currentPositioncurrent scroll top of the container
positionslist of position which are already computed as a number
valueslist of value

You can also register a new animation with existed built-in callback

const magicalScroll = MagicalScroll.getInstance();
const animationName = "opacity";
magicalScroll.registerPropertyPopulateCallback(
  animationName,
  MagicalScroll.populateNumberValueCallback,
);

Register/Replace the CSS generation callback

const magicalScroll = MagicalScroll.getInstance();
const animationName = "myScrollAnimation";
magicalScroll.registerPropertyPopulateCallback(
  animationName,
  (target, property, value) => {
    target.style.display = value ? null : "none";
  }
);

Explanation

NameMeaning
targetelement target
propertyname of the property, animationName as the example above
valuecomputed value

What the next?

  1. Provide other properties something like skew, brightness.
  2. Interaction Observer API for better performance.
  3. Support multiple instances.
  4. Child slug.
  5. Query for element.
  6. Function of toggling class by position.
  7. Inertia?
  8. Animation function (ease-in-out, ... etc.) support.
  9. Refactor it into better architecture.
  10. Matrix calculation.
1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago