Image Glitch Effect in HTML

Syiainfoku make a Image Glitch Effect in HTML

Image Glitch Effect in HTML

Hello !

Welcome back, with me the founder of Syiainfoku. You will learn together on the Syiainfoku blog how to make a Image Glitch Effect in HTML.

Ok, let's get started.

Preview Image Glitch Effect

Preview Image Glitch Effect

More or less the result will be like in the image above.

Source Code and Brief Explanation

To make this program [Image Glitch Effect]. First you need to create three Files, one HTML File and one CSS File and lastly one JS File. After creating these files, just paste the following code into your file. You can also download the source code file of this Image Glitch Effect from the given download button.

First, create an HTML file with the name index.html and paste the given code in your HTML file. Remember, you must create a file with an .html extension.

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Glitch Effect | hitoricoding</title>
<link rel="stylesheet" href="./assets/style.css">
</head>

<body>
<div id="wrapper"></div>
<script src="./assets/main.js"></script>
</body>

</html>

Second, create a CSS file with the name style.css and paste the given code in your CSS file. Remember, you must create a file with a .css extension. Background image can be changed according to taste.

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

#wrapper {
position: absolute;
width: 100%;
height: 100vh;
overflow: hidden;
background: url(../images/deb.svg);
background-attachment: fixed;
background-position: center;
background-size: cover;
}

.box {
position: absolute;
background: url(../images/deb.svg);
background-attachment: fixed;
}

Finally, create a JS file with the name main.js and paste the given code in your CSS file. Remember, you must create a file with a .js extension.

let bg = document.getElementById('wrapper');
let count = 20;
for (let i = 0; i < count; i++) {
let glitchBox = document.createElement('div')
glitchBox.className = 'box';
bg.appendChild(glitchBox);
}
setInterval(function () {
let glitch = document.getElementsByClassName('box');
for (let i = 0; i < glitch.length; i++) {
glitch[i].style.left = Math.floor(Math.random() * 100) + 'vw';
glitch[i].style.top = Math.floor(Math.random() * 100) + 'vh';
glitch[i].style.width = Math.floor(Math.random() * 300) + 'px';
glitch[i].style.height = Math.floor(Math.random() * 50) + 'px';
glitch[i].style.backgroundPosition = Math.floor(Math.random() * 50) + 'px';
}
}, 200)

That's it, now you have successfully created a Image Glitch Effect, If your code is not working or you are facing any error/problem, please download the source code file from the given download button. It's free and a .zip file will be downloaded then you have to extract it.

Closing

Thank you for those of you who have read and downloaded this source code, hopefully it can be useful and add to your insight.

If you found this article useful, you can share it. That's all from me, and THANK YOU