This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Determine if an element is in the visible viewport | |
function isInViewport(element) { | |
var rect = element.getBoundingClientRect(); | |
var html = document.documentElement; | |
return ( | |
rect.top >= 0 && | |
rect.left >= 0 && | |
rect.bottom <= (window.innerHeight || html.clientHeight) && | |
rect.right <= (window.innerWidth || html.clientWidth) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get image's max-width:100%; in pixels | |
function getMaxWidth(img) { | |
var maxWidth; | |
// Check if naturalWidth is supported | |
if (img.naturalWidth !== undefined) { | |
maxWidth = img.naturalWidth; | |
// Not supported, use in-memory solution as fallback | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hasClass, takes two params: element and classname | |
function hasClass(el, cls) { | |
return el.className && new RegExp("(\\s|^)" + cls + "(\\s|$)").test(el.className); | |
} | |
/* use like below */ | |
// Check if an element has class "foo" | |
if (hasClass(element, "foo")) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This gives us simple dollar function and event binding | |
var $ = document.querySelectorAll.bind(document); | |
Element.prototype.on = Element.prototype.addEventListener; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// removeClass, takes two params: element and classname | |
function removeClass(el, cls) { | |
var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)"); | |
el.className = el.className.replace(reg, " ").replace(/(^\s*)|(\s*$)/g,""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myObj = document.getElementsByClassName('class'); | |
for (var i = 0; i < myObj.length; ++i) { | |
var item = myObj[i]; | |
item.innerHTML = 'this is value'; | |
} |
NewerOlder