Skip to content

Instantly share code, notes, and snippets.

View AllThingsSmitty's full-sized avatar

Matt Smith AllThingsSmitty

View GitHub Profile
@AllThingsSmitty
AllThingsSmitty / script-loaded.js
Created May 5, 2015 13:25
Check if any given script has loaded
var myScript = document.createElement('script');
myScript.src = 'http://code.jquery.com/jquery-2.1.4.min.js';
myScript.onload = function() {
console.log('jQuery loaded.');
};
document.body.appendChild(myScript);
@AllThingsSmitty
AllThingsSmitty / responsive-font-size.css
Created May 12, 2015 15:01
Font size based on viewport size
/* base font size + viewport height + viewport width */
h1 {
font-size: calc(2rem + 4vh + 4vw);
}
/* responsive font-size responsive */
html {
font-size: calc(100% + .2vh + .2vw);
}
@AllThingsSmitty
AllThingsSmitty / comments.scss
Created May 12, 2015 15:06
Different types of comments used in Sass
/*
This is for multiline comments
It will output into your CSS depending on your mode.
If you are using the compressed mode, this will be removed.
*/
/* This can be used for inline comments */
/*!
This is for multiline comments.
@AllThingsSmitty
AllThingsSmitty / gwen.py
Created June 2, 2015 15:10
Hollaback console
print "This s**t is bananas"
for a in "bananas":
print a
@AllThingsSmitty
AllThingsSmitty / css-not.scss
Last active November 28, 2018 16:16
Use CSS :not() instead of applying and unapplying borders on navigations
.nav-tab {
...
// instead of putting it on
border-right: 1px solid #424242;
&:last-child {
border-right: 0; // and then taking it off
}
// use CSS not() to only apply to the elements you want
&:not(:last-child) {
border-right: 1px solid #424242;
@AllThingsSmitty
AllThingsSmitty / querySelector.js
Last active August 20, 2022 13:32
Use querySelector with .bind() as a shortcut to familiar function names
// returns first element selected - $('input[name="food"]')
var $ = document.querySelector.bind(document);
// return array of selected elements - $$('img.dog')
var $$ = document.querySelectorAll.bind(document);
// Credit: https://twitter.com/wesbos/status/608341616173182977
@AllThingsSmitty
AllThingsSmitty / dimensions.js
Last active October 7, 2021 16:06
Every possible way (pretty much) to get the dimensions of any image on a web page with JavaScript
var myImg = document.querySelector('img'),
op = document.querySelector('output');
op.innerHTML = "Computed width: " +
getComputedStyle(myImg).getPropertyValue('width') +
'<br>' + 'img.width: ' +
myImg.width +
'<br>' + 'getAttribute(width): ' +
myImg.getAttribute('width') +
'<br>' + 'img.naturalWidth: ' +
@AllThingsSmitty
AllThingsSmitty / wrap.js
Created June 19, 2015 12:51
Great way to wrap non-React UI elements that manage lists of items and leverage VDOM diffing
const SourceElement = React.createClass({
componentDidMount: f() {
this.props.view.addSource(this.props.source);
},
componentWillUnumount: f() {
this.props.view.removeSource(this.props.source);
},
componentWillReceiveProps: f(nextProps) {
@AllThingsSmitty
AllThingsSmitty / index.htm
Created June 26, 2015 00:07
Using Level 4 CSS validity pseudo-classes to improve form UX
<div class="container">
<form class="form">
<div class="form-group">
<label class="sr-only" for="emailAddress">Email Address</label>
<input class="form-control input-lg" type="email" placeholder="[email protected]" id="emailAddress" required>
<button type="submit" class="btn btn-default input-lg">Subscribe</button>
</form>
</div>
@AllThingsSmitty
AllThingsSmitty / replace.js
Last active July 22, 2017 18:36
Using String.replace() with regular expressions
var myString = 'EXAMPLEstring';
var myNewString = myString.replace(/[A-Z]/g, '0');
console.log(myNewString);
function replaceFunc (a, b, c) {
console.log(a, b, c);
return a.toLowerCase();
}
var myOtherString = myString.replace(/[A-Z]/g, replaceFunc);