Skip to content

Instantly share code, notes, and snippets.

@LindseyWhitney
LindseyWhitney / Scroll to Anchor Tags
Created December 16, 2015 22:07
Scroll to anchor tags rather than jumping.
// Smooth scrolling on anchor tags -------------------------
// Reference: https://css-tricks.com/snippets/jquery/smooth-scrolling/
$(function() {
$('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({
@LindseyWhitney
LindseyWhitney / Reveal Fixed Nav on Scroll Up
Created December 14, 2015 23:32
JavaScript to reveal a fixed header on scroll up only. It is hidden on start, and on scroll down.
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('nav').outerHeight(); // change 'nav' depending on what your containing element is.
var didScroll;
// on scroll, let the interval function know the user has scrolled
$(window).scroll(function(event){
didScroll = true;
});
@LindseyWhitney
LindseyWhitney / CSS Arrows
Created December 10, 2015 21:21
Creating arrows using CSS only.
.arrow-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
.arrow-down {
@LindseyWhitney
LindseyWhitney / Clear Form
Created November 3, 2015 20:06
Clears various types of form input.
//jQuery Plugin
$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = '';
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
@LindseyWhitney
LindseyWhitney / Orphan Removal
Created November 3, 2015 19:59
Removes orphans, so that a single word is never by itself on a line.
$("h2 a").each(function() {
var wordArray = $(this).text().split(" ");
if (wordArray.length > 1) {
wordArray[wordArray.length-2] += " " + wordArray[wordArray.length-1];
wordArray.pop();
$(this).html(wordArray.join(" "));
}
});