<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
display: flex;
min-height: 100vh;
overflow: hidden;
}
.sidebar {
flex: 0 0 200px;
background-color: #333;
color: white;
padding: 20px;
}
.content {
flex: 1;
padding: 20px;
}
header {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
}
.card {
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
margin-bottom: 20px;
cursor: pointer; /* 添加光标样式,表明是可点击的 */
transition: transform 0.2s ease-in-out; /* 添加过渡效果 */
}
.card:hover {
transform: scale(1.05); /* 悬停时放大卡片 */
}
</style>
<title>Flex 事件处理</title>
</head>
<body>
<div class="container">
<div class="sidebar">
<h2>Sidebar</h2>
<p>Some content in the sidebar.</p>
</div>
<div class="content">
<header>
<h1>Flex 事件处理</h1>
</header>
<div class="card" onclick="handleCardClick()">
<h2>Click me!</h2>
<p>Click this card to trigger an event.</p>
</div>
</div>
</div>
<script>
function handleCardClick() {
alert("Card clicked!");
}
</script>
</body>
</html>
在这个例子中,我添加了一个点击事件处理函数 handleCardClick,并将其绑定到卡片元素的 onclick 属性上。点击卡片时,会弹出一个简单的警告框,显示 "Card clicked!"。
此外,为了增加用户交互性,我还添加了一些样式,使卡片在悬停时放大,并添加了过渡效果。这样用户在交互时会有一种视觉上的反馈。
你可以根据实际需求,使用 JavaScript 添加其他事件监听器,例如鼠标悬停、键盘事件等,以实现更复杂的交互效果。
转载请注明出处:http://www.zyzy.cn/article/detail/10783/Flex