Javascript Sorting, Summing, and Rounding

I was running into an instance where I needed to be able to sort some arrays in either ASC or DESC order. I found some nice functions to help with that.

Example Usage:

 

Calculate Age / Date Masking

Javascript is kind of wonky about their dates. Here is a simple function to calculate age based on a given date string.

Note: This is not perfect and does not work with every possible date string, but it works pretty well with all standard US date formats.

Here is some decent code to setup some simple date masking.

 

Force Same Height on All Slick Slides

I love the slick slider, but sometimes the default functionality isn’t quite what I need. For a variety of other reasons the height of the slides can vary from slide to slide There is no built in “force height” option. Although it should be possible using “display: flex”, I try to avoid using flex on dynamic or complex elements that are hard to fail gracefully on.

I found some good code on the forums to force the slick slides to share the same height. I hope it helps you in the future too!

 

 

Quick Javascript Date Format

Format pretty much any valid date and return it in the standard Y-m-d format.

 

Basic Template Engine

Sometimes I need to setup Javascript templating for a page or two, or even just a form. I found a cool solution over on Krasimir Tsonev’s blog. I thought I would save it here for future use.

Template

I don’t really like having to setup long strings in javascript, it is just so ugly. So I use the <script type=”text/template”> approach to separate out my templates from my code. Here is an example.

This is just a basic “Attendee” template for some event. Notice the <%stuff%> inside there. That is where the magic happens.

Template Engine

Here is where the real magic happens.

So as you can see the “TemplateEngine” just takes some input and replaces anything between the <% %> symbols. Krasimir actually has a more functional version setup on his blog as well with support for some logic, but that is more than I needed. And if I ever did need that much functionality I would just turn to a full Template Engine.

Update:

I noticed a strange flaw in this template engine when working with templates that require multiple replacements on the same line of text. Here is another version of the template engine you can use which should find all variables in your template, regardless of how many are on a line.

 

Skrollr Menu

In my last post I talked about extending the easing functionality of jQuery. Here is what I was using that easing for. My custom skrollr menu!

HTML / PHP

First we need to setup the menu itself. Here is a shortened version of what I did.

I am going to skip over the actual skrollr section code because that is a large chunk of code that will vary greatly for each project. I leave that part to you!

Javascript

Here is the code needed to make your skrollr menu work.

There we go! Surprisingly simple to setup your own skrollr menu!

65gpg_tr

Here is what the menu looks like when you are looking at the third section ( …sort of. I didn’t want to screen shot the entire project ).

Note: If you want to setup your own menu for skrollr you should also check out the skrollr menu plugin. It works pretty well, but I had more fun making my own.

Another Note: The ‘easeOutQuad’ function is not built into jQuery. You will need to get jQuery UI or add your own easing functionality to use non standard easing functions. See my previous post.