Coder Social home page Coder Social logo

maximilianos / jquery.step Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 0.0 239 KB

A jQuery plugin for looping through an element array or jQuery object with a delay / timeout between each step callback

Home Page: http://maximilianos.github.io/jquery.step/

JavaScript 100.00%

jquery.step's Introduction

jquery.step

jQuery.step enables you to step or stagger through an array of jQuery DOM Elements. It is essentially like jQuery's native $().each() function, but with the added option to define the timeout or delay between each iteration.

Examples

Let's say you had a bunch of lis, like these:

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

and you wanted to apply a transition to each item, one after the other. Then using jQuery.step you could use code very similar to the following:

$('li').step(function () {
  $(this).addClass('stepped');
}, 500);

This would consecutively add the stepped class to each li in the list with a 500ms delay between each addition. You can then style the transition states for both the unstepped and stepped lis in your stylesheet.

A full implementation of the step function could look like the following:

function doEveryStep(index, elements, delay) {
	// ... do something with $(this)
	// on every step
}

function calculateTimeout(index, elements) {
  // ... optional, you can also pass in
  // exact milliseconds instead

	var timeout = 0;
	
  return timeout; // must return number
}

function done(index, elements) {
	// ... do something right after
  // the loop has finished
}

var options = {
	startAt: 0, // index to start from
	endAt: false, // index to end at (false === end of array)
	timeout: calculateTimeout,
	onEnd: done
};


$('li').step(doEveryStep, options);

You can check out the jquery.step mini demo page for a simple working example.

Usage

You can include the minified script into your project directly or you can install this script via npm or bower.

npm install jquery.step --save

or

bower install jquery.step --save

and then include in your project using any UMD compatible method you like

Properties & Config

jQuery.step takes two parameters:

  • the stepcallback parameter is the function that gets called at each iteration of the loop. this in the stepcallback function refers to the currently evaluating element. The function gets passed the following variables

    • index: the current index of the iteration
    • elements: the selected elements that we are stepping through
    • delay: the delay until the next iteration of the loop
  • the custom parameter is optional and can be one of three things:

    • a simple number that represents the delay in milliseconds between each iteration of the stepcallback,
    • a function that returns that number for every individual iteration. This function gets passed the following variables:
      • index: the current index of the iteration
      • elements: the selected elements that we are stepping through
    • or an object containing custom options for the step function. This object can contain any of the following:
      • timeout - accepts either a number or a function to return the delay between each iteration. (Default: 300)
      • startAt: a number indicating the index of the array of elements to begin from. (Default: 0)
      • endAt: a number indicating the index of the array of elements to stop at. (false indicates ending at the end of the array) (Default: false)
      • onEnd: a function that runs once after the step function has run for the last time. This function takes the following variables: (Default: false)
        • index: the current index of the iteration
        • elements: the selected elements that we are stepping through

jquery.step's People

Contributors

maximilianos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.