js实现图片轮番

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Title</title>
  6     <style>
  7         body,div,ul,li{
  8             margin:0;padding: 0;
  9         }
 10         ul{
 11             list-style-type:none;
 12         }
 13         body{
 14             background:#000;
 15             text-align:center;
 16             font:12px/20px Arial;
 17         }
 18         #box{
 19             position:relative;
 20             width:492px;
 21             height:172px;
 22             background:#fff;
 23             border-radius:5px;
 24             border:8px solid #fff;
 25             margin:10px auto;
 26             cursor:pointer;
 27         }
 28         #box .list{
 29             position:relative;
 30             width:490px;
 31             height:170px;
 32             overflow:hidden;
 33         }
 34         #box .list ul{
 35             position:absolute;
 36             top:0;left:0;
 37         }
 38         #box .list li{
 39             width:490px;
 40             height:170px;
 41             overflow:hidden;
 42         }
 43         #box .count{
 44             position:absolute;
 45             right:0;
 46             bottom:5px;
 47         }
 48         #box .count li{
 49             color:#fff;
 50             float:left;
 51             width:20px;
 52             height:20px;
 53             cursor:pointer;
 54             margin-right:5px;
 55             overflow:hidden;
 56             background:#F90;
 57             opacity:0.7;
 58             filter:alpha(opacity=70);
 59             border-radius:20px;
 60         }
 61         #box .count li .current{
 62             color:#fff;
 63             opacity:1;
 64             filter:alpha(opacity=100);font-weight:700;background:#f60;
 65         }
 66         #tmp{
 67             width:100px;
 68             height:100px;
 69             background:red;
 70             position:absolute;
 71         }
 72
 73     </style>
 74     <script>
 75         //获取ID
 76         var $ = function (id) {return typeof id === "string" ? document.getElementById(id) : id};
 77
 78         //获取tagName
 79         var $$ = function (tagName, oParent) {return (oParent || document).getElementsByTagName(tagName)};
 80
 81         //自动播放对象
 82         var AutoPlay = function (id) {this.initialize(id)};
 83
 84         AutoPlay.prototype = {
 85             initialize: function (id)
 86             {
 87                 var oThis = this;
 88                 this.oBox = $(id);
 89                 this.oUl = $$("ul", this.oBox)[0];
 90                 this.aImg = $$("img", this.oBox);
 91                 this.timer = null;
 92                 this.autoTimer = null;
 93                 this.iNow = 0;
 94                 this.creatBtn();
 95                 this.aBtn = $$("li", this.oCount);
 96                 this.toggle();
 97                 this.autoTimer = setInterval(function ()
 98                 {
 99                     oThis.next();
100                 }, 3000);
101                 this.oBox.onmouseover = function ()
102                 {
103                     clearInterval(oThis.autoTimer)
104                 };
105                 this.oBox.onmouseout = function ()
106                 {
107                     oThis.autoTimer = setInterval(function ()
108                     {
109                         oThis.next()
110                     }, 3000)
111                 };
112                 for (var i = 0; i < this.aBtn.length; i++)
113                 {
114                     this.aBtn[i].index = i;
115                     this.aBtn[i].onmouseover = function ()
116                     {
117                         oThis.iNow = this.index;
118                         oThis.toggle()
119                     }
120                 }
121             },
122             creatBtn: function ()
123             {
124                 this.oCount = document.createElement("ul");
125                 this.oFrag = document.createDocumentFragment();
126                 this.oCount.className = "count";
127                 for (var i = 0; i < this.aImg.length; i++)
128                 {
129                     var oLi = document.createElement("li");
130                     oLi.innerHTML = i + 1;
131                     this.oFrag.appendChild(oLi)
132                 }
133                 this.oCount.appendChild(this.oFrag);
134                 this.oBox.appendChild(this.oCount)
135             },
136             toggle: function ()
137             {
138                 for (var i = 0; i < this.aBtn.length; i++)
139                     this.aBtn[i].className = "";
140                 this.aBtn[this.iNow].className = "current";
141                 this.doMove(-(this.iNow * this.aImg[0].offsetHeight))
142             },
143             next: function ()
144             {
145                 this.iNow++;
146                 this.iNow == this.aBtn.length && (this.iNow = 0);
147                 this.toggle()
148             },
149             doMove: function (iTarget)
150             {
151                 var oThis = this;
152                 clearInterval(oThis.timer);
153                 oThis.timer = setInterval(function ()
154                 {
155                     var iSpeed = (iTarget - oThis.oUl.offsetTop) / 5;
156                     iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
157                     oThis.oUl.offsetTop == iTarget ? clearInterval(oThis.timer) : (oThis.oUl.style.top = oThis.oUl.offsetTop + iSpeed + "px")
158                 }, 30)
159             }
160         };
161         window.onload = function ()
162         {
163             new AutoPlay("box");
164         };
165     </script>
166 </head>
167 <body>
168 <div id="box">
169     <div class="list">
170         <ul>
171             <li><img src="image/01.jpg" width="490" height="170" /></li>
172             <li><img src="image/02.jpg" width="490" height="170" /></li>
173             <li><img src="image/03.jpg" width="490" height="170" /></li>
174             <li><img src="image/04.jpg" width="490" height="170" /></li>
175             <li><img src="image/05.jpg" width="490" height="170" /></li>
176         </ul>
177     </div>
178 </div>
179 </body>
180 </html>
时间: 2024-08-04 03:37:15

