1.8 אנימציות ומעברים פתרון
פתרון תרגול אנימציות ומעברים
תרגיל 1 - כפתור עם אפקט hover
<button class="hover-btn">Click Me</button>
.hover-btn {
background: #3498db;
color: white;
border: none;
padding: 14px 32px;
font-size: 1.1rem;
border-radius: 8px;
cursor: pointer;
box-shadow: 0 2px 8px rgba(52, 152, 219, 0.3);
transition: background 0.3s ease, transform 0.3s ease, box-shadow 0.3s ease;
}
.hover-btn:hover {
background: #2980b9;
transform: scale(1.05);
box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
}
.hover-btn:active {
transform: scale(0.95);
box-shadow: 0 1px 4px rgba(52, 152, 219, 0.3);
}
transition מוגדר על 3 מאפיינים ספציפיים - לא all, בשביל ביצועים טובים
- ב-hover: הכפתור מתרחב, משנה צבע ומקבל צל גדול יותר
- ב-active: הכפתור מתכווץ ומאבד צל - מרגישים שלוחצים עליו
תרגיל 2 - ספינר טעינה
<div class="loading-container">
<div class="spinner"></div>
<p class="loading-text">loading...</p>
</div>
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.3;
}
}
.loading-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
gap: 15px;
}
.spinner {
width: 50px;
height: 50px;
border: 5px solid #e0e0e0;
border-top-color: #3498db;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
.loading-text {
color: #666;
font-size: 0.9rem;
animation: pulse 1.5s ease-in-out infinite;
}
linear חשוב לסיבוב - אם נשתמש ב-ease, הספינר יאט ויאיץ ולא יראה טבעי
- אנימציית pulse עם ease-in-out על הטקסט - מתחלף בין שקוף לגלוי
תרגיל 3 - כרטיס הפיכה - Flip Card
<div class="flip-container">
<div class="flip-card">
<div class="flip-front">
<h2>Front Side</h2>
<p>Hover to flip</p>
</div>
<div class="flip-back">
<h2>Back Side</h2>
<p>This is the hidden content that appears when the card is flipped.</p>
</div>
</div>
</div>
.flip-container {
width: 300px;
height: 200px;
perspective: 1000px;
}
.flip-card {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
transition: transform 0.6s ease;
}
.flip-container:hover .flip-card {
transform: rotateY(180deg);
}
.flip-front,
.flip-back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
backface-visibility: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 12px;
padding: 20px;
}
.flip-front {
background: #3498db;
color: white;
}
.flip-back {
background: #2ecc71;
color: white;
transform: rotateY(180deg);
}
perspective על המיכל נותן אפקט 3D - ערך גדול יותר = פרספקטיבה רחוקה יותר
transform-style: preserve-3d מאפשר לילדים לחיות במרחב 3D
backface-visibility: hidden מסתיר את הצד האחורי כשהוא מופנה לצופה
- הצד האחורי מסובב ב-180 מעלות מראש - כך שכשהכרטיס מסתובב, הוא מתגלה
תרגיל 4 - אנימציית Fade-In
<div class="cards-container">
<div class="fade-card">Card 1</div>
<div class="fade-card">Card 2</div>
<div class="fade-card">Card 3</div>
<div class="fade-card">Card 4</div>
</div>
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.cards-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
padding: 40px;
max-width: 1000px;
margin: 0 auto;
}
.fade-card {
background: #3498db;
color: white;
padding: 40px 20px;
border-radius: 12px;
text-align: center;
font-size: 1.2rem;
font-weight: bold;
opacity: 0;
animation: fadeInUp 0.6s ease forwards;
}
.fade-card:nth-child(1) { animation-delay: 0.1s; }
.fade-card:nth-child(2) { animation-delay: 0.2s; }
.fade-card:nth-child(3) { animation-delay: 0.3s; }
.fade-card:nth-child(4) { animation-delay: 0.4s; }
opacity: 0 על הכרטיסים - מתחילים מוסתרים
animation-fill-mode: forwards (בתוך ה-shorthand) - ישארו גלויים אחרי האנימציה
:nth-child() עם animation-delay שונה יוצר אפקט stagger
- כל כרטיס מופיע 0.1 שניות אחרי הקודם - אפקט גל
<div class="slide-menu-wrapper">
<input type="checkbox" id="menu-toggle" class="menu-toggle">
<label for="menu-toggle" class="menu-btn">☰ Menu</label>
<div class="overlay"></div>
<nav class="slide-menu">
<label for="menu-toggle" class="close-btn">×</label>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Portfolio</a>
<a href="#">Contact</a>
</nav>
</div>
.menu-toggle {
display: none;
}
.menu-btn {
position: fixed;
top: 20px;
left: 20px;
background: #1a1a2e;
color: white;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
z-index: 100;
font-size: 1rem;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transition: opacity 0.4s ease, visibility 0.4s ease;
z-index: 998;
}
.slide-menu {
position: fixed;
top: 0;
right: 0;
width: 280px;
height: 100vh;
background: #1a1a2e;
transform: translateX(100%);
transition: transform 0.4s ease-in-out;
z-index: 999;
display: flex;
flex-direction: column;
padding: 60px 20px 20px;
}
.slide-menu a {
color: white;
text-decoration: none;
padding: 15px 10px;
border-bottom: 1px solid #2c2c4e;
font-size: 1.1rem;
}
.slide-menu a:hover {
background: #16213e;
}
.close-btn {
position: absolute;
top: 15px;
right: 20px;
color: white;
font-size: 2rem;
cursor: pointer;
}
/* open state */
.menu-toggle:checked ~ .slide-menu {
transform: translateX(0);
}
.menu-toggle:checked ~ .overlay {
opacity: 1;
visibility: visible;
}
transform: translateX(100%) מזיז את התפריט מחוץ למסך לצד ימין
- כשפותחים:
translateX(0) - חוזר למקום
- שימוש ב-translateX ולא ב-right - ביצועים הרבה יותר טובים
- ה-overlay עם
opacity ו-visibility - מאפשר transition על ההיעלמות
תרגיל 6 - אפקט Skeleton Loading
<div class="skeleton-card">
<div class="skeleton skeleton-image"></div>
<div class="skeleton skeleton-title"></div>
<div class="skeleton skeleton-text"></div>
<div class="skeleton skeleton-text short"></div>
</div>
@keyframes shimmer {
0% {
background-position: -200% 0;
}
100% {
background-position: 200% 0;
}
}
.skeleton-card {
width: 300px;
padding: 20px;
background: white;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.skeleton {
background: linear-gradient(
90deg,
#e0e0e0 25%,
#f0f0f0 50%,
#e0e0e0 75%
);
background-size: 200% 100%;
border-radius: 4px;
animation: shimmer 1.5s ease-in-out infinite;
}
.skeleton-image {
width: 100%;
height: 180px;
border-radius: 8px;
margin-bottom: 15px;
}
.skeleton-title {
width: 70%;
height: 20px;
margin-bottom: 12px;
}
.skeleton-text {
width: 100%;
height: 14px;
margin-bottom: 8px;
}
.skeleton-text.short {
width: 60%;
}
background-size: 200% עושה את ה-gradient רחב פי 2 מהאלמנט
- האנימציה מזיזה את ה-background-position - יוצרת אפקט של "גל אור" שעובר מצד לצד
- gradient עם 3 עצירות (כהה-בהיר-כהה) יוצר את אפקט הנצנוץ
תרגיל 7 - כדור קופץ - Bouncing Ball
<div class="bounce-scene">
<div class="ball"></div>
<div class="ball-shadow"></div>
</div>
@keyframes bounce {
0% {
transform: translateY(0) scale(1, 1);
}
50% {
transform: translateY(250px) scale(1, 1);
}
55% {
transform: translateY(250px) scale(1.2, 0.8);
}
60% {
transform: translateY(240px) scale(1, 1);
}
100% {
transform: translateY(0) scale(1, 1);
}
}
@keyframes shadowPulse {
0% {
transform: scaleX(0.6);
opacity: 0.2;
}
50% {
transform: scaleX(1.2);
opacity: 0.6;
}
100% {
transform: scaleX(0.6);
opacity: 0.2;
}
}
.bounce-scene {
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
height: 100vh;
padding-top: 50px;
}
.ball {
width: 60px;
height: 60px;
background: radial-gradient(circle at 30% 30%, #e74c3c, #c0392b);
border-radius: 50%;
animation: bounce 1s ease-in alternate infinite;
}
.ball-shadow {
width: 60px;
height: 15px;
background: radial-gradient(ellipse, rgba(0, 0, 0, 0.4), transparent);
border-radius: 50%;
margin-top: 250px;
animation: shadowPulse 1s ease-in alternate infinite;
}
ease-in לנפילה - הכדור מאיץ כלפי מטה, כמו הכבידה האמיתית
alternate - חוזר קדימה ואחורה - הנפילה היא ease-in, והעליה הופכת אוטומטית ל-ease-out
- ב-55% יש squash effect - הכדור נדחס כשפוגע ברצפה (רחב יותר וצר יותר)
- ב-60% חוזר לצורה הרגילה
- הצל גדל ומתחזק כשהכדור קרוב לרצפה, ומתכווץ ונחלש כשרחוק
radial-gradient על הכדור נותן מראה תלת-מימדי