Skip to content

Instantly share code, notes, and snippets.

View bloodyowl's full-sized avatar
🦉

Matthias Le Brun bloodyowl

🦉
View GitHub Profile
@bloodyowl
bloodyowl / smoothScrool.js
Created June 19, 2012 15:23
smoothScroll
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)
// 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)
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 June 29, 2012 09:52
smoothScroll explained
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,
@bloodyowl
bloodyowl / gist:3165483
Created July 23, 2012 19:07
Stylus gradients
// 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 September 15, 2012 12:00
Browser detection
/*
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);
/* 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 October 22, 2012 10:30
String to Element
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 October 22, 2012 13:34
Transition (craft.js mixin)
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
@bloodyowl
bloodyowl / gist:3938983
Created October 23, 2012 14:15
JS pagination
// 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){