<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery图片切换和Tab切换示例</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
}
/* 图片切换样式 */
.image-switcher {
margin-bottom: 30px;
}
.image-switcher-container {
position: relative;
height: 400px;
overflow: hidden;
border: 1px solid #ddd;
margin-bottom: 10px;
}
.image-switcher img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
opacity: 0;
transition: opacity 0.5s ease;
}
.image-switcher-controls {
display: flex;
justify-content: center;
gap: 10px;
}
.image-switcher-btn {
padding: 8px 15px;
background: #3498db;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
}
.image-switcher-btn:hover {
background: #2980b9;
}
/* Tab切换样式 */
.tab-container {
margin-top: 30px;
}
.tab-header {
display: flex;
border-bottom: 1px solid #ddd;
}
.tab-btn {
padding: 10px 20px;
background: #f1f1f1;
border: none;
cursor: pointer;
transition: all 0.3s;
}
.tab-btn.active {
background: #3498db;
color: white;
}
.tab-content {
display: none;
padding: 20px;
border: 1px solid #ddd;
border-top: none;
}
.tab-content.active {
display: block;
}
.footer {
margin-top: 30px;
text-align: center;
color: #666;
}
</style>
</head>
<body>
<div class="container">
<h1>jQuery切换效果演示</h1>
<!-- 图片切换部分 -->
<div class="image-switcher">
<h2>图片切换展示</h2>
<div class="image-switcher-container">
<img src="https://picsum.photos/id/237/800/400" alt="图片2">
<img src="https://picsum.photos/id/238/800/400" alt="图片3">
</div>
<div class="image-switcher-controls">
<button class="image-switcher-btn" data-index="0">图片1</button>
<button class="image-switcher-btn" data-index="1">图片2</button>
<button class="image-switcher-btn" data-index="2">图片3</button>
</div>
</div>
<!-- Tab切换部分 -->
<div class="tab-container">
<h2>Tab切换展示</h2>
<div class="tab-header">
<button class="tab-btn active" data-tab="tab1">选项卡1</button>
<button class="tab-btn" data-tab="tab2">选项卡2</button>
<button class="tab-btn" data-tab="tab3">选项卡3</button>
</div>
<div class="tab-content active" id="tab1">
<h3>选项卡1内容</h3>
<p>这是第一个选项卡的内容。你可以在这里放置任何HTML内容。</p>
</div>
<div class="tab-content" id="tab2">
<h3>选项卡2内容</h3>
<p>这是第二个选项卡的内容。Tab切换是非常常见的网页交互效果。</p>
</div>
<div class="tab-content" id="tab3">
<h3>选项卡3内容</h3>
<p>这是第三个选项卡的内容。jQuery让这些交互效果变得非常简单。</p>
</div>
</div>
<div class="footer">
<p>© 2023 切换效果演示页面</p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 图片切换功能
$('.image-switcher-btn').on('click', function() {
var index = $(this).data('index');
$('.image-switcher img').removeClass('active').eq(index).addClass('active');
$('.image-switcher-btn').removeClass('active');
$(this).addClass('active');
});
// Tab切换功能
$('.tab-btn').on('click', function() {
var tabId = $(this).data('tab');
$('.tab-btn').removeClass('active');
$(this).addClass('active');
$('.tab-content').removeClass('active');
$('#' + tabId).addClass('active');
});
});
</script>
</body>
</html>
192

被折叠的 条评论
为什么被折叠?



