Skip to content

Commit

Permalink
Merge pull request #263 from hepengwei/feat_he
Browse files Browse the repository at this point in the history
新增鼠标Hover交互效果4
  • Loading branch information
hepengwei authored Dec 13, 2024
2 parents fcc6389 + 39ed9b8 commit 16f00a7
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/825.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/8868.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/main.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/styles/pages/825.9f81f9c9.css

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/styles/pages/8868.e7d9d787.css

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.container {
$borderRadius: 24px;
width: 300px;
height: 424px;
width: 280px;
height: 396px;
display: flex;
justify-content: center;
align-items: center;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
align-items: center;

.content {
width: 70%;
max-width: 1300px;
width: 66%;
max-width: 1200px;
min-width: 900px;
display: flex;
justify-content: space-between;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useEffect, useCallback } from "react";
import React from "react";
import Card from "./Card";
import car1 from "images/html/car1.png";
import car2 from "images/html/car2.png";
Expand Down
115 changes: 115 additions & 0 deletions src/pages/html/InteractiveDesign/components/MouseHover4/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React, { useRef, useEffect, useCallback } from "react";
import styles from "./card.module.scss";

interface CardProps {
url: string;
}

const SPEED = 36;

const Card = (props: CardProps) => {
const { url } = props;
const containerRef = useRef<HTMLDivElement>(null);
const frameId = useRef<number>(0);
const xRef = useRef<number>(0);
const yRef = useRef<number>(0);

const rotateLoop = () => {
if (containerRef.current) {
const { clientWidth, clientHeight } = containerRef.current;
let nextX = xRef.current;
let nextY = yRef.current;
if (yRef.current <= 0) {
if (xRef.current >= clientWidth) {
nextX = clientWidth;
nextY = SPEED;
} else if (nextX + SPEED > clientWidth) {
nextY = nextX + SPEED - clientWidth;
nextX = clientWidth;
} else {
nextX += SPEED;
nextY = 0;
}
} else if (xRef.current >= clientWidth) {
if (yRef.current >= clientHeight) {
nextX = clientWidth - SPEED;
nextY = clientHeight;
} else if (nextY + SPEED > clientHeight) {
nextX = clientWidth - (nextY + SPEED - clientHeight);
nextY = clientHeight;
} else {
nextX = clientWidth;
nextY += SPEED;
}
} else if (yRef.current >= clientHeight) {
if (xRef.current <= 0) {
nextX = 0;
nextY = clientHeight - SPEED;
} else if (nextX - SPEED < 0) {
nextY = clientHeight - (SPEED - nextX);
nextX = 0;
} else {
nextX -= SPEED;
nextY = clientHeight;
}
} else {
if (yRef.current <= 0) {
nextX = SPEED;
nextY = 0;
} else if (nextY - SPEED < 0) {
nextX = SPEED - nextY;
nextY = 0;
} else {
nextX = 0;
nextY = Math.max(nextY - SPEED, 0);
}
}
xRef.current = nextX;
yRef.current = nextY;
containerRef.current.setAttribute(
"style",
`--url: url("${url}"); --x: ${nextX}px; --y: ${nextY}px`
);
frameId.current = window.requestAnimationFrame(rotateLoop);
}
};

const onMouseEnter = useCallback(() => {
if (containerRef.current) {
frameId.current = window.requestAnimationFrame(rotateLoop);
}
}, []);

const onMouseLeave = useCallback(() => {
if (containerRef.current) {
frameId.current && window.cancelAnimationFrame(frameId.current);
containerRef.current.setAttribute(
"style",
`--url: url("${url}"); --x: 0px; --y: 0px`
);
}
}, []);

useEffect(() => {
if (containerRef.current && url) {
containerRef.current.setAttribute("style", `--url: url("${url}")`);
}

return () => {
frameId.current && window.cancelAnimationFrame(frameId.current);
};
}, []);

return (
<div
className={styles.container}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
ref={containerRef}
>
<div className={styles.content} />
</div>
);
};

export default Card;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
.container {
$borderRadius: 24px;
width: 280px;
height: 396px;
display: flex;
justify-content: center;
align-items: center;
border-radius: $borderRadius;
cursor: pointer;
overflow: hidden;
position: relative;

&::before {
content: "";
opacity: 0;
position: absolute;
inset: 0;
border-radius: $borderRadius;
background: conic-gradient(#03a9f4, #e91e63, #9c27b0, #ff5722, #03a9f4);
mask: radial-gradient(
circle at var(--x) var(--y),
#000,
#000,
transparent,
transparent,
transparent
);
}

&:hover {
transform: scale(1.1);
&::before {
opacity: 1;
}
}

.content {
border-radius: $borderRadius;
overflow: hidden;
position: absolute;
inset: 3px;

&::before,
&::after {
content: "";
position: absolute;
background: var(--url);
background-size: cover;
background-position: center;
border-radius: $borderRadius;
}

&::before {
inset: 0;
filter: blur(20px);
}

&::after {
inset: 40px;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.container {
width: 100%;
height: 560px;
display: flex;
justify-content: center;
align-items: center;

.content {
width: 66%;
max-width: 1200px;
min-width: 900px;
display: flex;
justify-content: space-between;
align-items: center;
}
}
22 changes: 22 additions & 0 deletions src/pages/html/InteractiveDesign/components/MouseHover4/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import Card from "./Card";
import car1 from "images/html/car1.png";
import car2 from "images/html/car2.png";
import car3 from "images/html/car3.png";
import styles from "./index.module.scss";

const cardList = [car1, car2, car3];

const MouseHover4 = () => {
return (
<div className={styles.container}>
<div className={styles.content}>
{cardList.map((url: string, index: number) => (
<Card url={url} key={index} />
))}
</div>
</div>
);
};

export default MouseHover4;
2 changes: 2 additions & 0 deletions src/pages/html/InteractiveDesign/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import DragShopping from "./components/DragShopping";
import MouseHover1 from "./components/MouseHover1";
import MouseHover2 from "./components/MouseHover2";
import MouseHover3 from "./components/MouseHover3";
import MouseHover4 from "./components/MouseHover4";
import SlideButtonTab from "./components/SlideButtonTab";
import FlipBook from "./components/FlipBook";
import Switchs from "./components/Switchs";
Expand All @@ -34,6 +35,7 @@ const boxList = [
{ element: <MouseHover1 /> },
{ element: <MouseHover2 /> },
{ element: <MouseHover3 /> },
{ element: <MouseHover4 /> },
{ element: <SlideButtonTab /> },
{ element: <FlipBook /> },
{ element: <Switchs /> },
Expand Down

0 comments on commit 16f00a7

Please sign in to comment.