Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Farmatique / gist:3c691686079502f33d0cf8a6a72ffe2d
Created August 15, 2018 10:05
Chek for real mobile-device (pointerevent hack)
function isMobile() {
return !('onpointerdown' in document.documentElement);
}
// source: https://greensock.com/forums/topic/18719-how-to-manage-gsap-animation-in-mobile-device/
function installMediaQueryWatcher(mediaQuery, layoutChangedCallback) {
var mql = window.matchMedia(mediaQuery);
mql.addListener(function (e) { return layoutChangedCallback(e.matches); });
layoutChangedCallback(mql.matches);
}
// example below
installMediaQueryWatcher("(min-width: 600px)", function(matches) {
@Farmatique
Farmatique / gist:f277e584188440aae56421ab477a30e1
Last active December 24, 2018 11:56
Equalize or set the height of block to fit maximum height in set of blocks
function matchHeight(wrapperEl, matchEl){
var maxHeight = 0;
var element = jQuery(wrapperEl);
jQuery(element).find(matchEl).each(function(index, element){
// reset previous min-height
jQuery(this).css('min-height', 0);
if (jQuery(this).outerHeight() > maxHeight) { maxHeight = jQuery(this).outerHeight(); }
});
jQuery(element).find(matchEl).css('min-height', maxHeight);
}
body{
-ms-overflow-style: none; // IE 10+
overflow: -moz-scrollbars-none; // Firefox
}
body::-webkit-scrollbar {
display: none; // Safari and Chrome
}
@Farmatique
Farmatique / gist:f99bbebf9d97482b00e7bf107e5f3f4a
Created July 2, 2018 19:46
JS random number generator function
function random(min, max) {
if (max == null) { max = min; min = 0; }
if (min > max) { var tmp = min; min = max; max = tmp; }
return min + (max - min) * Math.random();
}
@Farmatique
Farmatique / gist:d03f263181e35cfd33b5478dde3755de
Last active July 2, 2018 10:44
Collapsed expanded menu button navbar menu button custom
<button type="button" class="navbar-toggle collapsed">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
.navbar-toggle {
outline: none;
width: 30px;
height: 50px;
@Farmatique
Farmatique / gist:d5756faf782b784d273b02a18257ed1c
Last active June 22, 2018 14:41
Pure JS Class helpers addclass, removeclass
function hasClass(ele,cls) {
return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
if (!hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
if (hasClass(ele,cls)) {
var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
ele.className=ele.className.replace(reg,'');
function Event(name){
this.name = name;
this.callbacks = [];
}
Event.prototype.registerCallback = function(callback){
this.callbacks.push(callback);
}
function Reactor(){
this.events = {};
function Observable() {
var observers = [];
this.sendMessage = function(msg){
for(var i = 0, length = observers.length; i < length; i++){
observers[i].notify(msg)
}
}
this.addObserver = function(observer){
observers.push(observer)
}
@Farmatique
Farmatique / gist:7ef83fb49188b4fa362fcece90755196
Created June 14, 2018 10:35
Call function once when scrolled toa certain position
window.onscroll = myScroll;
var counter = 0; // Global Variable
function myScroll(){
var val = document.getElementById("value");
val.innerHTML = 'pageYOffset = ' + window.pageYOffset;
if(counter == 0){ // if counter is 1, it will not execute
if(window.pageYOffset > 300){
alert('You have scrolled to second div');
counter++; // increment the counter by 1, new value = 1
}