Animate Elements with CSS Animations & JQuery

October 16, 2016

View Project on CodePen

Today, I wanted to feature a technique I have been using which quickly allows me to animate elements on a page with CSS animations.  It will loop through each element on the page  based on a CSS class and intermittently add an animation to that element.  The method has fall-backs for browsers that do not support CSS animations (in that they just show on page load, but do not animate) so you never have to worry about the content not showing on older browsers.  This method also prevents “glitchy” content blips that tend to occur with JQuery class additions on the document loading.

First, we must start with our HTML content:

It contains 4 paragraphs, all with some Lorem Ipsum.  What’s important is that each element is given a class of “animate”, which we will use as our selector.  The class can be given to any element you were hoping to use a CSS animation for, which opens up the ability to constantly add/remove and dynamically pull elements without ever having to worry about the front-end animation suffering.

This is because of the JQuery you will see below:

Fire this JQuery snippet when your document is ready.  Let’s talk a bit about what is happening here —

First, we use the each() selector to grab each element on the page with the “animate” class.  Then, we are saying: “Every 500 milliseconds, add the class ‘animation’ to that element, wait 500 milliseconds, go onto the next element, add the class to that one, and so on, and so forth.”  This function will go through every element with that class, add the animation class, and do this until they are all done.

I like this method because it delays the animation on each element by 500 milliseconds (which you of course can change the timing of), making a nice presentation no matter how many elements you have on the page.

You can, however, choose to simply queue all of the animations at once with this code instead:

Now that we have these core parts set up, the possibilities become endless with CSS.  I wanted to ensure a fallback in case the CSS animations aren’t supported as well, so we will incorporate the new @supports rule.

I am only relying on the -webkit- vendor prefix and full support, but you could open this up to more browsers by incorporating other vendor prefixes.  I find that this works for a majority of my cases (and will only improve), so it is enough for me.  The best part of this approach is that, worst case scenario, elements are given a class with our JQuery function and nothing happens on the front end.  No content is hidden or lost.

Again, let’s beak this down —

First, we are using the rule declaration and nested a few CSS rules.  If animations are supported, then I want to set the initial state of those elements.  In this case, I am going to fade in these elements, so any element with the animate class is going to need to be hidden:

Next, I want to determine what we do when they are animated.  The class “animation” is being added to each element, so let’s determine what that is:

In this case, the keyframes of the animation are being determined by the “slideIn” animation.  It transforms and fades the element in.  In the CSS class, we are able to determine which animation is used, how long it takes, and the state it is in when the animation is done.

You can change any of these elements and that’s the biggest reason I favor this method over any other.  I can update the CSS constantly, change animations, change how long they animate for, what they do, without ever modifying some of the main core code of HTML and JQuery.

If I ever want to animate another element, I can simply add the “animate” class to it and it will follow the same behavior.  This allows for a robust amount of flexibility.

For example, I can adjust the animation for any element by giving it a new CSS rule without ever touching the HTML or JQuery code.

Say I want to have the first paragraph perpetually loop through its animation.  I can do this with one simple new CSS rule:

And now, the first paragraph will run through the animation after 1 second of delay, run for 2 seconds, and loop infinitely.

View the Demo on CodePen

You can make countless rules and animations for all of the elements on the page, and also determine their state before the animation occurs.

I hope this helps with your front-end development processes going forward.  Feel free to explore this approach.  Hopefully, its ability to scale will really make it a routine method in your process.



Use Contact Form 7 To Post Rows to Advanced Custom Fields Repeater

April 17, 2015

I was in a particular situation where I needed a task management system that could take fields through a form from Contact Form 7 and post them to rows in the Advanced Custom Fields Repeater field I set up.  The idea was to use a form from the front end of WordPress to post to the back-end of WordPress in a custom field.  You can accomplish this by adding code to your functions.php file in the WordPress theme.

This example assumes that you already have a form set up through Contact Form 7 and you have already purchased the Advanced Custom Fields’ Repeater Field plugin.

1. Get Your Field Key

You can grab your field key from the ACF Repeater Field by selecting ‘Show Field Key‘ from the Screen Options.  You will use this in your function to determine which field to add rows to.




2.  Add WPCF7 Before Send Mail Hook

You add the function below to your functions.php file within the theme.  You are hooking into Contact Form 7’s “wpcf7_before_send_mail’ function to grab the post data and post it to a WordPress repeater field as a new row.

Obviously, your fields in the form and the repater field may be different.  The important things to take away from this functions script is that:

You’ve created the hook:


You’ve got the URL and ID of the post the form was submitted through:


You got the data from the form:


And you update the repeater field based on those elements:

Hope that helps!  Don’t forget you can always extend this to a plugin as opposed to a theme function as well.

Force Reload on a Hash or Anchor Change with JQuery

