Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (50)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

Walking Man Animation Using HTML, CSS, and JavaScript
ProgrammingCSSHTMLJavaScript

Walking Man Animation Using HTML, CSS, and JavaScript

By Admin
November 30, 2024 3 Min Read
0

Animation can add life to your website, creating interactive and engaging user experiences. This tutorial will show you how to build a side-scrolling effect with a walking character using HTML, CSS, and JavaScript. With keyboard controls, users can move the character in both directions while the background scrolls smoothly.

The images used in the source code is given below:

man.png
reverse-man.png
bg.jpg

Key Features of the Animation

  • A seamless, horizontally scrolling background.
  • A walking character controlled using the arrow keys.
  • Realistic sprite-based animation for the character.

The HTML Framework

The layout consists of a container for the scrolling background and the character. The character is wrapped inside a div to facilitate animations while maintaining a fixed position on the screen.

Styling with CSS

CSS handles the background scrolling and character animations.

  • Background Animation: A repeating background image scrolls horizontally, creating a sense of movement.
  • Sprite Animation: The character’s walking motion is achieved by transitioning between different frames of a sprite sheet using CSS @keyframes and steps().

Interactivity with JavaScript

JavaScript captures keyboard inputs to control the character and background:

  • Pressing the right arrow key (ArrowRight) moves the character forward.
  • Pressing the left arrow key (ArrowLeft) moves the character backward.
  • The character’s animation toggles between walking forward and walking backward.

Follow this video for complete guidance :

https://www.youtube.com/watch?v=7ZvkvJv4ZhI

Step 1: Designing the Character and Background

Prepare the images:

  • A background image (bg.jpg) that repeats seamlessly.
  • A sprite sheet (man.png) for the character walking forward.
  • A reverse sprite sheet (reverse-man.png) for the character walking backward.

Step 2: Adding Keyboard Controls

Using JavaScript, detect the arrow key presses and update the background’s position accordingly. The keydown event initiates the character’s walking animation, while the keyup event stops it.

Step 3: Animating the Sprite

The walking effect is created by shifting the sprite’s position horizontally within a fixed-width container. CSS animations are used to cycle through the frames of the sprite sheet.

Optimizations

  • Preloading Images: Preload the reverse sprite to avoid rendering delays when switching directions.
  • Performance Enhancements: Use optimized sprite sheets and minimal DOM updates for smoother animation.

Full Source Code

<style type="text/css">
	body{
		margin: 0;
	    overflow: hidden;
	}
	.bg{
	    height: 100vh;
	    background-image: url(bg.jpg);
	    background-repeat: repeat-x;
	    background-color:darkgray;
	}
	.player{
		position: absolute;
	    z-index: 10;
	    bottom: 12%;
	}
	.player-wrap{
		width: 210px;
    	height: 230px;
    	overflow: hidden;
	}
	.player-image{
		width: 30400px;
	    height: 600px;
	    background-image: url(man.png);
	    transform: scale(0.4);
	    background-repeat: no-repeat;
    	transform-origin: top left;
	}
	.walking{
		animation: walk .8s steps(38) infinite;
	}
	.reverse-man{
		background-image: url(reverse-man.png);
	}
	.walking-reverse{
    	animation: walk .8s steps(38) infinite reverse;
	}
	@keyframes walk{
		0%{
			background-position: 0 0;
		}
		100%{
			background-position: -30400px 0;
		}
	}
</style>
<body>
    <div class="environment">
        <div class="bg"></div>
        <div class="player" style="left:40%">
            <div class="player-wrap">
                <div class="player-image"></div>
            </div>
        </div>
    </div>

    <img src="reverse-man.png" style="display:none;">

    <script type="text/javascript">
    	const player = document.getElementsByClassName('player-image')[0];
    	const bg = document.getElementsByClassName('bg')[0];
    	var offset = 0;
		document.addEventListener('keydown', handleKeyDown);
    	document.addEventListener('keyup', handleKeyUp);

    	function handleKeyUp(e){
    		player.classList.remove('walking');
    		player.classList.remove('walking-reverse');
    	}

    	function handleKeyDown(e) {
		    if (e.key === 'ArrowLeft') {
		    	player.classList.remove('walking');
		    	player.classList.add('walking-reverse');
		    	player.classList.add('reverse-man');
		        offset += 10;
		    } else if (e.key === 'ArrowRight') {
		    	player.classList.add('walking');
		    	player.classList.remove('walking-reverse');
		    	player.classList.remove('reverse-man');
		        offset -= 10;
		    }
		    bg.style.backgroundPosition = `${offset}px 0`;
		}

    </script>
</body>

This technique is perfect for building interactive web games, storytelling platforms, or any application requiring dynamic character movement.

By combining HTML, CSS, and JavaScript, you can create a visually appealing side-scrolling animation that responds to user input. Try enhancing it further with features like obstacles, scoring, or multi-level environments for a full-fledged interactive experience.

Tags:

animationanimation using HTML and CSSCSScss animationjavascript
Author

Admin

Follow Me
Other Articles
How to Ace Your Software Development Interview
Previous

How to Ace Your Software Development Interview

Next

Simple Personal Blogging Template Using Bootstrap

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS
  • Real-time Speech to Text Typing Tool using JavaScript

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme