Skip to content

Instantly share code, notes, and snippets.

View r4fx's full-sized avatar
👋

Rafał Brzeski r4fx

👋
View GitHub Profile
@r4fx
r4fx / copying-array.js
Created January 4, 2019 10:37
Copying an array
const names = [ 'Jon', 'Jacob', 'Jeff' ]
const copy1 = names.slice()
const copy2 = [].concat(names)
const copy3 = Object.values(names)
const copy4 = [...names]
const copy5 = Array.of(...names)
const copy6 = JSON.parse(JSON.stringify(names))
const copy7 = names.map(i => i)
const copy8 = Object.assign([], names)
@r4fx
r4fx / onoff.js
Created April 21, 2016 07:42
Revert preventDefault
function doPrevent(e) {
e.preventDefault();
}
// Prevent events
$(window).on('scroll mousewheel DOMMouseScroll', doPrevent);
$(window).on('mousemove', function (e) {
// Revert
$(window).off('scroll mousewheel DOMMouseScroll', doPrevent);
@r4fx
r4fx / orphansfix.js
Created April 21, 2016 07:33
Orphans fix
/*
* Usage
*
$(function () {
$(".todayMenu article").removeOrphans();
});
*/
@r4fx
r4fx / objectfit.js
Last active May 18, 2016 15:04
css object-fit polyfill
if ('objectFit' in document.documentElement.style === false) {
var imgToFit = document.querySelectorAll('img.object-fit');
for (var i = 0; i < imgToFit.length; i++) {
var container = imgToFit[i].parentElement;
var imgToFitUrl = imgToFit[i].src;
if (imgToFitUrl) {
container.classList.add('no-object-fit');
container.style.backgroundImage = 'url(' + imgToFitUrl + ')';
@r4fx
r4fx / user-os-browser.js
Last active August 29, 2015 14:18
Get user environment
// ---------------------------------------------------------
// USER OS & BROWSER
// ---------------------------------------------------------
var userEnvironment = (function(){
var userOS = "";
var userBrowser = "";
var mobile = {
deviceOrientation: function(){
@r4fx
r4fx / didscroll.js
Created March 10, 2015 12:33
Scroll events performance
// Resource https://medium.com/@mariusc23/hide-header-on-scroll-down-show-on-scroll-up-67bbaae9a78c
var scrollValInit = 200;
var didScroll;
$(window).scroll(function () {
didScroll = true;
});
setInterval(function () {
@r4fx
r4fx / keys-pressed.js
Last active August 29, 2015 14:16
Events on keybord keys
var keys = {};
$(document).keydown(function (e) {
keys[e.which] = true;
showThemeSelector(e);
});
$(document).keyup(function (e) {
delete keys[e.which];
@r4fx
r4fx / loadscript.js
Last active August 29, 2015 14:15
Script loader
function loadScript(url, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
@r4fx
r4fx / hider.js
Last active August 29, 2015 14:15
Uniwersal hider
// Uniwersal hider
// ============================================================
function hider(targetToHide, trigger) {
$(document).on('mouseup', function (e) {
if (!targetToHide.is(e.target) // if the target of the click isn't the container...
&& targetToHide.has(e.target).length === 0 // ... nor a descendant of the container
&& !trigger.is(e.target)) // ... nor a trigger of the container
{
console.log(targetToHide);
targetToHide.add(trigger).removeClass('active');
@r4fx
r4fx / plugin-destroy.js
Last active August 29, 2015 14:07
plugin destroy
// jak przygotowac metode: http://jsfiddle.net/rafx/rtmtkqto/
// http://stackoverflow.com/questions/3502468/unbind-remove-kill-a-jquery-plugin
// przyklad na pluginie Stellar:
// http://stackoverflow.com/questions/12236631/can-stellar-js-readjust-element-offsets-on-window-resize
$(window).data('plugin_stellar').destroy();
$(window).data('plugin_stellar').init();