Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Friday, 24 January 2025

Lecture Notes Of Class 21: CSS Animations


Lecture Notes Of Class 21: CSS Animations

Objective:

Master animations in CSS to create dynamic, smooth visual effects for web pages.

Introduction to CSS Animations:

CSS animations allow you to create smooth transitions and animations by gradually changing property values over time. This is a powerful tool that enhances user experience on websites. The @keyframes rule defines the animation's behavior, while the animation property is used to apply the animation to elements.


Topics Covered:

1. @keyframes Rule:

The @keyframes rule is used to define the animation's behavior, specifically the intermediate steps between the starting and ending states. It specifies the styles at various points during the animation.

Syntax:

css

CopyEdit

@keyframes animation-name {

  from {

    property: value; /* Starting state */

  }

  to {

    property: value; /* Ending state */

  }

}

  • The from and to represent the beginning and end of the animation.
  • You can use percentage values to specify multiple intermediate steps, not just from and to.

Example:

css

CopyEdit

@keyframes slide {

  0% {

    transform: translateX(0); /* Start at the initial position */

  }

  50% {

    transform: translateX(100px); /* Move to the right by 100px */

  }

  100% {

    transform: translateX(0); /* End at the initial position */

  }

}

2. animation Property:

The animation property is a shorthand for defining an animation's key features. It includes the following components:

  • animation-name: The name of the @keyframes rule to use.
  • animation-duration: The time it takes for the animation to complete one cycle.
  • animation-timing-function: Defines the speed curve (ease-in, ease-out, etc.).
  • animation-delay: Specifies a delay before the animation starts.
  • animation-iteration-count: The number of times the animation should play.
  • animation-direction: Specifies whether the animation should play forwards, backwards, or alternate.
  • animation-play-state: Allows you to control whether the animation is running or paused.

Syntax:

css

CopyEdit

element {

  animation: animation-name duration timing-function delay iteration-count direction;

}

Example:

css

CopyEdit

.bounce {

  animation: bounce 2s ease-in-out 0s infinite;

}

Here, the .bounce class will apply the bounce animation for 2 seconds, using an easing function of ease-in-out, with an infinite number of repetitions.

3. animation-duration:

The animation-duration property specifies how long the animation should take to complete one cycle.

Example:

css

CopyEdit

@keyframes bounce {

  0% {

    transform: translateY(0);

  }

  50% {

    transform: translateY(-50px);

  }

  100% {

    transform: translateY(0);

  }

}

 

.element {

  animation-duration: 2s; /* Animation lasts 2 seconds */

}

The duration can be in seconds (s) or milliseconds (ms). For example, animation-duration: 2s will make the animation last for 2 seconds.

4. animation-iteration-count:

The animation-iteration-count property defines how many times the animation should repeat. It can be set to:

  • A specific number: For example, animation-iteration-count: 3; will play the animation three times.
  • infinite: To make the animation repeat endlessly.

Example:

css

CopyEdit

@keyframes slide {

  0% {

    transform: translateX(0);

  }

  100% {

    transform: translateX(100px);

  }

}

 

.element {

  animation: slide 2s linear 0s infinite;

}

In this case, the animation will repeat infinitely.


Lab Exercise: Create a Bouncing Ball Animation Using CSS

Objective: Create an animated bouncing ball effect using CSS.

Steps:

1.   HTML Structure: Create a simple HTML file with a div element to represent the ball.

html

CopyEdit

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Bouncing Ball Animation</title>

    <link rel="stylesheet" href="styles.css">

</head>

<body>

    <div class="ball"></div>

</body>

</html>

2.   CSS Styling: Style the .ball class to make the ball circular and position it at the bottom of the page.

css

CopyEdit

body {

    margin: 0;

    height: 100vh;

    display: flex;

    justify-content: center;

    align-items: flex-end;

    background-color: #f0f0f0;

}

 

.ball {

    width: 50px;

    height: 50px;

    background-color: #3498db;

    border-radius: 50%;

    animation: bounce 2s ease-in-out infinite;

}

 

@keyframes bounce {

    0% {

        transform: translateY(0);

    }

    50% {

        transform: translateY(-150px); /* Moves the ball up */

    }

    100% {

        transform: translateY(0); /* Moves the ball back down */

    }

}

Explanation:

  • The .ball element is styled as a circle with width and height set to 50px and a border-radius of 50%.
  • The @keyframes rule defines the bounce animation, where the ball moves vertically.
  • translateY(0) starts the ball at its original position, and translateY(-150px) moves the ball upward by 150px.
  • The animation has a duration of 2s, an ease-in-out timing function, and repeats infinitely.

