Skip to content

Instantly share code, notes, and snippets.

@acemir
Last active October 7, 2015 04:17
Show Gist options
  • Save acemir/d05d6912639dbafe3aac to your computer and use it in GitHub Desktop.
Save acemir/d05d6912639dbafe3aac to your computer and use it in GitHub Desktop.
Pure javascript utilities
Pure javascript utilities
function getDocHeight(){
var D = document;
return Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
}
function get_static_style(styles) {
var result = [];
styles.forEach(function(v, i, a){
var style='';
if( v.stylers ) { // only if there is a styler object
if (v.stylers.length > 0) { // Needs to have a style rule to be valid.
style += (v.hasOwnProperty('featureType') ? 'feature:' + v.featureType : 'feature:all') + '|';
style += (v.hasOwnProperty('elementType') ? 'element:' + v.elementType : 'element:all') + '|';
v.stylers.forEach(function(val, i, a){
var propertyname = Object.keys(val)[0];
var propertyval = val[propertyname].toString().replace('#', '0x');
// changed "new String()" based on: http://stackoverflow.com/a/5821991/1121532
style += propertyname + ':' + propertyval + '|';
});
}
}
result.push('style='+encodeURIComponent(style));
});
return result.join('&');
}
function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
function easeInOut(currentTime, start, change, duration) {
currentTime /= duration / 2;
if (currentTime < 1) {
return change / 2 * currentTime * currentTime + start;
}
currentTime -= 1;
return -change / 2 * (currentTime * (currentTime - 2) - 1) + start;
}
function scrollTo(element, to, duration) {
var start = element.scrollTop,
change = to - start,
increment = 20;
var animateScroll = function(elapsedTime) {
elapsedTime += increment;
var position = easeInOut(elapsedTime, start, change, duration);
element.scrollTop = position;
if (elapsedTime < duration) {
setTimeout(function() {
animateScroll(elapsedTime);
}, increment);
}
};
animateScroll(0);
}
//USAGE
scrollTo(document.body, 0, 300 );
scrollTo(document.documentElement, 0, 300 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment