挑战用React封装100个组件【002】

项目地址
https://github.com/hismeyy/react-component-100

组件描述

组件适用于需要展示图文信息的场景,比如产品介绍、用户卡片或任何带有标题、描述和可选图片的内容展示

样式展示

代码展示

InfoCard.tsx
js 复制代码
import './InfoCard.css'

interface InfoCardProps {
    title: string;
    description: string;
    imgSrc: string;
    imgAlt?: string;
}

const InfoCard = ({ 
    title, 
    description, 
    imgSrc, 
    imgAlt = title
}: InfoCardProps) => {
    return (
        <div className='info-card'>
            <div className='img'>
                {imgSrc && <img src={imgSrc} alt={imgAlt} />}
            </div>
            <div className="info">
                <h6>{title}</h6>
                <p>{description}</p>
            </div>
        </div>
    )
}

export default InfoCard
InfoCard.css
js 复制代码
.info-card {
    box-sizing: border-box;
    width: 350px;
    height: 450px;
    border-radius: 15px;
    background-color: #F5F5F5;
    box-shadow: 3px 3px 6px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    align-items: left;
    padding: 15px;
    cursor: pointer;
    transform-origin: center;
    position: relative;
    overflow: hidden;
}

.info-card .img {
    width: 320px;
    height: 320px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
    box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.info-card .img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.info-card .info {
    margin: 20px 5px 0;
    height: 60px;
    position: relative;
    z-index: 1;
    transition: transform 0.3s ease;
}

.info-card .info h6 {
    all: unset;
    display: block;
    width: 100%;
    font-size: 16px;
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: color 0.3s ease;
}

.info-card .info p {
    all: unset;
    display: -webkit-box;
    width: 100%;
    margin-top: 10px;
    font-size: 12px;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    line-height: 1.3;
    line-clamp: 2;
    box-orient: vertical;
}

.info-card:hover .img {
    transform: scale(1.02);
}

.info-card:hover .img img {
    transform: scale(1.1);
}

.info-card:hover .info h6 {
    color: #f08a5d;
}

使用

App.tsx
js 复制代码
import './App.css'
import InfoCard from './components/card/infoCard02/InfoCard'

function App() {
  return (
    <>
      <InfoCard
        title="Web全栈开发指南"
        description="全面的Web开发学习指南,涵盖前端技术、后端开发、数据库设计等多个领域的知识。本课程将帮助你构建完整的开发技能体系。"
        imgSrc="https://images.unsplash.com/photo-1517694712202-14dd9538aa97?w=600&q=80"
      />
    </>
  )
}

export default App
相关推荐
谢尔登19 分钟前
为什么React 17开始无需在组件中引入React了?
前端·react.js·前端框架
老前端的功夫44 分钟前
Webpack 深度解析:从配置哲学到编译原理
前端·webpack·前端框架·node.js
JackieDYH2 小时前
CSS滚动吸附详解:构建精准流畅的滚动体验-scroll-snap-type
前端·css
软件技术NINI3 小时前
html css js网页制作成品——敖瑞鹏html+css+js 4页附源码
javascript·css·html
开发者小天3 小时前
React中使用classnames的案例
前端·react.js·前端框架
困惑阿三3 小时前
深入理解 JavaScript 中的(Promise.race)
开发语言·前端·javascript·ecmascript·reactjs
我命由我123453 小时前
微信小程序 bind:tap 与 bindtap 的区别
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
馬致远4 小时前
案例1- 跳动的心
javascript·css·css3
魂祈梦5 小时前
页面出现莫名其妙的滚动条
前端·css
重铸码农荣光5 小时前
从零实现一个「就地编辑」组件:深入理解 OOP 封装与复用的艺术
前端·javascript·前端框架