-
Notifications
You must be signed in to change notification settings - Fork 2k
万宝锐旗直招-ethan-frontend #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
万宝锐旗直招-ethan-frontend #1841
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,44 @@ | ||
.App { | ||
text-align: center; | ||
position: relative; | ||
width: 100vw; | ||
height: 100vh; | ||
} | ||
|
||
.title { | ||
font-size: 56px; | ||
font-weight: 600; | ||
.container { | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
position: relative; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding-top: 170px; | ||
padding-bottom: 100px; | ||
box-sizing: border-box; | ||
background-position: center bottom 100px; | ||
background-size: auto 60%; | ||
background-repeat: no-repeat; | ||
} | ||
|
||
.text { | ||
font-size: 28px; | ||
font-weight: 400; | ||
.container .text-content { | ||
position: relative; | ||
z-index: 1; | ||
} | ||
|
||
.container .text-content .title { | ||
font-size: 60px; | ||
line-height: 60px; | ||
color: inherit; | ||
margin: 0; | ||
text-align: center; | ||
white-space: pre; | ||
} | ||
|
||
.container .text-content .content { | ||
font-size: 34px; | ||
text-align: center; | ||
line-height: 50px; | ||
white-space: pre; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,54 @@ | ||
import './App.css'; | ||
import Carousel from './components/Carousel/index' | ||
import imgIPhone from './assets/iphone.png' | ||
import imgTablet from './assets/tablet.png' | ||
import imgAirPods from './assets/airpods.png' | ||
|
||
import './App.css' | ||
|
||
export interface CarouselDataItem{ | ||
id: string | ||
color: string | ||
backgroundColor: string | ||
title: string[] | ||
contents?: string[] | ||
bg: string | ||
} | ||
|
||
|
||
const data: CarouselDataItem[] = [ | ||
{ | ||
id: '1', | ||
color: '#fff', | ||
backgroundColor: '#111111', | ||
title: ['xPhone'], | ||
contents: ['Lots to love. Less to spend.', 'Starting at $399.'], | ||
bg: imgIPhone | ||
}, | ||
{ | ||
id: '2', | ||
color: '#000', | ||
backgroundColor: '#FAFAFA', | ||
title: ['Tablet'], | ||
contents: ['Just the right amount of everything.'], | ||
bg: imgTablet | ||
}, | ||
{ | ||
id: '3', | ||
color: '#000', | ||
backgroundColor: '#F1F1F1', | ||
title: ['Buy a Tablet or xPhone for college.', 'Get airPods.'], | ||
bg: imgAirPods | ||
} | ||
] | ||
|
||
function App() { | ||
return <div className='App'>{/* write your component here */}</div>; | ||
return ( | ||
<div className="App"> | ||
<Carousel | ||
items={data} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default App; | ||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
@keyframes progress { | ||
from { | ||
transform: scaleX(0); | ||
} | ||
to { | ||
transform: scaleX(1); | ||
} | ||
} | ||
|
||
.carousel { | ||
width: 100%; | ||
height: 100%; | ||
overflow-x: hidden; | ||
} | ||
|
||
.carousel .carousel-wrapper { | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
flex-wrap: nowrap; | ||
padding: 0; | ||
margin: 0; | ||
transition: transform 0.3s; | ||
} | ||
|
||
.carousel .carousel-wrapper .carousel-item { | ||
width: 100%; | ||
height: 100%; | ||
flex-shrink: 0; | ||
position: relative; | ||
list-style: none; | ||
} | ||
|
||
.carousel .indicators { | ||
position: absolute; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
bottom: 30px; | ||
display: flex; | ||
align-items: center; | ||
padding: 0; | ||
} | ||
|
||
.carousel .indicators .indicator { | ||
list-style: none; | ||
box-sizing: border-box; | ||
width: 40px; | ||
height: 20px; | ||
border-radius: 1px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
cursor: pointer; | ||
} | ||
|
||
.carousel .indicators .indicator + .indicator { | ||
margin-left: 8px; | ||
} | ||
|
||
.carousel .indicators .indicator .indicator__track { | ||
display: block; | ||
width: 100%; | ||
height: 2px; | ||
background: rgb(140, 140, 140); | ||
} | ||
|
||
.carousel .indicators .indicator .indicator__bar__auto { | ||
display: block; | ||
border-radius: 1px; | ||
width: 100%; | ||
height: 2px; | ||
background-color: #fff; | ||
transform: scaleX(0); | ||
transform-origin: left; | ||
} | ||
|
||
.carousel .indicators .indicator.active .indicator__bar__auto { | ||
animation: progress linear; | ||
animation-duration: var(--indicator-animation-duration); | ||
} | ||
|
||
.carousel .indicators .indicator.active .indicator__bar { | ||
display: block; | ||
width: 100%; | ||
height: 2px; | ||
background-color: #fff; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* * | ||
* @author Ethan | ||
* @email [email protected] | ||
* @datetime 2023/04/19 | ||
* */ | ||
|
||
import { type FC, type CSSProperties, useEffect, useState } from 'react' | ||
import './index.css' | ||
|
||
import { CarouselDataItem } from '../../App' | ||
|
||
export interface CarouselProps { | ||
items: CarouselDataItem[] | ||
delay?: number | ||
autoPlay?: boolean | ||
} | ||
|
||
const Carousel: FC<CarouselProps> = ({ items, delay = 3000, autoPlay = true}) => { | ||
const [active, setActive] = useState<number>(0) | ||
const [auto,setAuto] = useState<boolean>(autoPlay) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
|
||
useEffect(() => { | ||
if (active >= items.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种边界情况是在防止什么 case, items 的变动吗,我理解如果是这样不应该是这样来处理 |
||
setActive(0) | ||
return | ||
} | ||
const timer = setTimeout(() => setActive((active + 1) % items.length), delay) | ||
if(!auto){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在这里用 auto 判断属于为了交互强行写逻辑了,auto 不纯粹且和交互严重耦合 |
||
clearTimeout(timer) | ||
} | ||
return () => clearTimeout(timer) | ||
}, [items, delay, active, auto]) | ||
|
||
return ( | ||
<div | ||
className="carousel" | ||
style={{ '--indicator-animation-duration': `${delay}ms` } as CSSProperties} | ||
> | ||
<ul className="carousel-wrapper" style={{ transform: `translateX(-${active * 100}%)` }}> | ||
{items.map((item, idx) => ( | ||
<li | ||
key={item.id} | ||
className={`${idx === active ? 'carousel-item active' : 'carousel-item'}`} | ||
> | ||
<div | ||
className="container" | ||
style={{ | ||
backgroundColor: item.backgroundColor, | ||
backgroundImage: `url(${item.bg})` | ||
}} | ||
> | ||
<div className="text-content" style={{ color: item.color }}> | ||
<h2 className="title">{item.title?.join('\r\n')}</h2> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. item.title 不存在还要展示 这个 h2 吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是的 应该{item.title && {item.title?.join('\r\n')}} |
||
{item.contents && <h4 className="content">{item.contents.join('\r\n')}</h4>} | ||
</div> | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
<ul className="indicators"> | ||
{items.map((item, idx) => ( | ||
<li | ||
key={item.id} | ||
className={`${idx === active ? 'indicator active' : 'indicator'}`} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种模板字符串的话 indicator 后面会多一个空格 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indicator${idx === active ? ' active' : '‘} |
||
onClick={() => { | ||
setAuto(false) | ||
setActive(idx) | ||
} | ||
} | ||
onMouseLeave={()=>{ | ||
setAuto(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我理解这个地方可能设计的有些问题,autoplay 本质上是决定轮播图是不是自动播放的,如果这样设计的话,即使我设置了 false,点击之后又变成自动播放了 |
||
}} | ||
> | ||
<span className="indicator__track"> | ||
<span className={auto ? "indicator__bar__auto": "indicator__bar"}/> | ||
</span> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export default Carousel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用传。从初始值会自动推导