Skip to content

Instantly share code, notes, and snippets.

let userLang = navigator.language || navigator.userLanguage;
@TimoM
TimoM / isVisibleInViewport.js
Created June 6, 2019 11:10
Check if element is in viewport
function isVisibleInViewport(elem)
{
var y = elem.offsetTop;
var height = elem.offsetHeight;
while ( elem = elem.offsetParent )
y += elem.offsetTop;
var maxHeight = y + height;
var isVisible = ( y < ( window.pageYOffset + window.innerHeight ) ) && ( maxHeight >= window.pageYOffset );
@TimoM
TimoM / trigger.js
Created June 6, 2019 11:08
Shorthand to trigger an event
Object.prototype.trigger = function(e) {
if (e == undefined) {
console.error("Param missing");
}
var event = document.createEvent('HTMLEvents');
event.initEvent(e, true, false);
this.dispatchEvent(event);
};
@TimoM
TimoM / addListenerMulti.js
Created June 6, 2019 11:08
Add same function to multiple events
function addListenerMulti(el, s, fn) {
s.split(' ').forEach(function(e){
el.addEventListener(e, fn, false);
});
}
@TimoM
TimoM / forEach.js
Last active June 6, 2019 11:25
Shorthand forEach
var forEach = function (array, callback, scope) {
for (var i = 0; i < array.length; i++) {
callback.call(scope, i, array[i]); // passes back stuff we need
}
};
@TimoM
TimoM / closestByClass.js
Created June 6, 2019 11:05
Select an element with chosen class that is nearest to element
Object.prototype.closestByClass = function(clazz) {
var el = this;
while (!el.classList.contains(clazz)) {
el = el.parentNode;
if (!el) {
return null;
}
}
@TimoM
TimoM / bottom.js
Last active June 6, 2019 11:23
Detect if reached bottom of page
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert("bottom!");
}
});
@TimoM
TimoM / scrollAnchor.js
Last active November 11, 2019 09:46
Soft Scroll to anchor
$("a[href*=\\#]").click(function(event){
$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 800);
event.preventDefault();
});
@TimoM
TimoM / loadBackgroundImages.js
Created February 24, 2018 16:19
loadBackgroundImages
$(window).on('load', function () {
// image loader
setInterval(loadBackgroundImages, 200);
});
function loadBackgroundImages(){
var count = 0;
$('[data-background-image]').each( function(){
console.debug('load '+$(this).attr('data-background-image') );
var source = $(this).attr('data-background-image');
var target = $(this).css('backgroundImage');
@TimoM
TimoM / contentFadeIn.css
Last active February 24, 2018 16:18
Content FadeIn on Scroll down
section.hide {
margin-top: 40px;
margin-bottom: -40px;
opacity: 0;
filter: alpha(opacity=0);
}
section.show {
margin-top: 0px;
margin-bottom: 0px;
opacity: 1;