1 of 23

Pseudo-Classes, Transitions, & Keyframes

Fall 2023

2 of 23

Announcements

  • Quiz 2 review this Friday (10/13)
  • Quiz 2 in class on Monday (10/16)
  • Homework 5 due Wednesday (10/18)
    • I strongly recommend that you complete HW5 as soon as you can – a way to study for the quiz
  • Before we start class today, please download the zip files for both activities for Lecture 13:

3 of 23

Outline

  1. Pseudo-classes
  2. Transitions
  3. Preview: Using Transitions with JavaScript
  4. Intro to Keyframes
  5. Websites of Inspiration
  6. Activity

4 of 23

Outline

  • Pseudo-class review
  • Transitions
  • Preview: Using Transitions with JavaScript
  • Intro to Keyframes
  • Websites of Inspiration
  • Activity

5 of 23

Pseudo-class Review

“A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, the pseudo-class :hover can be used to select a button when a user's pointer hovers over the button and this selected button can then be styled.”

When do you use them (see demos):

  • If a user interacts with an element, you can apply a special style that “turns on” when the interaction happens.
  • You can also select an element in particular situation (e.g., “all even elements”) and apply a styles to it.

6 of 23

Pseudo-class Demos

Demo walkthrough (transitions/01-pseudo-classes):

  1. Pseudo-classes typically used for links
  2. Pseudo-classes used to select elements that match a pattern
    • e.g. li:nth-of-type(even) {� border: 2px solid orange;�}
  3. Pseudo-classes typically used for forms (e.g., input:focus)
  4. Pseudo-classes for adding additional content (::before and ::after)
  5. Pseudo-classes for contextually showing / hiding information

7 of 23

Outline

  • Pseudo-classes
  • Transitions
  • Preview: Using Transitions with JavaScript
  • Intro to Keyframes
  • Websites of Inspiration
  • Activity

8 of 23

Transitions (Review)

Transitions are often used in combination with a pseudo-class to “transition” between the off state and the on state using animation.

  • You can animate colors, sizes, padding and margin – basically anything that can be represented as a number.
  • Transitions allow you specify:
    • The property or properties you want to animate
    • How long the animation will take
    • The timing function
    • Whether there should be a delay in the animation

8

9 of 23

Transition Syntax

div {

transition: <property> <duration> <timing-function> <delay>;

}

9

10 of 23

Transition: Example

a {

color: red;

transition: all 0.3s ease-out 0s;

transition-property: color, margin;

}

a:hover {

color: blue;

font-size: 200%;

}

10

11 of 23

Transitions: Demo

02-pseudo-classes-with-transitions

  • Think about the kinds of property changes that might result in an interesting transition effect (text and background color changes, width, height, rotation, border radius, margin, padding, etc.)
  • Experiment with some of the transform property and the many functions that can be applied to it (rotate, skew, translate, scale, etc.)

11

12 of 23

Outline

  • Pseudo-classes
  • Transitions
  • Preview: Using Transitions with JavaScript
  • Intro to Keyframes
  • Websites of Inspiration
  • Activity

13 of 23

Preview: Using Transitions with JavaScript

Transitions are often used in combination with JavaScript (which we’ll talk more about after Spring Break. Let’s take a look at some techniques that use transitions and JavaScript and talk through how they work…

  • transitions/sample-files/advanced-techniques/text-animations
  • transitions/sample-files/advanced-techniques/scrollytelling_demos

14 of 23

Outline

  • Pseudo-classes
  • Transitions
  • Preview: Using Transitions with JavaScript
  • Intro to Keyframes
  • Websites of Inspiration
  • Activity

15 of 23

Keyframe Animations

What is a keyframe?

Keyframes are a way to make more complex animations.

  • You define a series of keyframes in a separate block inside your CSS file and give it a name.
    • Inside the keyframe definition, you specify what should happen at different time intervals.
  • You then apply the keyframe to specific elements.

16 of 23

Example: Define the keyframe, then apply it

#myMessage {

animation: bounceIn 2s ease-in-out;

}

@keyframes bounceIn {

0% {

transform: scale(0.1);

}

60% {

transform: scale(1.2);

}

100% {

transform: scale(1);

}

}

17 of 23

Animation properties

animation-name

Name of the keyframe you defined.

animation-timing-function

e.g., linear, ease-in, ease-out

animation-delay

how long to wait before animation starts

animation-iteration-count

How many times animation should repeat

animation-direction

normal, reverse, alternate, alternate-reverse

animation-fill-mode

e.g., forwards, backwards, normal, both

animation-play-state

e.g., paused, running

18 of 23

Demos

Let’s check out the demo files!

  • keyframes/samples/01-the-basics
  • keyframes/samples/02-heart-infinite
  • keyframes/samples/03-spin-activate-on-hover

19 of 23

Outline

  • Pseudo-classes
  • Transitions
  • Preview: Using Transitions with JavaScript
  • Intro to Keyframes
  • Websites of Inspiration
  • Activity

20 of 23

Gallery of Inspiration

There are also a ton of animation libraries that you can use to jazz up your websites. For instance:

21 of 23

Libraries for implementing scrolling effects…

There are also a ton of animation libraries that you can use to jazz up your websites. For instance:

22 of 23

Outline

  • Pseudo-classes
  • Transitions
  • Preview: Using Transitions with JavaScript
  • Intro to Keyframes
  • Websites of Inspiration
  • Activity

23 of 23

Pick an Activity

Pick one activity and complete it:

If you need to make up a tutorial, you can submit this as a completed tutorial assignment. Just email Sarah the link to your completed activity (linked from your homepage).