אנימציות ומעברים

Transition (מעברים)

מעבר חלק בין מצבים:

.button {
    background-color: blue;
    transition: background-color 0.3s ease;
}

.button:hover {
    background-color: darkblue;
}

תכונות transition

.element {
    transition: all 0.3s ease 0s;
    /* property duration timing-function delay */
}

Timing Functions

פונקציה התנהגות
linear קבוע
ease ברירת מחדל
ease-in מתחיל איטי
ease-out מסיים איטי
ease-in-out איטי בהתחלה ובסוף

Transform

translate (הזזה)

transform: translate(50px, 100px);
transform: translateX(50px);
transform: translateY(100px);

rotate (סיבוב)

transform: rotate(45deg);
transform: rotate(-90deg);

scale (גודל)

transform: scale(1.5);
transform: scale(0.5);

skew (הטיה)

transform: skew(10deg, 5deg);

Animation

תחביר בסיסי

@keyframes slide-in {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

.element {
    animation: slide-in 0.5s ease forwards;
}

עם אחוזים

@keyframes bounce {
    0% { transform: translateY(0); }
    50% { transform: translateY(-30px); }
    100% { transform: translateY(0); }
}

.ball {
    animation: bounce 1s ease infinite;
}

תכונות animation

תכונה ערכים
animation-name שם keyframe
animation-duration 1s, 300ms
animation-iteration-count infinite, 3
animation-direction normal, alternate
animation-fill-mode forwards, backwards

דוגמאות

Loading Spinner

@keyframes spin {
    to { transform: rotate(360deg); }
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #3498db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

Hover Effect

.button {
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.button:hover {
    transform: translateY(-3px);
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}

הבא: שיטות עבודה מומלצות ➡️

בדקו את עצמכם

נסו לענות לבד לפני שאתם פותחים את התשובה.

  1. מה ההבדל בין transition ל-animation?

    1. אין הבדל
    2. transition מגיב לשינוי מצב (כמו hover), animation רץ לפי keyframes בעצמו
    3. animation רק לטקסט
    4. transition איטי יותר
    הצגת התשובה

    תשובה ב. transition עובר בין שני מצבים, animation מגדיר רצף שלם.

  2. אילו מאפיינים הכי זולים להנפשה?

    1. top ו-left
    2. margin ו-padding
    3. width ו-height
    4. transform ו-opacity
    הצגת התשובה

    תשובה ד. transform ו-opacity לא גורמים לחישוב פריסה מחדש, ולכן הם חלקים.

  3. מה לעשות עבור משתמשים שביקשו פחות תנועה?

    1. להאט את האנימציות
    2. להסתיר את הדף
    3. כלום
    4. לכבות או לצמצם אנימציות עם @media (prefers-reduced-motion: reduce)
    הצגת התשובה

    תשובה ד. אנימציות יכולות לגרום סחרחורת. כיבוד ההעדפה הוא חלק מנגישות.