如何使用 CSS 和 JavaScript 繞圓旋轉多個物件?
使用CSS 繞圓旋轉多個物件
在嘗試創建多個物件繞圓旋轉的動畫時,遇到了只有一個物體旋轉正確。本文旨在提供解決此問題的指導,以確保所有所需的物件圍繞圓圈無縫旋轉。
現有程式碼和實作
提供的程式碼涉及「outCircle」充當旋轉中心的div,在其中,巢狀的「旋轉」div 包含要設定動畫的對象。使用 CSS3 動畫,「旋轉」div 配置為圍繞「outCircle」無限旋轉。但是,嘗試在「旋轉」div 中新增其他物件會導致不良行為。
解決方案
要解決此問題,基於JavaScript 的方法更合適用於處理多個物件旋轉:
var radius = 100; // Adjust to place items closer or farther from the center var fields = $('.item'); // Target the objects to be rotated var container = $('#container'); // Parent div encasing the rotating objects var width = container.width(); var height = container.height(); var angle = 0; var step = (2 * Math.PI) / fields.length; // Calculate the angular separation between objects fields.each(function() { // Calculate the coordinates for each object based on its position in the circle var x = Math.round(width / 2 + radius * Math.cos(angle) - $(this).width() / 2); var y = Math.round(height / 2 + radius * Math.sin(angle) - $(this).height() / 2); if (console) { console.log($(this).text(), x, y); // Display coordinates for debugging } $(this).css({ left: x + 'px', // Set the left position top: y + 'px', // Set the top position }); // Increment the angle based on the spacing between objects angle += step; });
登入後複製
附加註意事項
此外,CSS3可用於設定動畫行為:
body { padding: 2em; } #container { width: 200px; height: 200px; margin: 10px auto; border: 1px solid #000; position: relative; border-radius: 50%; animation: spin 10s linear infinite; // Define the animation duration and direction } .item { width: 30px; height: 30px; line-height: 30px; text-align: center; border-radius: 50%; position: absolute; background: #f00; animation: spin 10s linear infinite reverse; // Reverse the animation for each object } @keyframes spin { 100% { transform: rotate(1turn); // Rotate the objects one full turn } }
登入後複製
範例
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>
登入後複製
此方法提供一個靈活且可擴展的解決方案,用於使用CSS3 和JavaScript 繞圓旋轉任意數量的物件。
以上是如何使用 CSS 和 JavaScript 繞圓旋轉多個物件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
如何修復KB5055612無法在Windows 10中安裝?
4 週前
By DDD
<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
<🎜>:種植花園 - 完整的突變指南
3 週前
By DDD
北端:融合系統,解釋
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在本週的平台新聞綜述中,Chrome引入了一個用於加載的新屬性,Web開發人員的可訪問性規範以及BBC Move

有很多分析平台可幫助您跟踪網站上的訪問者和使用數據。也許最著名的是Google Analytics(廣泛使用)
