Profile 卡片是一种用户界面元素,用于在应用程序或网站中显示用户的个人信息。它通常包含用户的头像、姓名、职业等基本信息,以及其他相关信息,如社交媒体链接或联系方式。Profile 卡片广泛应用于社交网络、个人博客和企业网站等场景。
此 Profile 卡片实现的基本功能包括:
1. HTML 结构
HTML 结构定义了卡片的布局和内容:
<div class="bg-white rounded-lg shadow-lg p-6">
<div class="flex items-center justify-between">
<h2 class="text-gray-900 text-xl font-bold">My Profile</h2>
<div class="flex items-center">
<svg ...>...</svg>
</div>
</div>
<div class="mt-6">
<div class="flex items-center">
<img ...>
<div class="ml-4">
<h3 class="text-gray-900 text-lg font-bold">Snowman Jensen</h3>
<p class="text-gray-600 text-sm">Software Engineer</p>
</div>
</div>
<div class="mt-6 flex items-center justify-between">
<div class="flex items-center">
<svg ...>...</svg>
<span class="text-gray-900 text-lg font-bold ml-2">549</span>
</div>
<div class="flex items-center">
<svg ...>...</svg>
<span class="text-gray-900 text-lg font-bold ml-2">225.5</span>
</div>
</div>
<div class="mt-6">
<h4 class="text-gray-900 text-lg font-bold">Current Exercise</h4>
<div class="mt-4 flex items-center justify-between">
<div class="flex items-center">
<img ...>
<div class="ml-4">
<h3 class="text-gray-900 text-lg font-bold">Weightlifting</h3>
<p class="text-gray-600 text-sm">17:00 - 18:00</p>
</div>
</div>
<div class="flex items-center">
<svg ...>...</svg>
<span class="text-gray-900 text-lg font-bold ml-2">549</span>
</div>
</div>
</div>
</div>
</div>
2. CSS Styling
CSS 样式定义了卡片的外观和布局:
.bg-white {
background-color: white;
}
.rounded-lg {
border-radius: 1rem;
}
.shadow-lg {
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
.p-6 {
padding: 1.5rem;
}
.flex {
display: flex;
}
.items-center {
align-items: center;
}
.justify-between {
justify-content: space-between;
}
.text-gray-900 {
color: #212529;
}
.text-xl {
font-size: 1.25rem;
}
.font-bold {
font-weight: bold;
}
.ml-4 {
margin-left: 1rem;
}
.text-sm {
font-size: 0.875rem;
}
.text-gray-600 {
color: #6b7280;
}
.mt-6 {
margin-top: 1.5rem;
}
3. JavaScript
JavaScript 代码负责动态加载数据和更新卡片内容:
const profile = {
name: "Snowman Jensen",
occupation: "Software Engineer",
socialLinks: [
{
icon: "fab fa-twitter",
link: "https://twitter.com/snowmanjensen",
},
{
icon: "fab fa-github",
link: "https://github.com/snowmanjensen",
},
],
currentExercise: {
name: "Weightlifting",
time: "17:00 - 18:00",
},
stats: {
workouts: 549,
hours: 225.5,
},
};
const profileContainer = document.getElementById("profile-container");
const createElement = (element, item) => {
const newElement = document.createElement(element);
newElement.textContent = item;
return newElement;
};
const createIcon = (iconClass) => {
const newIcon = document.createElement("i");
newIcon.classList.add("h-6", "w-6", iconClass);
return newIcon;
};
const populateProfile = (data) => {
const profileName = createElement("h3", data.name);
const profileOccupation = createElement("p", data.occupation);
const profileImage = document.querySelector("img");
profileImage.src = `https://source.unsplash.com/random/100x100`;
profileContainer.appendChild(profileName);
profileContainer.appendChild(profileOccupation);
data.socialLinks.forEach((link) => {
const socialIcon = createIcon(link.icon);
const socialLink = createElement("a");
socialLink.href = link.link;
socialLink.appendChild(socialIcon);
profileContainer.appendChild(socialLink);
});
const currentExerciseName = createElement("h3", data.currentExercise.name);
const currentExerciseTime = createElement("p", data.currentExercise.time);
const currentExerciseContainer = document.querySelector(".flex.items-center");
currentExerciseContainer.appendChild(currentExerciseName);
currentExerciseContainer.appendChild(currentExerciseTime);
const workoutCount = createElement("span", data.stats.workouts);
const hoursCount = createElement("span", data.stats.hours);
const workoutIcon = createIcon("fa-solid fa-dumbbell");
const hoursIcon = createIcon("fa-solid fa-clock");
const workoutContainer = document.querySelector(".flex.items-center:first-child");
const hoursContainer = document.querySelector(".flex.items-center:last-child");
workoutContainer.appendChild(workoutIcon);
workoutContainer.appendChild(workoutCount);
hoursContainer.appendChild(hoursIcon);
hoursContainer.appendChild(hoursCount);
};
populateProfile(profile);
此 Profile 卡片功能实现清晰简洁,使用 HTML、CSS 和 JavaScript 实现了基本功能。
开发过程中的经验与收获:
未来该卡片功能的拓展与优化: