Skip to content

Instantly share code, notes, and snippets.

Avatar
🦉

Matthias Le Brun bloodyowl

🦉
View GitHub Profile
View smoothScrool.js
smoothScroll=function(b){var c="pageYOffset" in window?window.pageYOffset:document.documentElement.scrollTop,d=+new Date(),e=d+1000,f=document.height<(b.offsetTop+window.innerHeight)?document.height-window.innerHeight:b.offsetTop,s=f-c<0?true:false,g=setInterval(function(){var h=+new Date();if(h<=e){scrollTo(0,s?(c-(((h-d)/(e-d)*c)+f)):c+(h-d)/(e-d)*(f-c))}else{clearInterval(g)}},10)};
// smoothScroll(elementToReach)
View addEvent.js
// a = element
// b = event (string)
// c = function to call
addEvent = function(a, b, c) {
if ("addEventListener" in window) { // !(IE)
a.addEventListener(b, function(d){
d.preventDefault();
c.call(a);
}, false)
View popover.css
addEvent = function(a, b, c) {
if ("addEventListener" in window) { // !(IE)
a.addEventListener(b, function(d){
d.preventDefault();
c.call(a);
}, false)
} else { // IE
a.attachEvent("on" + b, function() {
c.call(a);
return false
@bloodyowl
bloodyowl / smoothScroll.js
Created Jun 29, 2012
smoothScroll explained
View smoothScroll.js
function (destination, duration) {
// destination (relative to window top) in px
// duration in ms
/* Variables */
var startPosition = "pageYOffset" in window ? window.pageYOffset : document.documentElement.scrollTop,
startTime = +new Date(),
endTime = startTime + duration,
// if top of element is outside the document, then define end point to document limit
// elsewhere, go the the top of the element
destinationSafe = document.height < (destination + window.innerHeight) ? document.height - window.innerHeight : destination,
View gist:3165483
// middle between two colors
middle(first, second)
rgb((red(first) + red(second)) / 2, (green(first) + green(second)) / 2, (blue(first) + blue(second)) / 2)
// simple gradients
linear-gradient(start, end)
// fallback using the intermediate color
background: middle(start, end)
@bloodyowl
bloodyowl / browser.js
Created Sep 15, 2012
Browser detection
View browser.js
/*
Simple browser detection script.
Returns an object.
*/
var Browser = new function() {
this.UA = window.navigator.userAgent.toLowerCase();
this.isChrome = !!(this.UA.indexOf("chrome") + 1);
this.isSafari = !!(this.UA.indexOf("safari") + 1);
this.isIE = !!(this.UA.indexOf("msie") + 1);
View toHandler.js
/* toHandler function
* returns a handler with cross browser preventDefault and cancelPropagation options
* @func : function called with eventObject and the handler itself
* @preventDefault : boolean
* @cancelPropagation : boolean
*/
function toHandler(func, preventDefault, cancelPropagation){
var cache;
return cache = function(object){
@bloodyowl
bloodyowl / gist:3930814
Created Oct 22, 2012
String to Element
View gist:3930814
String.prototype.toElement = function() {
var childNodes, fragment, i, item, sandbox, _i, _len;
sandbox = document.createElement("div");
fragment = document.createDocumentFragment();
sandbox.innerHTML = this;
childNodes = $A(sandbox.childNodes);
for (i = _i = 0, _len = childNodes.length; _i < _len; i = ++_i) {
item = childNodes[i];
fragment.appendChild(item);
}
@bloodyowl
bloodyowl / gist:3931534
Created Oct 22, 2012
Transition (craft.js mixin)
View gist:3931534
DOM.prototype.transition = function(transition, persistent){
var element = this,
hash = new Hash,
prop = "TransitionEvent" in window ? 0 : // FF >= 16
"WebKitTransitionEvent" in window ? 1 : // webkit
+opera.version() > 10.49 ? 2 : // Opera
"mozTransitionEvent" in window ? 3 : // FF <= 15
"msTransitionEvent" in window ? 4 : // IE
-1, // nope
View gist:3938983
// requires Array.range mixin
function paginate(list, itemsPerPage){
// @list : Array, what to paginate
// @itemsPerPage : Number
var fragment = DOM.createFragment();
Array.range(0, Math.ceil(list.length / itemsPerPage)).forEach(function(item){