-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from hepengwei/feat_he
新增鼠标Hover交互效果4
- Loading branch information
Showing
13 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/pages/html/InteractiveDesign/components/MouseHover3/card.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/pages/html/InteractiveDesign/components/MouseHover3/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/pages/html/InteractiveDesign/components/MouseHover4/Card.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
62 changes: 62 additions & 0 deletions
62
src/pages/html/InteractiveDesign/components/MouseHover4/card.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/pages/html/InteractiveDesign/components/MouseHover4/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/pages/html/InteractiveDesign/components/MouseHover4/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters