求大神帮改成图片每6秒自动切换下一张

咕嘿嘿poi 2020-12-31 01:52:00
目前这段代码是每次刷新随机出现一张图片但是不会自动切换,求大神帮改成在不刷新的情况下每6秒自动切换下张图片。如果切换的时候有个淡入淡出的效果就更好了。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
</head>
<body>
<img id="img1" style="z-index: -1;position: absolute;left: 0;top: 0;" />
<script>
+ function() {
//设置初始化设置
$("#img1").attr("src", "./img/" + getRandomSrc() + ".jpg"); //获取同级目录中的img文件内的jpg图片
var width = window.innerWidth > 0 ? window.innerWidth : document.body.clientWidth; //宽度
var height = window.innerHeight > 0 ? window.innerHeight : document.body.clientHeight; //高度
$("#img1").attr("width", width);
$("#img1").attr("height", height);
}();
function getRandomSrc() {
var rnd = Math.ceil(Math.random() * 7);
return rnd;
}
</script>
</body>
</html>

...全文
1368 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
姎楹 2020-12-31
  • 打赏
  • 举报
回复

<script>
           $(function() {
            getRandomSrc();
            setInterval(getRandomSrc, 6000);               
            });
            function getRandomSrc() {
                var rnd = Math.ceil(Math.random() * 7);
                 //设置初始化设置
                $("#img1").attr("src", "./img/" + rnd + ".jpg");  //获取同级目录中的img文件内的jpg图片
                var width = window.innerWidth > 0 ? window.innerWidth : document.body.clientWidth;   //宽度
                var height = window.innerHeight > 0 ? window.innerHeight : document.body.clientHeight;   //高度
                $("#img1").attr("width", width);
                $("#img1").attr("height", height);
            }
</script>
丰云 2020-12-31
  • 打赏
  • 举报
回复
var width = window.innerWidth > 0 ? window.innerWidth : document.body.clientWidth; //宽度 var height = window.innerHeight > 0 ? window.innerHeight : document.body.clientHeight; //高度 $("#img1").attr("width", width); $("#img1").attr("height", height); setInterval(function(){ $("#img1").attr("src", "./img/" + getRandomSrc() + ".jpg"); //获取同级目录中的img文件内的jpg图片 }, 6000)

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