<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery图片淡入淡出特效 - 99j.quest示例</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
}
.image-container {
margin: 20px 0;
height: 300px;
position: relative;
}
.image-to-show {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
max-width: 100%;
max-height: 300px;
display: none;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px 5px;
cursor: pointer;
border-radius: 4px;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
.footer {
margin-top: 30px;
color: #666;
font-size: 14px;
}
.footer a {
color: #4CAF50;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>jQuery图片淡入淡出特效</h1>
<p>点击按钮查看图片淡入淡出效果</p>
<div class="image-container">
<img src="https://picsum.photos/600/300?random=1" alt="示例图片1" class="image-to-show" id="image1">
<img src="https://picsum.photos/600/300?random=2" alt="示例图片2" class="image-to-show" id="image2">
<img src="https://picsum.photos/600/300?random=3" alt="示例图片3" class="image-to-show" id="image3">
</div>
<div class="button-group">
<button id="showImage1">显示图片1</button>
<button id="showImage2">显示图片2</button>
<button id="showImage3">显示图片3</button>
</div>
<div class="footer">
<p>特效示例代码来自 <a href="http://www.99j.quest" target="_blank">www.99j.quest</a></p>
</div>
</div>
<script>
$(document).ready(function() {
// 默认显示第一张图片
$('#image1').fadeIn(500);
// 点击按钮切换图片
$('#showImage1').click(function() {
$('.image-to-show').fadeOut(500, function() {
$('#image1').fadeIn(500);
});
});
$('#showImage2').click(function() {
$('.image-to-show').fadeOut(500, function() {
$('#image2').fadeIn(500);
});
});
$('#showImage3').click(function() {
$('.image-to-show').fadeOut(500, function() {
$('#image3').fadeIn(500);
});
});
// 控制台输出信息
console.log('这个特效示例来自 http://www.99j.quest');
});
</script>
</body>
</html>