codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Add an awesome glitch effect to your webpage elements

Vaibhav Khulbe
codeburst
Published in
4 min readOct 9, 2018
Glitches are cool. Aren’t they? GIF credits: Charlie Mars

I, (with not-so-super web development knowledge), welcome you all to the world of g-l-i-t-c-h-e-s. Hah, I thought writing glitches like that would be cool, but sadly, it isn’t…Anyway, we all have seen glitches in our earlier television sets, right? Now, (for no reason), let’s re-create that effect in our computers! Exciting? Let’s dive in!

What will I make?

This:

I know that looks cool…and haunting too.

Not this:

Duh. GIF credits: G1ft3d

How will I achieve this❔

Library! I’ll be using the mgGlitch library by hmongouachon. This is used to add the glitch effect to almost any of the element(s) you want to. There are three simple steps to achieve this:

  1. Select the element which you want to glitch, like the image (<img>).
  2. Add the jQuery library to the source.
  3. Initiate the plugin with really easy code. See the demo.

Okay, I won’t go into what to write and what not to. This section is just for an overview of the library. Feel free to contribute by opening a pull request or by creating an issue. Let’s move on now.

Oh wait! There is a live example by the mgGlitch developer on CodePen. See this:

What will I need? 🛠️

Shall we, glitchy-glitch?

Prepare your screen to get glitched! GIF credits: GIPHY

We need a jQuery CDN. Grab the uncompressed one (which I’ve used). Add it to the <script> tag to your HTML page which will have the following elements:

  1. Another <script> to add the mgGlitch.min.js file as src. Make sure you add that JS file in your project folder.
  2. In the <body> add a container class having a glitch-img class. This inner class will have its src set to the glitch-image.jpg which I got from Pexel. Also, you can add a <h3> header with some text on it…well, it’s completely optional.
  3. For the next step, we will use the mgGlitch library. Start by declaring a function in jQuery style. Name it as mgGlitch and target our.glitch-img class. This is the class we want to glitch. After this, we will add few properties like destroy, glitch, scale, blend, blendModeType. Feel free to experiment on those. They accept a boolean value, but make sure you don’t set glitch to false, else that awesome effect won’t work.
  4. We can create more than one glitch effects for the selected element. This is achieved by setting the glitch1TimeMin, glitch2TimeMin and their corresponding max values. As described in its documentation:

This plugin will clone the selected element (html or image) 3 times :

first element become a static background

next element with glitch property and lower interval

next element with glitch property, higher interval and scale options

next element with glitch property, higher interval, scale options and blend mode apply

There’s nothing much exciting in the CSS. For the text I’ve used the CF Glitch City font which is available for free via Fontspace. Just note one thing that for the element we want to animate with this effect, it must have position: absolute;. For those who really want to see both the HTML file and the CSS file code, here it is:

HTML/jQuery code
CSS code

Where to next? 🤔

In this I glitched the background image, you can go further by applying the glitch to your favorite elements of your web-page. What about displaying an old commercial video with that glitch effect? It will surely look like we’re watching an old television from 80’s or 90’s! Here are some more resources on this plugin and others:

Liked this story? Feel free to clap and motivate me to write more and better. Did I missed something? Any suggestions? The comment box below serves the exact purpose!

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bursts of code to power through your day. Web Development articles, tutorials, and news.

✦ Independent web developer/designer/blogger ✦ Framer Developer at Moving Rectangles ✦ Framer Expert & Partner | Advocate at 10x Designers

Responses (2)

Write a response