jQuery轮换广告是一种使用jQuery库实现网页上广告图片或内容自动切换显示的技术。它通常用于网站首页或其他显眼位置,以提高用户体验和广告效果。
以下是一个简单的jQuery定时轮换广告示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery轮换广告示例</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
#ad-container {
width: 300px;
height: 250px;
overflow: hidden;
}
#ad-container img {
width: 100%;
height: 100%;
display: none;
}
</style>
</head>
<body>
<div id="ad-container">
<img src="ad1.jpg" alt="广告1">
<img src="ad2.jpg" alt="广告2">
<img src="ad3.jpg" alt="广告3">
</div>
<script>
$(document).ready(function() {
var ads = $('#ad-container img');
var index = 0;
function showAd() {
ads.hide();
ads.eq(index).show();
index = (index + 1) % ads.length;
}
ads.eq(0).show();
setInterval(showAd, 3000); // 每3秒切换一次广告
});
</script>
</body>
</html>
setInterval
时,注意检查是否有其他定时器或长时间运行的任务影响定时器的准确性。通过以上示例代码和解决方法,您可以快速实现一个简单的jQuery轮换广告,并解决可能遇到的常见问题。
领取专属 10元无门槛券
手把手带您无忧上云