To position the cards, you must apply a CSS transform to them. Initially, each card requires a rotation transformation around the transform-origin
located at the lower right corner. Therefore, you must determine the rotational angle for each card (by adding an angle of 7deg
or another degree). Next, you need to shift each card so that they form a path, resembling an ellipse as depicted in your image. Define an ellipse with horizontal and vertical radii (a
and b
), increase the rotation angle (around the center of the ellipse), and compute the x
and y
coordinates of the point on the elliptical path using the following formula (based on polar coordinates equations):
x = a * cos(alpha);
y = b * sin(alpha);
The dx
and dy
values (the difference between the current and previous x
and y
respectively) must be calculated and accumulated for use in the translate
transformation.
Below is a demonstration of the code:
JS:
var n = 13; // number of cards
var al = 7; // angular difference between two cards
var a = 90; // horizontal radius of the elliptical path for card arrangement
var b = 200; // vertical radius of the elliptical path for card arrangement
var step = 4; // degree interval to move between cards along the elliptical path
// Define initial angles and variables
var initAlpha = (180 - al * (n - 1)) / 2;
var beta = (180 - step * (n - 1)) / 2 - 15;
var betaRad = beta * Math.PI / 180;
var prefixes = ["webkit", "moz", "ms", ""];
var x = a * Math.cos(betaRad), y = b * Math.sin(betaRad);
var dx = 0, dy = 0;
// Function to perform transformations
function transform(elem, dx, dy, da) {
for(var i = 0; i < prefixes.length; i++){
elem.style[prefixes[i] + (prefixes[i] ? "Transform" : "transform")] = "translate(" + dx + "px," +
dy + "px) rotate(" + da + "deg)";
}
}
for(var i = 0; i < n; i++) {
var card = document.createElement("div");
card.className = "card";
document.body.appendChild(card);
transform(card, dx.toFixed(10), dy.toFixed(10), initAlpha);
beta += step;
initAlpha += al;
betaRad = beta * Math.PI / 180;
var x2 = a * Math.cos(betaRad);
var y2 = b * Math.sin(betaRad);
dx += x - x2;
dy += y - y2;
x = x2;
y = y2;
}
CSS:
.card {
width:150px;
height:100px;
border:1px solid gray;
border-radius:5px;
position:absolute;
top:200px;
left:30px;
-webkit-transform-origin:right bottom;
-ms-transform-origin:right bottom;
-moz-transform-origin:right bottom;
transform-origin:right bottom;
background:lightyellow;
transition:background 0.3s;
}
.card:hover {
background:orange;
}
body {
background:black;
}
Please note that while the arranged cards may not be exactly like the image shown, they closely resemble it. You can adjust parameters such as the number of cards (n
), angular differences al
, horizontal radius a
, vertical radius b
of the elliptical path, step
, initAlpha
, beta
to modify the card layout.
For a more perceptible demo allowing you to input text for the card without unexpected directional issues, check out this alternate demonstration.