Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> CSS

CSS के साथ किसी एलिमेंट को सर्कुलर पथ में कैसे मूव करें


परिचय

CSS एनिमेशन transform फंक्शन्स को संयोजित, रोटेट, और ट्रांसलेट करके एलिमेंट्स को विभिन्न तरीकों से ट्रांसफॉर्म करने में हमारी मदद कर सकते हैं।

उदाहरण: CSS ट्रांसफॉर्म के साथ सर्कुलर मोशन

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
      margin: 8%;
      width: 35px;
      height: 35px;
      border-radius: 5px;
      background: red;
      animation: move 3s infinite linear;
    }
    @keyframes move {
      0% {
        transform: rotate(0deg) translateX(40px) rotate(0deg);
      }
      100% {
        transform: rotate(360deg) translateX(40px) rotate(-360deg);
      }
    }
  </style>
</head>
<body>
  <div></div>
</body>
</html>

तकनीक समझाया गया

यह "डबल रोटेशन" तकनीक का उपयोग करता है:

  1. rotate(0deg → 360deg) — एलिमेंट को केंद्र के चारों ओर घुमाता है
  2. translateX(40px) — एलिमेंट को केंद्र से 40px दूर ले जाता है (त्रिज्या)
  3. rotate(0deg → -360deg) — एलिमेंट को काउंटर-रोटेट करता है ताकि यह सीधा रहे

परिणाम: एलिमेंट एक सर्कल में घूमता है जबकि अपनी दिशा बनाए रखता है।

मॉडर्न विकल्प: CSS offset-path (मोशन पाथ)

<!DOCTYPE html>
<html>
<head>
  <style>
    .orbit {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background: linear-gradient(135deg, #667eea, #764ba2);
      offset-path: path('M 200,200 m -100,0 a 100,100 0 1,0 200,0 a 100,100 0 1,0 -200,0');
      animation: orbit 4s linear infinite;
    }
    @keyframes orbit {
      0% { offset-distance: 0%; }
      100% { offset-distance: 100%; }
    }
    .container {
      width: 400px;
      height: 400px;
      margin: 2rem auto;
      border: 2px dashed #cbd5e1;
      border-radius: 50%;
      position: relative;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="orbit"></div>
  </div>
</body>
</html>

उन्नत: एकाधिक ऑर्बिटिंग एलिमेंट्स

<!DOCTYPE html>
<html>
<head>
  <style>
    .solar-system {
      width: 500px;
      height: 500px;
      margin: 2rem auto;
      position: relative;
      border: 1px solid #334155;
      border-radius: 50%;
      background: radial-gradient(circle at center, #1e293b, #0f172a);
    }
    .sun {
      position: absolute;
      top: 50%; left: 50%;
      transform: translate(-50%, -50%);
      width: 60px; height: 60px;
      border-radius: 50%;
      background: radial-gradient(circle at 30% 30%, #fde047, #f59e0b);
      box-shadow: 0 0 40px #fde047, 0 0 80px #f59e0b;
      z-index: 10;
    }
    .planet {
      position: absolute;
      top: 50%; left: 50%;
      border-radius: 50%;
      transform-origin: center center;
      animation: orbit linear infinite;
    }
    .mercury { width: 10px; height: 10px; background: #94a3b8; animation-duration: 3s; margin: -5px; }
    .venus { width: 16px; height: 16px; background: #fbbf24; animation-duration: 5s; margin: -8px; }
    .earth { width: 18px; height: 18px; background: #3b82f6; animation-duration: 8s; margin: -9px; }
    .mars { width: 12px; height: 12px; background: #ef4444; animation-duration: 12s; margin: -6px; }
    @keyframes orbit {
      from { transform: translate(-50%, -50%) rotate(0deg) translateX(120px) rotate(0deg); }
      to { transform: translate(-50%, -50%) rotate(360deg) translateX(120px) rotate(-360deg); }
    }
    /* अलग-अलग कक्षाओं के लिए ट्रांसलेट X एडजस्ट करें */
    .venus { animation-duration: 5s; }
    .earth { animation-duration: 8s; }
    .mars { animation-duration: 15s; }
  </style>
</head>
<body>
  <div class="solar-system">
    <div class="sun"></div>
    <div class="planet mercury"></div>
    <div class="planet venus"></div>
    <div class="planet earth"></div>
    <div class="planet mars"></div>
  </div>
</body>
</html>

ब्राउज़र सपोर्ट

  • ट्रांसफॉर्म दृष्टिकोण: सभी मॉडर्न ब्राउज़र (IE10+)
  • offset-path: Chrome 69+, Firefox 72+, Edge 79+, Safari 16.4+
  1. CSS से स्क्रॉलबार कैसे छिपाएं? CSS से स्क्रॉलबार कैसे छिपाएं?

    CSS से स्क्रॉलबार छिपाने के लिए, कोड निम्नानुसार है -उदाहरण<!DOCTYPE html> <html> <head> <style>    body {       height: 150vh;       width: 200vw;       overflow: hidden;       font-family: "Segoe UI&

  1. CSS से तीर कैसे बनाएं? CSS से तीर कैसे बनाएं?

    CSS से तीर बनाने के लिए, कोड निम्नानुसार है -उदाहरण<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style>    body {       font-family: "Segoe UI", Tahoma, Geneva

  1. CSS offset-path (मोशन पाथ) के साथ सर्कुलर पथ में एलिमेंट मूव करना CSS offset-path (मोशन पाथ) के साथ सर्कुलर पथ में एलिमेंट मूव करना

    परिचय हम CSS offset-path प्रॉपर्टी का उपयोग करके किसी एलिमेंट के लिए मोशन पथ परिभाषित कर सकते हैं। यह जटिल पथ एनिमेशन के लिए एक मॉडर्न, घोषनात्मक तरीका प्रदान करता है। सिंटैक्स selector { offset-path: /* value */; } उदाहरण: SVG पाथ के साथ सर्कुलर मोशन <!DOCTYPE html> <html> <head