本文目录一览:
求一个图片轮换JS效果代码
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
style type="text/css"
* { margin:0; padding:0; word-break:break-all; }
ul, li { list-style:none; }
#focus_change_btn .current { background:url() no-repeat 37px 8px;}
#focus_change_btn .current img { border-color:#EEE; }
#focus_change_btn li { display:inline; float:right; margin:0 10px; padding-top:12px; }
#focus_change_btn li img { width:20px; height:20px; border:2px solid #888; }
#abb { width:200px; height:200px;}
#abb li { display:inline; margin:40px 20px; }
#abb li img { width:200px; height:50px; border:0;}
/style
script type="text/javascript"
function $(id) { return document.getElementById(id); }
/*位移算法,参数分别是:ID名,水平位移,垂直位移,和延迟时间(就是漂移时间);每次移动的距离加上一张图片的width或者height就行。*/
function moveElement(elementID,final_x,final_y,interval) {
if (!document.getElementById) return false;
if (!document.getElementById(elementID)) return false;
var elem = document.getElementById(elementID);
if (elem.movement) {
clearTimeout(elem.movement);
}
if (!elem.style.left) {
elem.style.left = "0px";
}
if (!elem.style.top) {
elem.style.top = "0px";
}
var xpos = parseInt(elem.style.left);
var ypos = parseInt(elem.style.top);
if (xpos == final_x ypos == final_y) {
return true;
}
if (xpos final_x) {
var dist = Math.ceil((final_x - xpos)/10);//ceil(x)对x进行上舍入
xpos = xpos + dist;
}
if (xpos final_x) {
var dist = Math.ceil((xpos - final_x)/10);
xpos = xpos - dist;
}
if (ypos final_y) {
var dist = Math.ceil((final_y - ypos)/10);
ypos = ypos + dist;
}
if (ypos final_y) {
var dist = Math.ceil((ypos - final_y)/10);
ypos = ypos - dist;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";
elem.movement = setTimeout(repeat,interval);
}
/*清除CSS,方便后面添加*/
function classNormal(){
var focusBtnList = $('focus_change_btn').getElementsByTagName('li');
for(var i=0; ifocusBtnList.length; i++) {
focusBtnList[i].className='';
}
}
/*修改下面的图片顺序*/
function changeAbb(i,j,k){
var abblist = $('abb').getElementsByTagName('img');
abblist[i].src='images/t1.gif';
abblist[j].src='images/t2.gif';
abblist[k].src='images/t3.gif';
}
/*给触发按钮添加事件*/
function focusChange() {
var focusBtnList = $('focus_change_btn').getElementsByTagName('li');
focusBtnList[0].onmouseover = function() {
moveElement('focus_change_list',0,0,5);
classNormal();
focusBtnList[0].className='current';
changeAbb(0,1,2);
}
focusBtnList[1].onmouseover = function() {
moveElement('focus_change_list',-250,0,5);
classNormal();
focusBtnList[1].className='current';
changeAbb(1,0,2);
}
focusBtnList[2].onmouseover = function() {
moveElement('focus_change_list',-500,0,5);
classNormal()
focusBtnList[2].className='current';
changeAbb(1,2,0);
}
}
window.onload=function(){
focusChange();
}
/script
/head
body
div style="width:410px;height:245px;position:relative;margin:0;padding:0; border:1px solid blue;"
div id="focus_change" style="position:relative; width:250px; height:245px; overflow:hidden; margin:0px 0px 0px 80px;"
div id="focus_change_list" style="top:0; left:0;position:absolute; width:760px; height:245px; "
ul
li style="float:left;" img style="width:250px; height:245px; border:none; " src=""/ /li
li style="float:left;"img style="width:250px; height:245px; border:none; " src=""/ /li
li style="float:left;"img style="width:250px; height:245px; border:none; " src=""/ /li
/ul
/div
/div
div style="position:absolute; width:410px; height:30px; top:200px; left:0; background:#000; filter:alpha(opacity=50); -moz-opacity:0.5; opacity: 0.5;"/div
div id="focus_change_btn" style="position:absolute; width:410px; height:60px; top:190px; left:0;"
ul style="padding-left:5px;"
li class="current"a href="#"img src="" alt="" //a/li
lia href="#"img src="" alt="" //a/li
lia href="#"img src="" alt="" //a/li
/ul
/div
/div
div
ul id="abb"
lia href="#"img src="" alt="" //a/li
lia href="#"img src="" alt="" //a/li
lia href="#"img src="" alt="" //a/li
/ul
/div
/body
/html
/*楼主的图片我看不清楚,我用百度中心的图替代一下。CSS改成自己需要的,我中间加了个遮蔽层,楼主觉得不好看可以删掉*/
这段JS图片轮显代码应该怎么改?
试试这个图片切换
有12345数字一起切换
鼠标点一下数字会变换到另一张图片,不点就会自动换
用js实现图片轮显
一、思路:页面加载、获取整个容器、所有放数字索引的li及放图片列表的ul、定义放定时器的变量、存放当前索引的变。
二、提示:
1、 index不能一直无限制的递增下去,需做判断
2、调用切换图片函数时需将递增之后的index作为参数传过去
body
div class="wrap" id='wrap'
ul id="pic"
liimg src="1.jpg" alt=""/li
liimg src="2.jpg" alt=""/li
liimg src="3.jpg" alt=""/li
liimg src="4.jpg" alt=""/li
liimg src="5.jpg" alt=""/li
/ul
ol id="list"
li class="on"1/li
li2/li
li3/li
li4/li
li5/li
/ol
input value="上一张" onclick="" type="button"/
input value="下一张" onclick="" type="button"/
/div
script type="text/javascript"
var wrap=document.getElementById('wrap'),
pic=document.getElementById('pic').getElementsByTagName("li"),
list=document.getElementById('list').getElementsByTagName('li'),
index=0;
function autoPlay (type) {//type==0--上一张;type==1--下一张
if(type==0)index--;
else if(type==1)index++;
if (index = pic.length) index = 0;//如果Index大于照片数量就从第一张开始
changePic(index);
}
// 定义图片切换函数
function changePic (curIndex) {
for (var i = 0; i pic.length; ++i) {
pic[i].style.display = "none";
list[i].className = "";
}
pic[curIndex].style.display = "block";
list[curIndex].className = "on";
}
/script
/body
我只是大致写的一个例子,你自己调试。