Skip to content

Instantly share code, notes, and snippets.

View tobiasroeder's full-sized avatar
:octocat:
I <3 </>

Tobias Röder tobiasroeder

:octocat:
I <3 </>
View GitHub Profile
@tobiasroeder
tobiasroeder / mixArray.js
Last active September 27, 2023 09:52
A way to mix an array in JavaScript.
// For production use i recommend: https://phuoc.ng/collection/1-loc/shuffle-an-array/
// function
function mixArray(array) {
let newArray = [],
maxRandNbr = array.length;
for (let i = 0; i = maxRandNbr; i++) {
randNbr = Math.round(Math.random() * (maxRandNbr - 1));
@tobiasroeder
tobiasroeder / fade.js
Last active October 10, 2019 20:27
Simple fade in/out function without any library. (pur JavaScript)
// source: https://chrisbuttery.com/articles/fade-in-fade-out-with-javascript/
// fadeOut (fade.js)
function fadeOut(el) {
el.style.opacity = 1;
(function fade() {
var val = parseFloat(el.style.opacity);
if (!((val -= .1) < 0)) {
el.style.opacity = val;
requestAnimationFrame(fade);
@tobiasroeder
tobiasroeder / loadJSON.js
Created July 13, 2019 14:23
A function which load the content of a json file.
// load JSON
function loadJSON(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
} else {
// callback([{}]);
@tobiasroeder
tobiasroeder / splitter.js
Created July 13, 2019 14:22
A function which splitter a number.
// 1234 => 1.234
function splitter(t) {
if (t = '' + t,
t.length > 3) {
var e = t.length % 3,
n = e > 0 ? t.substring(0, e) : '';
for (i = 0; i < Math.floor(t.length / 3); i++) n += 0 == e && 0 == i ? t.substring(e + 3 * i, e + 3 * i + 3) : '.' + t.substring(e + 3 * i, e + 3 * i + 3);
return n;
}
@tobiasroeder
tobiasroeder / data.css
Created June 22, 2019 08:22
CSS before/after content property with the content from an attribute.
[data-before]::before {
content: attr(data-before);
}
[data-after]::after {
content: attr(data-after);
}
@tobiasroeder
tobiasroeder / readJSON.js
Created June 5, 2019 14:36
Pur JavaScript to read a JSON file for the YouTube, GitHub or other API
fetch(YOUR_JSON_FILE_URL).then(function(responsive) {
return responsive.json();
}).then(function(obj) {
console.log(obj);
});