Skip to content

Instantly share code, notes, and snippets.

@lstanard
lstanard / JS Pattern: jQuery plugin
Last active August 11, 2022 16:11
JavaScript Design Pattern: jQuery Plugin Template
(function($){
$.fn.pluginName = function(options) {
var settings = $.extend({
'option': 'value'
}, options||{} );
}
})(jQuery);
@lstanard
lstanard / JS Pattern: jQuery plugin without selector
Created September 16, 2013 15:00
JavaScript Design Pattern: jQuery Plugin Template without Selector
;(function($){
$.pluginName = function(options) {
var settings = $.extend({
'option': 'value'
}, options||{} );
}
})(jQuery);
@lstanard
lstanard / JS Pattern: Module Pattern - Example 02
Last active December 23, 2015 05:09
JavaScript Design Pattern: Module Pattern - Example 02
// Methods available as myFunction.methodTest();
var myFunction = (function(){
var _root = this,
_config = {
'option': 'value'
},
_init = function() {
// Do something
};
@lstanard
lstanard / JS Pattern: Module Pattern - Example 01
Last active December 23, 2015 05:09
JavaScript Design Pattern: Module Pattern - Example 01
// If using jQuery, must be wrapped in a $(document).ready function
$(document).ready(function(){
(function ($, MyObject) {
var _root = this,
_config = {
'option': 'value'
},
@lstanard
lstanard / JS Pattern: Basic IIFE
Created October 8, 2013 13:22
JS Pattern: Basic IIFE
(function(){
/* code */
}());
@lstanard
lstanard / Log window scrollTop
Last active December 25, 2015 00:49
Log current window top position
$(window).on('scroll', function() {
console.log( $(window).scrollTop() );
});
@lstanard
lstanard / jQuery lightweight plugin boilerplate
Created October 18, 2013 02:53
jQuery lightweight plugin boilerplate
/*!
* jQuery lightweight plugin boilerplate
* Original author: @ajpiano
* Further changes, comments: @addyosmani
* Licensed under the MIT license
*/
// the semi-colon before the function invocation is a safety
// net against concatenated scripts and/or other plugins
// that are not closed properly.
@lstanard
lstanard / ScrollTo
Last active August 11, 2022 16:12
Bind smooth scroll event to links
// Setup scrollto links
$('.scrollto').on('click', function(e) {
var targetElement = $('#' + $(this).attr('href').substring(1));
if (targetElement.length >= 1) {
var target = targetElement.offset().top,
scrollSpeed = function() { return Math.ceil( Math.abs( target - $w.scrollTop() ) / 1.5 ); };
$('html, body').animate({
scrollTop: target
}, scrollSpeed() );
}
@lstanard
lstanard / jQuery resize listener
Created March 5, 2014 14:38
Resize listener to call functions based on window size
$(document).ready(function() {
var mobile,
w = $(window),
breakpoint = 640,
sw = document.documentElement.clientWidth,
sh = document.documentElement.clientHeight;
w.on('resize orientationchange', function() {
sw = document.documentElement.clientWidth;
@lstanard
lstanard / waitForFinalEvent()
Created March 18, 2014 14:06
Call window resize event only when finished
// Call window resize function only once finished
var waitForFinalEvent = (function () {
var timers = {};
return function (callback, ms, uniqueId) {
if (!uniqueId) {
uniqueId = "Don't call this twice without a uniqueId";
}
if (timers[uniqueId]) {
clearTimeout (timers[uniqueId]);
}