---
title: "图片切换图片自动切换"
author: "piperzero"
date: "2013-06-14T22:46:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2013-07-16T08:31:53Z"
id: 1906885
url: "https://piperzero.iteye.com/blog/1906885"
---

&lt;html&gt;  
&lt;head&gt;  
&lt;meta charset="utf-8"/&gt;  
&lt;style type="text/css"&gt;  
body{margin:50px auto;padding:0;border:0; background-color:#ccffcc;}  
#main{width:450px;height:400px;margin:auto}  
#head{height:400px;background-color:#ccc}  
#head img{width:450px;height:400px;padding-left:5px;padding-right:5px}  
#cont li{float:left;list-style:none;width:35px;height:30px;text-align:center;line-height:30px;margin-right:20px;border:1px solid orange}  
#cont ul{width:360px;margin:auto;margin-top:5px background-color:#ccc}  
&lt;/style&gt;  
&lt;script&gt;  
//先定义一个计数器，统计图片  
var count = 1;  
//周期函数的返回值  
var interval = '';  
function changePic(){  
var myimg = document.getElementById("show");  
if(count == 6){  
count = 0;  
}  
var k = ++count;  
myimg.src = "images/0"+k+".jpg";  
//图片修改后，序号背景色也要发生变化  
//先将所有的序号颜色恢复  
for(var i=1;i&lt;=6;i++){  
document.getElementById("num"+i).style.backgroundColor = '';  
}  
//将当前序号设置背景色  
document.getElementById("num"+k).style.backgroundColor = "yellow";  
}

//间歇性函数实现图片周期变换  
function start\_chan(){

interval = setInterval("changePic()",1000);  
}  
//停止周期函数  
function stop\_chan(){  
clearInterval(interval);  
}  
function choosePic(n){  
stop\_chan();  
document.getElementById("show").src = "images/0"+n+".jpg";  
for(var i=1;i&lt;=8;i++){  
document.getElementById("num"+i).style.backgroundColor = '';  
}  
document.getElementById("num"+n).style.backgroundColor = "yellow";  
count = n;  
}  
&lt;/script&gt;  
&lt;/head&gt;

&lt;body onLoad="start\_chan()"&gt;  
&lt;div id="main"&gt;  
&lt;div id="head"&gt;  
&lt;img id="show" src="images/01.jpg" onMouseOver="stop\_chan()" onMouseOut="start\_chan()"/&gt;  
&lt;/div&gt;  
&lt;div id="cont"&gt;  
&lt;ul&gt;  
&lt;li id="num1" onMouseOver="choosePic(1)" onMouseOut="start\_chan()"&gt;1&lt;/li&gt;  
&lt;li id="num2" onMouseOver="choosePic(2)" onMouseOut="start\_chan()"&gt;2&lt;/li&gt;  
&lt;li id="num3" onMouseOver="choosePic(3)" onMouseOut="start\_chan()"&gt;3&lt;/li&gt;  
&lt;li id="num4" onMouseOver="choosePic(4)" onMouseOut="start\_chan()"&gt;4&lt;/li&gt;  
&lt;li id="num5" onMouseOver="choosePic(5)" onMouseOut="start\_chan()"&gt;5&lt;/li&gt;  
&lt;li id="num6" onMouseOver="choosePic(6)" onMouseOut="start\_chan()"&gt;6&lt;/li&gt;  
&lt;/ul&gt;  
&lt;/div&gt;  
&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
![](http://img.blog.csdn.net/20130614224448109)

![](http://img.blog.csdn.net/20130614224526812)