以下是一个简单的PHP图片轮播的示例代码:
<!DOCTYPE html>
<html>
<head>
<title>PHP图片轮播</title>
<style>
#slideshow {
position: relative;
width: 500px;
height: 300px;
overflow: hidden;
margin: 0 auto;
}
#slideshow img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="slideshow">
<?php
// 图片路径数组
$images = array(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
// 遍历图片数组并输出图片标签
foreach ($images as $image) {
echo '<img src="' . $image . '" alt="Slideshow Image">';
}
?>
</div>
<script>
// 图片轮播
var slideshow = document.getElementById('slideshow');
var images = slideshow.getElementsByTagName('img');
var currentIndex = 0;
setInterval(function() {
// 隐藏当前图片
images[currentIndex].style.display = 'none';
// 计算下一张图片的索引
currentIndex = (currentIndex + 1) % images.length;
// 显示下一张图片
images[currentIndex].style.display = 'block';
}, 2000);
</script>
</body>
</html>
在上述代码中,首先定义了一个包含图片路径的数组 $images
,然后使用 PHP 的 foreach 循环遍历数组,并输出对应的图片标签。接着,使用 JavaScript 实现了图片轮播的逻辑,使用 setInterval 方法实现定时切换图片的效果。每隔 2 秒,当前显示的图片会被隐藏,然后计算出下一张图片的索引,再将其显示出来。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站