Skip to content

Instantly share code, notes, and snippets.

View jayllellis's full-sized avatar

Jason Ellis jayllellis

View GitHub Profile
@jayllellis
jayllellis / element-exists.js
Last active August 29, 2015 14:05
jQuery check if element exists
/**
* @desc checks if current element exists
* @param none
* @return int - the number of elements present, > 0 = element exists
*/
jQuery.fn.exists = function(){ return this.length > 0; }
// Usage:
if( $('#my-div').exists() ){
// do something
@jayllellis
jayllellis / is-on-screen.js
Last active August 29, 2015 14:05
jQuery check if element is on screen
/**
* @desc checks if current element is on screen or within viewport
* @param none
* @return int - the coordinates of the window vs. the element
*/
jQuery.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
@jayllellis
jayllellis / setcookie.php
Last active August 29, 2015 14:06
PHP setcookie
<?php
if( isset($_COOKIE['cw_section']) ){// cookie is set
$redirect_section = $_COOKIE['cw_section'];
if($redirect_section == 'doctor'){
header('Location: http://www.example.com/doctor/');
}
else{
header('Location: http://www.example.com/patient/');
}
}
@jayllellis
jayllellis / equal-heights.js
Last active January 16, 2017 21:45
Simple jQuery equal height columns
/**
* @desc create equal height columns
* @param object - the elements to apply equal heights to
* @return none
*/
jQuery.fn.equalHeights = function(){
var colSelector = this.selector;// Get the selector of the object
var newHeight;
var colHeights = [];
$(colSelector).css('height', '');// Clear heights first
@jayllellis
jayllellis / top-is-visible.js
Created January 21, 2015 14:12
Top of element becomes visible within viewport
/**
* @desc checks if current element is on screen or within viewport (as soon as it becomes visible)
* @param none
* @return int - the coordinates of the window vs. the element
*/
jQuery.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
@jayllellis
jayllellis / nav-scroll-active.js
Created March 4, 2015 02:49
Change nav items to active as you scroll
$(window).scroll(function() {
$('section').each(function(){
var currentSection = $(this);
var currentId = $(this).attr('id');
if(currentSection.isOnScreen()){
$('.sidenav a').removeClass('active');
$('a[href="#'+currentId+'"]').addClass('active');
}
});
@jayllellis
jayllellis / custom-excerpt-length.php
Created April 2, 2015 15:00
WordPress Custom Excerpt Length
/*************************************************************************************
* Custom excerpt length
*************************************************************************************/
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
@jayllellis
jayllellis / content-changed.js
Created April 6, 2015 04:28
Detect when an element in the DOM's content changes.
$(document).ready(function(){
$('.some-element').bind('DOMSubtreeModified', function() {
// Do something
});
});
@jayllellis
jayllellis / scroll-stop.js
Created July 3, 2015 14:10
When the user stops scrolling, do something
(function($) {
"use strict";
$(document).ready (function(){
$(window).scroll(function(){
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {// when the user stops scrolling
// do something
}, 500));
});
});// end document ready
@jayllellis
jayllellis / done-resizing.js
Created July 16, 2015 18:01
Detects when the user is done reszing
var resizeTimer;
$(window).on('resize', function(e) {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
// Run code here, resizing has "stopped"
}, 250);