怎么用html5+css3 实现图片轮播

怎么用html5+css3 实现图片轮播,第1张

1、首先我们创建一个简单的项目,如图所示包括html,css和img三个。

2、这里是html文件,引入css和html代码文件,如图所示。

3、这里是css文件代码,上面是div和图片显示的效果代码,后面是动画效果。

4、这里是事件,这里定义了四个时间段的状态,兼容了ie的。

5、如图所示这里是效果图,会根据时间轮播显示下一张图片 了。

<html>

<head>

<title>图片滚动 </title>

<style>

#div1 {position:relativewidth:650pxheight:210pxoverflow:hidden}

#div2{position:absolute}

li{float:leftlist-style-type:nonepadding:5px}

img{border:none}

a{position:relative}

</style>

<script>

window.onload=function()

{

var odiv2=document.getElementById('div2')

var ali=odiv2.getElementsByTagName('li')

var aspeed=-5

odiv2.innerHTML+=odiv2.innerHTML

odiv2.style.width=ali[0].offsetWidth*ali.length+'px'

setInterval(function()

{

odiv2.style.left=odiv2.offsetLeft+aspeed+'px'

if (odiv2.offsetLeft<-odiv2.offsetWidth/2)

{

odiv2.style.left='0px'

}

},30)}

}

</script>

</head>

<body>

<div id='div1'>

<div id='div2'>

<li><a href=""><img src="1.jpg" /></a></li>

<li><a href=""><img src="2.jpg" /></a></li>

<li><a href=""><img src="3.jpg" /></a></li>

<li><a href=""><img src="4.jpg" /></a></li>

<!--这边你可以添加任意多的li,也就是你可以添加任意多的图片,使任意多的图片轮播-->

</div>

</div>

</body>

</html>

一、数字键控制代码:

<div style="position:relative top:-50px left:240px">

    <a href="javascript:show(1)"><span id="I1" style="width:18px text-align:left background:gray">1</span></a>

    <a href="javascript:show(2)"><span id="I2" style="width:18pxtext-align:leftbackground-color:gray">2</span></a>

  <a href="javascript:show(3)"><span id="I3" style="width:18pxtext-align:leftbackground-color:gray">3</span></a>

    <a href="javascript:show(4)"><span id="I4" style="width:18pxtext-align:leftbackground-color:gray">4</span></a>

    <a href="javascript:show(5)"><span id="I5" style="width:18pxtext-align:leftbackground-color:gray">5</span></a>

    <a href="javascript:show(6)"><span id="I6" style="width:18pxtext-align:leftbackground-color:gray">6</span></a></div>

    <script language="javaScript"> 

 var nowIndex=1

 var maxIndex=6

 function show(index)

 {

 if(Number(index)){                                     

 clearTimeout(theTimer)

 nowIndex=index

 }

 for(var i=1i<(maxIndex+1)i++){

  if(i==nowIndex)

   {document.getElementById('pic'+nowIndex).style.display=''

   document.getElementById('I'+nowIndex).style.backgroundColor='red'}

  else

   {document.getElementById('pic'+i).style.display='none'

   document.getElementById('I'+i).style.backgroundColor='gray'}

  }{

  if(nowIndex==maxIndex)

   nowIndex=1

  else

   nowIndex++

  }

 theTimer=setTimeout('show()',3000)

 }

 </script>

 </div>

二、图片自动播放:

<div id="butong_net_left" style="overflow:hiddenwidth:1000px">

<table cellpadding="0" cellspacing="0" border="0">

<tr><td id="butong_net_left1" valign="top" align="center">

<table cellpadding="2" cellspacing="0" border="0">

<tr align="center">


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7207905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-02
下一篇 2023-04-02

发表评论

登录后才能评论

评论列表(0条)

保存