js实现图片轮番的相关文章

JS获取图片的缩略图,并且动态的加载多张图片

找了好多资料也没有找到该死的ie的解决办法,最后放弃了ie <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js获取缩略图</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <st

原生JS编写图片切换效果和点击按钮的样式变化

这两天更进一步的了解了JS,老师让我们用原生的js编写图片切换和改变点击按钮样式,就是让我们学会怎么去把一个问题拆分,怎么将一个大问题拆分成许多的小问题,再用函数封装起来.比如一个点击按钮,让其点击时背景色发生改变,点击另一个时,上一个按钮要变回原来本有的颜色:这个问题用jquery,一行代码就搞定,但是用原生js就得分三个部分来考虑: 1.添加改变背景的样式. 2.怎么获取到除了当前点击的按钮以外其他的兄弟节点. 3.怎么去除按钮的样式属性. 也讲了事件委托,实现了可以删除新添加的元素的功能.

【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)

转载自:http://www.himigame.com/iphone-cocos2dx/1354.html 首先说明一个问题: 为什么要在线更新资源和脚本文件!? 对于此问题,那要说的太多了,简单概括,如果你的项目已经在google play 或Apple Store 等平台上架了,那么当你项目需要做一些活动或者修改前端的一些代码等那么你需要重新提交一个新版本给平台,这时候你的上架时候是个不确定的时候,具体什么时候能上架,主要跟平台有关,你再着急,也没有用的. 那么如果你的项目是使用脚本语言进行

分别用css3、JS实现图片简单的无缝轮播功效

本文主要介绍分别使用CSS3.JS实现图片简单无缝轮播功效: 一.使用CSS3实现:利用animation属性 (实现一张一张的轮播,肉眼只看见一张图片) HTML部分比较简单,两个div下包着几个img标签:为了实现无缝轮播,注意第一张图片要与最后一张图片相同: <div class="out"> <div class="imgs"> <img src="img/beatuy.jpg"/> <img s

asp.net MVC发布iis无法加载css,js和图片

今天真够郁闷的,遇到了在本地能运行的项目到了iis服务器那里就不行了,无法加载css,js和图片,这里说清楚一下先,关于asp.net 的MVC中这样的情况其实不少,但是之前遇到的是在visual studio运行的时候就已经不能加载css和js文件,那种情况一般都是路径的问题,改下页面代码就行,网上教程不少,而这个其实是一个CMS的开源系统.Orchard,国庆实在无聊,就想玩下这个asp.net MVC框架的CMS,而且是微软推荐的开源CMS,提到了就来说说这个吧,和国内的其他CMS对比起来

兼容ie[6-9]、火狐、Chrome、opera、maxthon3、360浏览器的js本地图片预览

html代码: <div id="divPreview"> <img id="imgHeadPhoto" src="Images/Headphoto/noperson.jpg" style="width: 160px; height: 170px;border: solid 1px #d2e2e2;" alt="" /> </div> <asp:FileUpload

js判断图片加载完成

<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <img id="mainPic" src="http://img1c.xgo-img

JS实现图片上传预览效果:方法一

<script type="text/javascript"> //处理file input加载的图片文件 $(document).ready(function(e) { //判断浏览器是否有FileReader接口 if(typeof FileReader =='undefined') { /*$("#images_show").css({'background':'none'}).html('亲,您的浏览器还不支持HTML5的FileReader接口

nginx实现动态分离,解决css和js等图片加载问题

改帖专门为使用nginx,通过nginx把请求转发到web服务器再返回客户端的时候,解决css和js和图片加载不出来的问题. 如果没安装nginx,请访问一下地址进行安装 <a href="http://www.cnblogs.com/sz-jack/p/5200283.html">http://www.cnblogs.com/sz-jack/p/5200283.html</a>