Class bRando

A class for automatically changing CSS backgrounds on any element in a random or sequential order — a background changer. A usage example can be found in the readme.

Remarks

A background will never repeat (back-to-back) unless only one background value is given.

Hierarchy

  • bRando

Constructors

  • Constructs a background changer and accepts an Options object. A usage example can be found in the readme.

    Parameters

    Returns bRando

    Remarks

    First Background

    Creating a background changer does not immediately change the background. Each selected element will first display whatever background is set by your CSS styles. Then, after the time set by the timeout option has elapsed, it will change the background.

    If you would like the background changer to immediately load the first background, call next right after creation.

    const bgChanger = bRando.create(options); // create a background changer
    bgChanger.next(); // immediately change to one of the backgrounds

    This will cause the background changer to load with one of the values set by the backgrounds option.

Properties

_CSSBackgroundVarName: string
_CSSContentVarName: string
_CSSOpacityVarName: string
_CSSSelector: string = ""
_CSSTransitionVarName: string
_backgrounds: string[] = []
_changer: number = -1
_currentBackgroundIndex: number = -1
_demoBackgrounds: string[] = ...

An array of demo CSS backgrounds. Follow the link below ↙ to see the values in the source code.

_isAfterOpaque: boolean = false
_nodes: NodeList
_originalCSSBackgrounds: string[] = []
_originalCSSPositions: string[] = []
_originalCSSZIndexes: string[] = []
_random: boolean = true
_styleElement: HTMLElement
_timeout: number = 0
_transition: string = ""

Accessors

  • get CSSSelector(): string
  • Returns the CSS selector used when selecting elements for a background changer. e.g. "main"

    Returns string

  • get backgrounds(): string[]
  • Returns an array of the background values set for a background changer. e.g. ["red", "green", "blue"]

    Returns string[]

  • set backgrounds(value): void
  • Sets an array of CSS backgrounds to be used. e.g. ["red","green","blue"]

    Parameters

    • value: undefined | string[]

    Returns void

    Default Value

    An array of demo values

  • get currentBackgroundIndex(): number
  • Returns the index of the background currently being displayed from the backgrounds array. Returns -1 if none of the backgrounds are being displayed.

    Returns number

  • get nodes(): NodeList
  • Returns a NodeList containing every element selected for a background changer.

    Returns NodeList

  • get random(): boolean
  • Returns whether a background changer will randomly (true) or sequentially (false) pick the next background value.

    Returns boolean

  • set random(value): void
  • Sets whether to pick the next background value at random (true) or sequentially (false).

    Parameters

    • value: undefined | boolean

    Returns void

    Default Value

    true

  • get timeout(): number
  • Returns the set time between background changes (in milliseconds). e.g. 5000

    Returns number

  • set timeout(value): void
  • Sets the time between background changes (in milliseconds). e.g. 5000

    Parameters

    • value: undefined | number

    Returns void

    Remarks

    Timing Values

    Never set the transition duration longer than the timeout.

    // DO NOT SET transition > timeout

    // Bad
    const options = {
    transition: "5000ms", // transition duration set to 5 seconds
    timeout: 3000, // time between backgrounds set to 3 seconds
    ...
    }

    // Bad
    bgChanger.transition = "5000ms"; // transition duration set to 5 seconds
    bgChanger.timeout = 3000; // time between backgrounds set to 3 seconds

    If it is longer, the transition to a new background will not complete before the next background is set, causing background changes to glitch past animations.

    Default Value

    7500

  • get transition(): string
  • Returns the CSS transition (without a transition-property) used when changing between backgrounds. e.g. "500ms ease-in" or "0.5s ease-in-out 0.25s"

    Returns string

  • set transition(value): void
  • Sets the CSS transition (without a transition-property) used when changing between backgrounds. e.g. "500ms ease-in" or "0.5s ease-in-out 0.25s"

    Parameters

    • value: undefined | string

    Returns void

    Remarks

    Timing Values

    Never set the transition duration longer than the timeout.

    // DO NOT SET transition > timeout

    // Bad
    const options = {
    transition: "5000ms", // transition duration set to 5 seconds
    timeout: 3000, // time between backgrounds set to 3 seconds
    ...
    }

    // Bad
    bgChanger.transition = "5000ms"; // transition duration set to 5 seconds
    bgChanger.timeout = 3000; // time between backgrounds set to 3 seconds

    If it is longer, the transition to a new background will not complete before the next background is set, causing background changes to glitch past animations.

    Default Value

    "5000ms"

Methods

  • Returns whether a background changer has been removed.

    Returns boolean

    true if removed

    Example

    bgChanger.remove(); // changer was told to remove itself
    bgChanger.isRemoved(); // returns true
  • Returns whether a background changer is playing.

    Returns boolean

    true if running

    Example

    bgChanger.play(); // changer was told to play
    bgChanger.isRunning(); // returns true
  • Advances a background changer to the next background in the sequence or at random, depending on the value of random.

    Returns void

    Example

    bgChanger.next();
    
  • Pauses a background changer on the current background. Calling this will not pause any transition animations.

    Returns void

    Example

    bgChanger.pause();
    
  • Plays a background changer. This is automatically called when a new background changer is constructed.

    Returns void

    Example

    bgChanger.pause(); // changer was paused
    bgChanger.play(); // resume changer
  • Removes a background changer from the DOM and restores the selected elements to their state before background changes were made.

    Returns void

    Example

    const bgChanger = new bRando(options); // changer created for <body>
    bgChanger.remove(); // remove changer from the DOM and <body>

Generated using TypeDoc