September 2, 2014

I had a JQuery function that detected if there was a hash/anchor tag appended to the loaded URL.  If there was, it would scroll to the anchor tag on the page.

This was the function:

This was great — the problem was, however, that if for some reason the hash changed but the page remained the same, it wouldn’t scroll to the new anchor. This was expected because DOCUMENT.READY will only fire once.

The most common method of detecting a hash/anchor change is using this function:

This will force a reload whenever a hash tag changes. Now, if the URL changes, the page will reload and it will fire my original function detecting a hash/anchor tag.

For this particular instance, this method worked. It doesn’t always, and this may not apply to your problem, but in this case it made sense. It’s also very well supported.

Good luck!

SPAN Simple Responsive Fluid Minimalistic CSS Framework

January 15, 2014

Download on Github

In an attempt to offer some sort of contribution to the web development world, I have decided to put out the SPAN Simple CSS Framework. It’s meant to be a very minimalistic, out-of-the-box CSS framework, and I use the term ‘framework’ very loosely.

A lot of frameworks come with bells and whistles, and that was not my goal in this.  They almost always took on their own unique styling and after a while became recognizable.  Frameworks like Bootstrap were great, but could be identified from a mile away.  Doing custom web development often meant that the style needed to be custom, too.

I would spend so much time coding and customizing the framework, I should have just started from scrap anyway!

The entire point of this framework is to get the absolute bare minimum set up that is relatively easy to learn and rely on.  It’s also very easy to build upon and begin customizing.

This framework does NOT:

  • Take care of resetting all browser styling.
  • Cover all browser quirks and shortcomings.

This framework is:

  • HTML5 and CSS3 compliant.
  • Fluid & Responsive
  • Minified to be only 1.02KB

Getting Started

Download from Github

The entire basis on the framework is that there are containers, rows, and then “SPANs” that total 100%.  For example, if you wanted a row with 4 columns equaling 25%, your markup would look like this:


So, to illustrate this, there is a container that spans 100%, there is a row that spans 960 pixels, then there is a div with a class .span25 that spans 25% of it’s container.

The SPAN Div Class

The grid is defined by the class .span proceeded by the number of percentage you’d like the column to be in width.  All classes that begin with the name “span” carry this behavior:

Here is a full list of the available numerical amounts for each of the span classes:

With these, you can mix and match any combination.  Ideally, you would want it to total 100%, but that’s not necessarily the case if you start a new <div class=”row”> instead.

Clearing Elements

That’s pretty simple.  Just use a div with a class of ‘clear’.

Determining the row size.

Rows are set to be 1080 pixels wide.  The different div span classes do not need to be contained within a row, and they will adjust since the widths are percentages.  You can adjust the row sizing by simply changing the max-width property.

Spacing, Padding, and Responsive Spacing

The .span classes are meant to include padding in the widths using the CSS property of box-sizing:border-box.  This was done intentionally because the hope was to have this framework be more flexible to other applications.  You can always adjust your margins in the universal selector for all of the columns.


That’s about it.   My whole intention was to have it be relatively easy to learn and improve upon.  I wanted a framework that let me build upon it really quickly.

Any comments or suggestions are welcome.

Get the Real URL of a Tiny URL in PHP

October 21, 2013

A while back I set up a website,, that allows me to post Youtube videos of songs I really like. But, I’m lazy, and I wanted to use the least amount of effort to accomplish this.

I ended up setting up a If This Then That recipe for the hashtag “#songdump.” So, whenever I share a song through Twitter, IFTTT grabs the URL and through some other scripts, retrieves all the necessary info I need for that video, and posts it in WordPress (where the data is called from).

Problem is, when you share something on Twitter, it’s often a TinyURL which makes it a lot harder to retrieve info from. Luckily, we can use the PHP function “get_headers()” to accomplish this.

Tiny URL’s come in a lot of different forms, so I used this method because alternatives often had you filter based on a predetermined set of base URLs. That did me no good.

Here’s the script:

Let’s recap what’s happening line by line here –

  1. Storing our tiny URL as a variable, showing 2 examples
  2. Using the get_headers() function to retrieve the location of our URL
  3. This returns an array, and one of values in that array is our URL. It begins with “Location:” or is some cases “location:”. Feel free to use print_r($headers) function before this to learn all of get_headers() returned values
  4. The key for this value is often in different orders (which is why I provided 2 URLs as examples). We will loop through every value to see if it contains “ocation:” (because as I said, sometimes it’s uppercase, sometimes it’s lowercase) and then return that key.
  5. Once we find the right array object, we get its value and store it as a variable.
  6. Using a string replace function, we remove the actual “Location” label. I do this twice, uppercase and lowercase, because it can get returned either way in some cases.
  7. Then we print out the real URL!

Hope that helps! Happy coding!