Nyx

Chapter 8 · Core

Animation

A little movement helps visitors notice what changed: a message fading in, a menu sliding open, a wrong answer shaking. In this chapter you'll learn Nyx's ready-made animations, how to make your own, and how to run several in a row.

How long it takes

Every animation takes an optional time: how long it should last. You write it the way you'd say it, as text:

You writeHow long
"300ms"300 milliseconds, about a third of a second. This is what Nyx uses if you don't give a time.
"500ms"Half a second. A good speed for most things.
"1 second"One second. "1s" and "1 sec" work too.
"1.5 seconds"One and a half seconds.
"a second"Also one second.
800A plain number means milliseconds: 800 milliseconds.

A millisecond is a thousandth of a second, so "1000ms" is the same as "1 second". Most animations feel best between "200ms" and "600ms". Anything slower starts to feel like waiting.

Fading

.fadeIn()Show a hidden element by slowly making it appear.
.fadeOut()Slowly make it disappear, then hide it.
.fadeToggle()Fade out if it's showing, fade in if it's hidden.
School trip forms are due on Friday.

Press Run, then click the button a few times. Try "2 seconds" to see it slowly.

Sliding

Sliding folds an element open or closed, like a blind. It's perfect for menus and question-and-answer lists, because the content below moves smoothly instead of jumping.

.slideDown()Unfold a hidden element.
.slideUp()Fold it closed, then hide it.
.slideToggle()Fold it open or closed, whichever it isn't.

How do I join the science club?

Is there a fee?

Each answer starts with the hidden attribute. .next() (from Moving around) finds the answer right after the clicked question.

Moving

.move(x, y) slides an element away from where it normally sits:

.move(Move it 150,right (pixels) 0,down (pixels) "1 second"how long )Done
First numberLeft and right. Positive goes right, negative goes left.
Second numberUp and down. Positive goes down, negative goes up. (On web pages, numbers grow downwards.)

The numbers always count from the element's starting place, not from where it was a moment ago. So .move(0, 0) always takes it home, and doing .move(150, 0) twice leaves it in the same spot.

Getting attention

Three short animations that say something without words:

.shake()Wiggle side to side, like shaking your head: "no". Use for a wrong answer or a mistake in a form.
.pulse()Grow a little and back: "look here". Use when something has just updated.
.bounce()Hop up: "yes!". Use for success.
Watch me

Keep these for moments that matter. If everything wiggles, nothing stands out.

Your own animation

.animate() smoothly changes any styles you choose, from what they are now to new values. You give it the styles in curly brackets, the same way as .css() in Styles and visibility:

Loading…
  1. { width: 300, ... }The styles to change, and their new values. Plain numbers mean pixels, just like .css().
  2. "1.5 seconds"How long the change takes.

When it finishes, the new styles stay. Styles with numbers or colours can be animated: sizes, spacing, opacity, fontSize, borderRadius, colours and more. Styles that only switch between words, like display, jump at the end instead of changing smoothly.

Changing the speed curve

An optional third value says how the speed changes along the way. This is called the easing:

"ease"Starts gently, speeds up, slows at the end. Nyx uses this if you don't choose.
"linear"The same speed the whole way, like a machine.
"ease-in"Starts slowly and speeds up. Good for things leaving the screen.
"ease-out"Starts fast and slows down. Good for things arriving.
"ease-in-out"Slow at both ends, faster in the middle.
nyx("#a-bar").animate({ width: 300 }, "2 seconds", "linear");

One after another

When you chain animations on the same element, Nyx runs them in order: each one waits for the one before it to finish. Use .wait() to add a pause in between.

This reads like a recipe: fade in, wait two seconds, fade out. It's the usual way to show a short message.

Different elements move at the same time

The waiting only happens on the same element. If you animate two different elements, they move together. That's useful: nyx("#a-old").fadeOut(); nyx("#a-new").fadeIn(); swaps two boxes at once.

Running code when it finishes

Code on the next line runs straight away, without waiting for the animation. To run code after it finishes, put it in .done() at the end of the chain:

Welcome back, Asha!

Popup open

Notice the order: the Output shows the print() line immediately, but the status only changes a second later, when the fade has finished.

.done() always goes last, because it waits for everything before it. If you know async functions, you can also write await nyx("#a-popup").fadeOut().done();. You'll meet await properly in Servers and AJAX.

Stopping an animation

.stop() makes the animation that's playing jump straight to its end. It's handy when a visitor clicks quickly and you don't want them to wait.

Slow
Kind to everyone, automatically

Some people feel dizzy or unwell when things move on screen, so they ask their computer for reduced motion. Nyx notices this setting and finishes animations instantly for them. Your page still works the same; it just doesn't move. You don't need to write anything.

Putting it together

A "Save" button that shows a message, waits, then hides it again, and only lets you save once the message is gone. Press Run, then click Save.

  1. button.prop("disabled", true);Switch the button off, so it can't be clicked twice.
  2. .slideDown().wait("1.5 seconds").fadeOut()Unfold the message, keep it for a moment, then fade it away. Each step waits for the one before.
  3. .done(() => { ... })Once all of that has finished, switch the button back on.

Common mistakes

MistakeFix
.fadeOut("slow")Nyx doesn't understand words like "slow" or "fast". Write a time: "600ms". Nyx tells you in the console if it can't read a time.
.hide("500ms").hide() is always instant. Use .fadeOut("500ms") or .slideUp("500ms").
Code after the animation runs too earlyPut it inside .done(() => { ... }).
.done(fn).fadeIn().done() must be the last thing in the chain. Put .fadeIn() before it.
Expecting .move() to add upIt counts from the starting place. To go further, use bigger numbers: .move(300, 0).

Exercise

Fill in the blanks, then press Check answers.

1. Make #banner slowly disappear.

nyx("#banner").();

2. Unfold the hidden menu #menu.

nyx("#menu").();

3. Shake #password after a wrong password.

nyx("#password").();

4. Fade in, then pause for 3 seconds, then fade out.

nyx("#tip").fadeIn().("3 seconds").fadeOut();

5. Run code once the fade has finished.

nyx("#tip").fadeOut().(() => { ... });

6. Smoothly grow #bar to 400 pixels wide.

nyx("#bar").({ width: 400 });

Remember

Times are plain English: "500ms", "1 second". No time means "300ms".
.fadeIn(), .fadeOut(), .slideDown(), .slideUp() and their Toggle versions show and hide smoothly.
.move(x, y) counts from the starting place. .move(0, 0) goes home.
.shake() says no, .pulse() says look, .bounce() says yes.
.animate({ ... }, time) smoothly changes any styles you choose.
Chains on one element run in order. .wait() pauses, .done() runs code at the end.