Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added callback #1

Merged
merged 1 commit into from
Dec 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ var el = document.querySelector('#el');
fade.out(el); // fade el out in 1s
fade.in(el, 1500); // fade el in in 1.5s
fade(el, 0.3, 3000); // fade el to opacity 0.3 in 3s
fade.out(el, function() {
el.style.display = 'none'; // set display to none after fade out
});
```

## API

The default `duration` always is `1000` ms.

### fade(el, opacity[, duration])
### fade(el, opacity[, duration][, callback])

Fade `el` to `opacity` (in `duration` ms).
Fade `el` to `opacity` (in `duration` ms). Call `callback` when finished.

### fade.out(el[, duration])
### fade.out(el[, duration][, callback])

Fade out `el` (in `duration` ms).
Fade out `el` (in `duration` ms). Call `callback` when finished.

### fade.in(el[, duration])
### fade.in(el[, duration][, callback])

Fade in `el` (in `duration` ms).
Fade in `el` (in `duration` ms). Call `callback` when finished.

## Installation

Expand Down Expand Up @@ -63,4 +66,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ module.exports = fade;
* @param {Element} el
* @param {Number} opacity
* @param {Number=} duration
* @param {Function=} callback
*
* @todo Add other vendor prefixes
* @todo Properly clear transition
*/

function fade (el, opacity, duration) {
function fade (el, opacity, duration, callback) {
if (typeof duration === 'undefined') duration = 1000;
else if (typeof duration === 'function') {
callback = duration;
duration = 1000;
}

var oldTransition = prefixed.get(el.style, 'transition') || '';
prefixed(el.style, 'transition', 'opacity ' + (duration/1000) + 's');
el.style.opacity = opacity;

setTimeout(function () {
prefixed(el.style, 'transition', oldTransition);
if (callback) callback();
}, duration);
}

Expand All @@ -38,19 +44,21 @@ function fade (el, opacity, duration) {
*
* @param {Element} el
* @param {Number=} duration
* @param {Function=} callback
*/

fade.out = function (el, duration) {
fade(el, 0, duration);
fade.out = function (el, duration, callback) {
fade(el, 0, duration, callback);
};

/**
* Fade out `el`.
*
* @param {Element} el
* @param {Number=} duration
* @param {Function=} callback
*/

fade['in'] = function (el, duration) {
fade(el, 1, duration);
fade['in'] = function (el, duration, callback) {
fade(el, 1, duration, callback);
};