GithubHelp home page GithubHelp logo

rvagg / archived-ender-transition-support Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 112 KB

A simple feature-detect for Ender to make `$.support.transition` available in browsers.

License: Other

JavaScript 100.00%

archived-ender-transition-support's Introduction

ender-transition-support

A simple feature-detect for Ender to make $.support.transition available in browsers. Where CSS transitions are supported, it will contain an object with an 'end' property which will identify the current browser's equivalent of the 'transitionEnd' event. Where CSS transitions are not supported, $.support.transition will be null.

Also adds a convenience onTransitionEnd() method to your Ender collections (see bottom example).

Add to your ender build with:

$ ender add ender-transition-support

Example

CSS

.foo {
  height: 200px;
  width: 200px;
  transition: opacity ease 1.5s;
  -ms-transition: opacity ease 1.5s;
  -webkit-transition: opacity ease 1.5s;
  opacity: 0;
  background-color: red;
}
.foo.shown {
  opacity: 1;
}

HTML

<button>show</button>
<div class="foo"></div>

JavaScript

var $button = $('button')
  , $div    = $('div')

$button.on('click', function () {
  var post
  if ($button.text() == 'show') {
    $div.show()[0].offsetHeight // trigger DOM reflow
    $div.addClass('shown')
    $button[0].disabled = true
    post = function () {
      $button[0].disabled = false
      $button.text('hide')
    }
    if ($.support.transition)
      $div.one($.support.transition.end, post)
    else
      post()
  } else {
    $div.removeClass('shown')
    $button[0].disabled = true
    post = function () {
      $div.hide()
      $button.text('show')
      $button[0].disabled = false
    }
    if ($.support.transition)
      $div.one($.support.transition.end, post)
    else
      post()
  }
})

This example is available in the file example.html in the repository if you want to play with it.

Note how you can use a check for $.support.transition to allow for browsers with and without transition support. Also note the use of one() rather than on(), you don't want to leave your event handlers laying around if you don't need to reuse them!

onTransitionEnd()

The onTransitionEnd() method is simply a wrapper around the above pattern where you provide a callback function that is either triggered on the 'transitionEnd' event or is called directly if transition events aren't supported. Using it we can trim down our example 'click' handler to the following:

$button.on('click', function () {
  if ($button.text() == 'show') {
    $div.show()[0].offsetHeight // force reflow
    $div.addClass('shown')
    $button[0].disabled = true
    $div.onTransitionEnd(function () {
      $button[0].disabled = false
      $button.text('hide')
    })
  } else {
    $div.removeClass('shown')
    $button[0].disabled = true
    $div.onTransitionEnd(function () {
      $div.hide()
      $button.text('show')
      $button[0].disabled = false
    })
  }
})

Credits

Credit for the feature-detect goes to the Modernizr team, I lifted the code from Bootstrap.

Licence

Licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

archived-ender-transition-support's People

Contributors

rvagg avatar

Stargazers

 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.