我需要一个帮助!我不是 CSS 高手。所以我为我的案子做了一些事情。但这于事无补。在这里我试着解释我的问题。
有人可以帮忙吗?
import "./styles.css";
import React, { useRef, useState, useEffect } from "react";
import styles from "styled-components";
const DotsWrapper = styles.div`
display: flex;
justify-content: center;
top: 0;
left: 0;
right: 0;
bottom: 0;
`;
const Dot = styles.div`
width: 50px;
height: 50px;
text-align: center;
padding: 2rem;
border-radius: 50%;
background-color: ${({ isActive }) => (isActive ? "#fc916a" : "#436073")};
transition: transform 0.3s, background-color 0.3s;
transform: ${({ isActive, index, activeIndex, centerIndex }) => {
const distanceFromCenter = index - centerIndex;
const distanceFromActive = index - activeIndex;
if (isActive) {
return "scale(1.3)";
} else if (distanceFromActive === 1) {
return "translateX(25px)";
} else if (distanceFromActive === -1) {
return "translateX(-25px)";
} else if (distanceFromCenter > 0) {
return "translateX(50px)";
} else if (distanceFromCenter < 0) {
return "translateX(-50px)";
} else {
return "scale(1)";
}
}};
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 20px;
margin: 0 10px;
`;
const CenterDot = styles(Dot)`
transform: scale(1.3);
`;
export default function App() {
const mobileContent = {
page_blocks: [
{ info_date: "01-01-2002" },
{ info_date: "01-01-2003" },
{ info_date: "01-01-2004" },
{ info_date: "01-01-2005" },
{ info_date: "01-01-2006" },
{ info_date: "01-01-2007" }
]
};
const [activeIndex, setActiveIndex] = React.useState(0);
const activeDot = React.useRef(null);
const centerIndex = Math.floor(mobileContent.page_blocks.length / 2);
const windowWidth = window.innerWidth;
const centerPosition = windowWidth / 2;
const [activePosition, setActivePosition] = React.useState(0);
React.useEffect(() => {
const activeDotPosition = activeDot.current.getBoundingClientRect();
const activePosition =
activeDotPosition.x + activeDotPosition.width / 2 - centerPosition;
setActivePosition(activePosition);
}, [windowWidth]);
const result = centerPosition - activePosition;
const translateX = result > 0 ? `${result}px` : `-${Math.abs(result)}px`;
return (
<React.Fragment>
<div
className="line"
style={{
position: "relative",
width: "100%",
overflowX: "hidden",
height: "350px"
}}
>
<br />
<br />
<br />
windowWidth = {windowWidth}
<br />
centerPosition = {centerPosition}
<br />
activePosition = {activePosition}
<br />
result= {result}
<br />
{translateX}
<br />
<DotsWrapper style={{ transform: `translateX(${translateX})` }}>
{mobileContent.page_blocks.map((item, index) =>
index === activeIndex ? (
<CenterDot
key={index}
isActive
index={index}
centerIndex={centerIndex}
activeIndex={activeIndex}
ref={activeDot}
>
{new Date(item.info_date).getFullYear()}
</CenterDot>
) : (
<Dot
key={index}
isActive={index === activeIndex}
index={index}
centerIndex={centerIndex}
activeIndex={activeIndex}
onClick={() => setActiveIndex(index)}
>
{new Date(item.info_date).getFullYear()}
</Dot>
)
)}
</DotsWrapper>
</div>
</React.Fragment>
);
}
活动圆圈需要始终位于窗口的水平中心。 其余的圆圈需要根据活动圆圈自动对齐。
如何做到这一点?我希望你能理解。谁能帮帮我?
我试着在这里解释一下。
在上面的代码中,活动圆点将位于中心。 所以你需要做的是 当前
ex 图。
|--------------------|--------------------| 1000px(总宽度)
----活动点------------------------ 250px
所以,
我们需要测量。您需要多少空间或像素来移动活动点以使其居中?
(总宽度)-(中心位置)-(活动点位置) 例 1: 结果 = 1000px - 500px - 500px = 250px
例 2: 结果 = 1000px - 500px - 800px = -350px
所以现在 (活动点位置)+(结果) 例 1: 250 像素 + 250 像素 = 500 像素 例 2: 800px + ( - 300px ) = 500px
所以使用这个计算使活动点位置中心(使用 css translateX 或任何其他)
和其他不活跃的点将自动对齐