jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。全屏背景切换是指在一个网页上实现背景图片的全屏显示,并且可以动态切换不同的背景图片。
background-size: cover
或 background-size: 100% 100%
来实现全屏显示。以下是一个使用 jQuery 实现全屏背景切换的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 全屏背景切换</title>
<style>
body, html {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.bg {
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
width: 200%;
position: absolute;
z-index: -1;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="bg" id="bg"></div>
<button id="changeBg">切换背景</button>
<script>
$(document).ready(function() {
var backgrounds = [
'url(background1.jpg)',
'url(background2.jpg)',
'url(background3.jpg)'
];
var currentIndex = 0;
function changeBackground() {
$('#bg').css('background-image', backgrounds[currentIndex]);
currentIndex = (currentIndex + 1) % backgrounds.length;
}
$('#changeBg').click(changeBackground);
// 自动切换背景
setInterval(changeBackground, 3000);
});
</script>
</body>
</html>
通过以上方法,可以实现一个简单且高效的全屏背景切换效果。
领取专属 10元无门槛券
手把手带您无忧上云