Skip to content

Instantly share code, notes, and snippets.

View nicovanzyl's full-sized avatar
🖥️
Building things for humans on computers.

Nico van Zyl nicovanzyl

🖥️
Building things for humans on computers.
View GitHub Profile
<div class="space" style="height: 300px">
<div class="item moon"><img src="http://nicovanzyl.com/external/files/img/moon.png"></div>
<div class="item ship"><img src="http://nicovanzyl.com/external/files/img/small-ship.png"></div>
<div class="item earth"></div>
<!--(optional)<div class="item shadow"></div>-->
</div>
@nicovanzyl
nicovanzyl / NaN-batman.js
Last active December 3, 2019 13:43
na na na na na na na na na na na na na na na na Batman! https://www.youtube.com/watch?v=EtoMN_xi-AM
Array(16).join('wat' - 1) + ' Batman!';
@nicovanzyl
nicovanzyl / animate-scroll.js
Created October 16, 2015 11:53
jQuery animate scroll to element (using anchor href and element ID)
// Animate scrolling
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
},
@nicovanzyl
nicovanzyl / ionic-commands.sh
Last active October 16, 2015 13:06
Frequently used ionic commands
# Create local server to run app in the browser with live reload
ionic serve
# Create local server to run app in the browser with android and ios styles side by side
ionic serve --lab
# Run app on device emulator
ionic run android
# Run app on device emulator with live reload features
@nicovanzyl
nicovanzyl / jquery.same-height.js
Created October 20, 2015 13:02
Make two elements the same height
var $columns = $('.column');
var height = 0;
$columns.each(function () {
if ($(this).height() > height) {
height = $(this).height();
}
});
$columns.height(height);
@nicovanzyl
nicovanzyl / distance.js
Last active November 17, 2015 07:09
Get the distance between two lat-long points
// Option 1
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
@nicovanzyl
nicovanzyl / jquery-api-request
Created January 14, 2016 08:35
request shorthand
function api_request(url, type, data, callback, error) {
$.ajax({
type: type,
url: url,
data: data,
error: function(xhr, opt, err) {
if (error && typeof(error) === "function") {
loadError(xhr, opt, err);
} else {
loadError(xhr.statusText);
@nicovanzyl
nicovanzyl / delay-typing
Last active January 14, 2016 08:43
will fire function after user has stopped typing for a set time
var typewatch = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
// Sample
$(document).on('keyup', '#branch_address', function(event) {
@nicovanzyl
nicovanzyl / location-selector
Created January 14, 2016 08:42
Map that works with form fields - supports lat, long & address
if ($('.location-selector').length > 0) {
var map;
var markersArray = [];
// init map
function initMap() {
var currentLat = -33.9253;
var currentLng = 18.4239;
var branchLocation = {lat: currentLat, lng: currentLng};
@nicovanzyl
nicovanzyl / elementInViewport.js
Created October 6, 2016 07:24
Check if an element is in the viewport - vanilla JS
function elementInViewport(el) {
var top = el.offsetTop;
var left = el.offsetLeft;
var width = el.offsetWidth;
var height = el.offsetHeight;
while(el.offsetParent) {
el = el.offsetParent;
top += el.offsetTop;
left += el.offsetLeft;