Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ Example usage:
$('.msg').prepareTransition().toggleClass('hidden');
});

Example CSS:

.examplepanel {
opacity: 1;
transition: opacity 1s;
display: block;
}

.is-shown .examplepanel {
opacity: 0;
display: none;
}

.examplepanel.is-transitioning {
display: block !important;
}
10 changes: 10 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify');

gulp.task('default', function() {
return gulp.src("./preparetransition.js")
.pipe(uglify())
.pipe(rename("preparetransition.min.js"))
.pipe(gulp.dest('./'))
});
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "prepare-transition",
"version": "0.1.1",
"description": "The prepareTransition plugin sets display and visibility to override any existing display and visibility properties.",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/snookca/prepareTransition.git"
},
"keywords": [
"css",
"transition",
"jquery"
],
"author": "jonathan@snook.ca",
"license": "MIT",
"bugs": {
"url": "https://github.com/snookca/prepareTransition/issues"
},
"homepage": "https://github.com/snookca/prepareTransition#readme",
"devDependencies": {
"gulp": "^3.9.0",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.2.0"
}
}
105 changes: 97 additions & 8 deletions preparetransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,102 @@

(function($){

$.fn.prepareTransition = function(){
// Checks if transitions are supported
function supportsTransitions() {
var b = document.body || document.documentElement,
s = b.style,
p = 'transition';

if (typeof s[p] == 'string') { return true; }

// Tests for vendor specific prop
var v = ['Moz', 'webkit', 'Webkit', 'Khtml', 'O', 'ms'];
p = p.charAt(0).toUpperCase() + p.substr(1);

for (var i=0; i<v.length; i++) {
if (typeof s[v[i] + p] == 'string') { return true; }
}

return false;
}

// gets the CSS & JS name of the 'transform' property, which can vary from browser to browser
function getTransformName() {
var st = window.getComputedStyle(document.body, null);

var rtnObj = {
css: null, js: null
}

if( st.getPropertyValue("transform") !== null ) {
rtnObj.css = "transform";
rtnObj.js = "transform";
return rtnObj;
}

if( st.getPropertyValue("-webkit-transform") !== null ) {
rtnObj.css = "-webkit-transform";
rtnObj.js = "webkitTransform";
return rtnObj;
}

if( st.getPropertyValue("-moz-transform") !== null ) {
rtnObj.css = "-moz-transform";
rtnObj.js = "MozTransform";
return rtnObj;
}

if( st.getPropertyValue("-ms-transform") !== null ) {
rtnObj.css = "-ms-transform";
rtnObj.js = "msTransform";
return rtnObj;
}

if( st.getPropertyValue("-o-transform") !== null ) {
rtnObj.css = "-o-transform";
rtnObj.js = "OTransform";
return rtnObj;
}

return null;
}

/**
* @param property {string} optional - If passed transition end events will only look for events with this property type. This is useful when avoiding multiple transition events on the same element or it's children.
*/
$.fn.prepareTransition = function( property ){
var hasTrans = supportsTransitions();

if(property === "transform") property = getTransformName().css;

return this.each(function(){
var el = $(this);
// remove the transition class upon completion
el.one('TransitionEnd webkitTransitionEnd transitionend oTransitionEnd', function(){
el.removeClass('is-transitioning');
});

if( hasTrans ) {
var evtFired = false;

// Need to add MS events as well?

// remove the transition class upon completion (don't ise $.one, incase multiple properties transitioning)
el.on('TransitionEnd webkitTransitionEnd transitionend oTransitionEnd', function(evt){

// just triggers for one property type (if specified)
if( property && evt.originalEvent.propertyName !== property ) return;

// stops multiple events triggering
if(!evtFired) {
evtFired = true;
el.removeClass('is-transitioning');

// Need to add MS events as well?
$(this).off('TransitionEnd webkitTransitionEnd transitionend oTransitionEnd');
}
});
} else {
el.addClass('hasnt-transition');
}

// Don't we need to add MS events as well?

// check the various CSS properties to see if a duration has been set
var cl = ["transition-duration", "-moz-transition-duration", "-webkit-transition-duration", "-o-transition-duration"];
Expand All @@ -33,14 +122,14 @@ $.fn.prepareTransition = function(){
duration || (duration = parseFloat( el.css( itm ) ));
});

// Should really add delay here as well, right?

// if I have a duration then add the class
if (duration != 0) {
el.addClass('is-transitioning');
if( hasTrans ) el.addClass('is-transitioning');
el[0].offsetWidth; // check offsetWidth to force the style rendering
};
});
};


}(jQuery));

3 changes: 1 addition & 2 deletions preparetransition.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.