Important CSS Animation Properties Recap:

  • @keyframes: Defines the stages of the animation and its effects.
  • animation: A shorthand property that combines multiple animation-related properties like name, duration, timing-function, and more.
  • animation-duration: Specifies how long the animation lasts for one cycle.
  • animation-iteration-count: Defines how many times the animation should repeat (or set it to infinite for endless repetition).

Conclusion:

CSS animations are a powerful tool to enhance web design. By mastering the @keyframes rule and the animation property, you can create smooth transitions, animations, and dynamic effects to engage users. The bouncing ball animation is a simple yet effective way to demonstrate how these concepts work in practice.



 


Lecture Notes Of Class 19 - CSS Transitions

 


Lecture Notes Of  Class 19 - CSS Transitions

Objective: Learn to animate CSS properties using transitions for dynamic and interactive web pages.


1. Introduction to CSS Transitions

CSS Transitions allow you to make changes to CSS properties over a defined period, creating a smooth animation effect instead of an instant change. They are especially useful for hover effects, button interactions, or any element state changes.


2. Key Properties of CSS Transitions

2.1 transition Property

The transition shorthand property specifies which CSS properties to animate, the duration, timing function, and delay.

Syntax:

css

CopyEdit

transition: property duration timing-function delay;

Components:

  • property: The CSS property to animate (e.g., background-color, width).
  • duration: The time the transition takes (e.g., 1s, 500ms).
  • timing-function: Defines the speed curve of the animation (e.g., ease, linear).
  • delay (optional): The delay before the transition starts (e.g., 0.5s).

Example:

css

CopyEdit

div {

  transition: background-color 1s ease-in-out;

}

2.2 transition-duration

This property sets the time (in seconds or milliseconds) for the transition to complete.

Syntax:

css

CopyEdit

transition-duration: time;

Example:

css

CopyEdit

div {

  transition-duration: 2s;

}

2.3 transition-timing-function

Defines the pace of the transition effect. It controls how the intermediate values are calculated over time.

Common Values:

  • ease (default): Starts slow, speeds up, then slows down.
  • linear: Moves at a constant speed.
  • ease-in: Starts slow and accelerates.
  • ease-out: Starts fast and decelerates.
  • ease-in-out: Combines ease-in and ease-out.

Example:

css

CopyEdit

div {

  transition-timing-function: ease-in;

}


3. Combining Transition Properties

You can combine multiple transition properties using the transition shorthand.

Example:

css

CopyEdit

div {

  transition: all 0.5s ease-in-out;

}

Here:

  • all means all animatable properties will transition.
  • 0.5s is the duration.
  • ease-in-out specifies the timing function.

4. Lab Exercise: Create Hover Effects Using Transitions

Objective: Apply transitions to create smooth hover effects.

Steps:

1.   Create an HTML file with a basic structure.

2.   Style a button with CSS.

3.   Add transitions for hover effects.

HTML Code:

html

CopyEdit

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>CSS Transitions</title>

  <link rel="stylesheet" href="styles.css">

</head>

<body>

  <div class="container">

    <button class="hover-button">Hover Me</button>

  </div>

</body>

</html>

CSS Code:

css

CopyEdit

/* General styles */

body {

  font-family: Arial, sans-serif;

  display: flex;

  justify-content: center;

  align-items: center;

  height: 100vh;

  margin: 0;

  background-color: #f0f0f0;

}

 

.container {

  text-align: center;

}

 

button.hover-button {

  background-color: #4CAF50;

  color: white;

  border: none;

  padding: 15px 30px;

  font-size: 18px;

  border-radius: 5px;

  cursor: pointer;

  transition: background-color 0.5s ease, transform 0.3s ease;

}

 

button.hover-button:hover {

  background-color: #45a049;

  transform: scale(1.1);

}

Explanation:

1.   Initial Styles: The button has a green background, white text, and rounded corners.

2.   Hover Effect:

o    background-color changes smoothly to a lighter green (#45a049).

o    transform: scale(1.1) makes the button slightly larger on hover.

o    The transition property ensures these changes happen over 0.5 seconds for background color and 0.3 seconds for scaling.


5. Summary

  • CSS Transitions create smooth animations between property changes.
  • Use transition-duration to control how long the animation lasts.
  • Apply transition-timing-function to adjust the speed curve of the animation.
  • Combine multiple transitions for more dynamic effects.

Tip: Practice creating hover effects for various elements like images, links, and cards to enhance your web development skills!




Search This Blog

Powered by Blogger.