js 图片轮播(5张图片)
程序员文章站
2022-06-19 19:43:27
演示地址:http://img.jb51.net/online/picplayer/picplay.htm复制代码 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>pic player</title>
<script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.2.6.js"></script>
<script type="text/javascript" src="http://img.jb51.net/jslib/jquery/tween.js"></script>
</head>
<style type="text/css">
img{border:0;}
</style>
<body>
<div id="picplayer" style="position:relative;overflow:hidden;width:300px;height:300px;clear:none;border:solid 1px #ccc;">
there is a pic-player
</div>
<script>
var p = $('#picplayer');
var pics1 = [{url:'http://img.jb51.net/online/picplayer/1.jpg',link:'//www.jb51.net/#',time:5000},{url:'http://img.jb51.net/online/picplayer/2.jpg',link:'//www.jb51.net/#',time:4000},{url:'http://img.jb51.net/online/picplayer/3.jpg',link:'//www.jb51.net',time:6000},{url:'http://img.jb51.net/online/picplayer/2.jpg',link:'//www.jb51.net',time:6000},{url:'http://img.jb51.net/online/picplayer/1.jpg',link:'//www.jb51.net',time:6000}];
initpicplayer(pics1,p.css('width').split('px')[0],p.css('height').split('px')[0]);
//
//
function initpicplayer(pics,w,h)
{
//选中的图片
var selecteditem;
//选中的按钮
var selectedbtn;
//自动播放的id
var playid;
//选中图片的索引
var selectedindex;
//容器
var p = $('#picplayer');
p.text('');
p.append('<div id="piccontent"></div>');
var c = $('#piccontent');
for(var i=0;i<pics.length;i++)
{
//添加图片到容器中
c.append('<a href="'+pics[i].link+'" target="_blank"><img id="picitem'+i+'" style="display: none;z-index:'+i+'" src="'+pics[i].url+'" /></a>');
}
//按钮容器,绝对定位在右下角
p.append('<div id="picbtnholder" style="position:absolute;top:'+(h-25)+'px;width:'+w+'px;height:20px;z-index:10000;"></div>');
//
var btnholder = $('#picbtnholder');
btnholder.append('<div id="picbtns" style="float:right; padding-right:1px;"></div>');
var btns = $('#picbtns');
//
for(var i=0;i<pics.length;i++)
{
//增加图片对应的按钮
btns.append('<span id="picbtn'+i+'" style="cursor:pointer; border:solid 1px #ccc;background-color:#eee; display:inline-block;"> '+(i+1)+' </span> ');
$('#picbtn'+i).data('index',i);
$('#picbtn'+i).click(
function(event)
{
if(selecteditem.attr('src') == $('#picitem'+$(this).data('index')).attr('src'))
{
return;
}
setselecteditem($(this).data('index'));
}
);
}
btns.append(' ');
///
setselecteditem(0);
//显示指定的图片index
function setselecteditem(index)
{
selectedindex = index;
clearinterval(playid);
//alert(index);
if(selecteditem)selecteditem.fadeout('fast');
selecteditem = $('#picitem'+index);
selecteditem.fadein('slow');
//
if(selectedbtn)
{
selectedbtn.css('backgroundcolor','#eee');
selectedbtn.css('color','#000');
}
selectedbtn = $('#picbtn'+index);
selectedbtn.css('backgroundcolor','#000');
selectedbtn.css('color','#fff');
//自动播放
playid = setinterval(function()
{
var index = selectedindex+1;
if(index > pics.length-1)index=0;
setselecteditem(index);
},pics[index].time);
}
}
</script>
</body>
</html>
如果想增加图片可以通过增加 var pics1 后面的内容。即可。
演示地址:http://img.jb51.net/online/picplayer/picplay.htm
复制代码 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>pic player</title>
<script type="text/javascript" src="http://img.jb51.net/jslib/jquery/jquery-1.2.6.js"></script>
<script type="text/javascript" src="http://img.jb51.net/jslib/jquery/tween.js"></script>
</head>
<style type="text/css">
img{border:0;}
</style>
<body>
<div id="picplayer" style="position:relative;overflow:hidden;width:300px;height:300px;clear:none;border:solid 1px #ccc;">
there is a pic-player
</div>
<script>
var p = $('#picplayer');
var pics1 = [{url:'http://img.jb51.net/online/picplayer/1.jpg',link:'//www.jb51.net/#',time:5000},{url:'http://img.jb51.net/online/picplayer/2.jpg',link:'//www.jb51.net/#',time:4000},{url:'http://img.jb51.net/online/picplayer/3.jpg',link:'//www.jb51.net',time:6000},{url:'http://img.jb51.net/online/picplayer/2.jpg',link:'//www.jb51.net',time:6000},{url:'http://img.jb51.net/online/picplayer/1.jpg',link:'//www.jb51.net',time:6000}];
initpicplayer(pics1,p.css('width').split('px')[0],p.css('height').split('px')[0]);
//
//
function initpicplayer(pics,w,h)
{
//选中的图片
var selecteditem;
//选中的按钮
var selectedbtn;
//自动播放的id
var playid;
//选中图片的索引
var selectedindex;
//容器
var p = $('#picplayer');
p.text('');
p.append('<div id="piccontent"></div>');
var c = $('#piccontent');
for(var i=0;i<pics.length;i++)
{
//添加图片到容器中
c.append('<a href="'+pics[i].link+'" target="_blank"><img id="picitem'+i+'" style="display: none;z-index:'+i+'" src="'+pics[i].url+'" /></a>');
}
//按钮容器,绝对定位在右下角
p.append('<div id="picbtnholder" style="position:absolute;top:'+(h-25)+'px;width:'+w+'px;height:20px;z-index:10000;"></div>');
//
var btnholder = $('#picbtnholder');
btnholder.append('<div id="picbtns" style="float:right; padding-right:1px;"></div>');
var btns = $('#picbtns');
//
for(var i=0;i<pics.length;i++)
{
//增加图片对应的按钮
btns.append('<span id="picbtn'+i+'" style="cursor:pointer; border:solid 1px #ccc;background-color:#eee; display:inline-block;"> '+(i+1)+' </span> ');
$('#picbtn'+i).data('index',i);
$('#picbtn'+i).click(
function(event)
{
if(selecteditem.attr('src') == $('#picitem'+$(this).data('index')).attr('src'))
{
return;
}
setselecteditem($(this).data('index'));
}
);
}
btns.append(' ');
///
setselecteditem(0);
//显示指定的图片index
function setselecteditem(index)
{
selectedindex = index;
clearinterval(playid);
//alert(index);
if(selecteditem)selecteditem.fadeout('fast');
selecteditem = $('#picitem'+index);
selecteditem.fadein('slow');
//
if(selectedbtn)
{
selectedbtn.css('backgroundcolor','#eee');
selectedbtn.css('color','#000');
}
selectedbtn = $('#picbtn'+index);
selectedbtn.css('backgroundcolor','#000');
selectedbtn.css('color','#fff');
//自动播放
playid = setinterval(function()
{
var index = selectedindex+1;
if(index > pics.length-1)index=0;
setselecteditem(index);
},pics[index].time);
}
}
</script>
</body>
</html>
如果想增加图片可以通过增加 var pics1 后面的内容。即可。
上一篇: 樱桃怎么吃美味又营养
下一篇: 腊肉炒花菜的做法