需要实现的功能列表:

  • 左右按钮切换
  • 图片无限滚动
  • 小按钮切换
  • 动画函数
  • 自动播放

html和css代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>焦点轮播图</title>
    <style type="text/css">
        *{ margin: 0; padding: 0; text-decoration: none;}
        body { padding: 20px;}
        #container { width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; position: relative;}
        #list { width: 4200px; height: 400px; position: absolute; z-index: 1;}
        #list img { float: left;}
        #buttons { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px;}
        #buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 5px;}
        #buttons .on {  background: orangered;}
        .arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px;  position: absolute; z-index: 2; top: 180px; background-color: RGBA(0,0,0,.3); color: #fff;}
        .arrow:hover { background-color: RGBA(0,0,0,.7);}
        #container:hover .arrow { display: block;}
        #prev { left: 20px;}
        #next { right: 20px;}
    </style>
</head>
<body>

<div id="container"> <div id="list" style="left: -600px;"> <img src="img/5.jpg" alt="1"/> <img src="img/1.jpg" alt="1"/> <img src="img/2.jpg" alt="2"/> <img src="img/3.jpg" alt="3"/> <img src="img/4.jpg" alt="4"/> <img src="img/5.jpg" alt="5"/> <img src="img/1.jpg" alt="5"/> </div> <div id="buttons"> <span index="1" class="on"></span> <span index="2"></span> <span index="3"></span> <span index="4"></span> <span index="5"></span> </div> <a href="javascript:;" id="prev" class="arrow">&lt;</a> <a href="javascript:;" id="next" class="arrow">&gt;</a> </div>

</body> </html>

下面依次按照功能列表完成Js代码

左右按钮切换功能

window.onload = function() {
    // 先获取元素
    var container = document.getElementById('container');
    var list = document.getElementById('list');
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');

<span class="c1">// 添加事件</span>
<span class="nx">prev</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">animate</span><span class="p">(</span><span class="mi">600</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">animate</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">offset</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
<span class="p">}</span>

}

这样就是实现了左右按钮动画切换效果,但是当切换到最左边或者最右边的时候,就会出现空白,所以还需要实现无限滚动效果。

无限滚动

window.onload = function() {
    // 先获取元素
    var container = document.getElementById('container');
    var list = document.getElementById('list');
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');

<span class="c1">// 添加事件</span>
<span class="nx">prev</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">animate</span><span class="p">(</span><span class="mi">600</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">animate</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">newLeft</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">offset</span><span class="p">;</span>
    <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">newLeft</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="c1">// 判断边界</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">newLeft</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">600</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">newLeft</span> <span class="o">=</span> <span class="o">-</span><span class="mi">3000</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">newLeft</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">3000</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">newLeft</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>

}

按钮跟随图片一起变化

window.onload = function() {
    // 先获取元素
    var container = document.getElementById('container');
    var list = document.getElementById('list');
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');

<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

<span class="c1">// 添加事件</span>
<span class="nx">prev</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span><span class="c1">//index不能小于1或者大于5</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>           
    <span class="p">}</span>
    <span class="nx">showButton</span><span class="p">();</span>
    <span class="nx">animate</span><span class="p">(</span><span class="mi">600</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>           
    <span class="p">}</span>
    <span class="nx">showButton</span><span class="p">();</span>
    <span class="nx">animate</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span><span class="p">);</span>

<span class="p">}</span>
<span class="kd">function</span> <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">newLeft</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">offset</span><span class="p">;</span>
    <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">newLeft</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">newLeft</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">600</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">newLeft</span> <span class="o">=</span> <span class="o">-</span><span class="mi">3000</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">newLeft</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">3000</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">newLeft</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="c1">// 小按钮显示效果</span>
<span class="kd">function</span> <span class="nx">showButton</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span><span class="s1">''</span><span class="p">;</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="p">}</span>        
    <span class="p">}</span>
    <span class="nx">button</span><span class="p">[</span><span class="nx">index</span> <span class="o">-</span> <span class="mi">1</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s1">'on'</span><span class="p">;</span>
<span class="p">}</span>

}

小按钮切换

window.onload = function() {
    // 先获取元素
    var container = document.getElementById('container');
    var list = document.getElementById('list');
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');

<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

<span class="c1">// 添加事件</span>
<span class="nx">prev</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span><span class="c1">//index不能小于1或者大于5</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>           
    <span class="p">}</span>
    <span class="nx">showButton</span><span class="p">();</span>
    <span class="nx">animate</span><span class="p">(</span><span class="mi">600</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>           
    <span class="p">}</span>
    <span class="nx">showButton</span><span class="p">();</span>
    <span class="nx">animate</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span><span class="p">);</span>

<span class="p">}</span>

<span class="c1">// 小按钮切换</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span><span class="c1">//如果当前按钮时不进行操作</span>
        <span class="kd">var</span> <span class="nx">myIndex</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">getAttribute</span><span class="p">(</span><span class="s1">'index'</span><span class="p">));</span>
        <span class="kd">var</span> <span class="nx">offset</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="p">(</span><span class="nx">myIndex</span> <span class="o">-</span> <span class="nx">index</span><span class="p">);</span>
        <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">);</span> 
        <span class="nx">index</span> <span class="o">=</span> <span class="nx">myIndex</span><span class="p">;</span>
        <span class="nx">showButton</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">newLeft</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">offset</span><span class="p">;</span>
    <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">newLeft</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">newLeft</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">600</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">newLeft</span> <span class="o">=</span> <span class="o">-</span><span class="mi">3000</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">newLeft</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">3000</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">newLeft</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="c1">// 小按钮显示效果</span>
<span class="kd">function</span> <span class="nx">showButton</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span><span class="s1">''</span><span class="p">;</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="p">}</span>        
    <span class="p">}</span>
    <span class="nx">button</span><span class="p">[</span><span class="nx">index</span> <span class="o">-</span> <span class="mi">1</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s1">'on'</span><span class="p">;</span>
<span class="p">}</span>

}

动画函数

window.onload = function() {
    // 先获取元素
    var container = document.getElementById('container');
    var list = document.getElementById('list');
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');
    var animate = false;

<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

<span class="c1">// 添加事件</span>
<span class="nx">prev</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span><span class="c1">//index不能小于1或者大于5</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>           
    <span class="p">}</span>
    <span class="nx">showButton</span><span class="p">();</span>
    <span class="c1">// animate为falsel时才执行动画</span>
    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">animate</span><span class="p">){</span>
        <span class="nx">animate</span><span class="p">(</span><span class="mi">600</span><span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
<span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>           
    <span class="p">}</span>
    <span class="nx">showButton</span><span class="p">();</span>
    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">animate</span><span class="p">){</span>
        <span class="nx">animate</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">// 小按钮切换</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span><span class="c1">//如果当前按钮时不进行操作</span>
        <span class="kd">var</span> <span class="nx">myIndex</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">getAttribute</span><span class="p">(</span><span class="s1">'index'</span><span class="p">));</span>
        <span class="kd">var</span> <span class="nx">offset</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="p">(</span><span class="nx">myIndex</span> <span class="o">-</span> <span class="nx">index</span><span class="p">);</span>
        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">animate</span><span class="p">){</span>
            <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">);</span>              
        <span class="p">}</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="nx">myIndex</span><span class="p">;</span>
        <span class="nx">showButton</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">animated</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
    <span class="kd">var</span> <span class="nx">time</span> <span class="o">=</span> <span class="mi">300</span><span class="p">;</span><span class="c1">//动画时间</span>
    <span class="kd">var</span> <span class="nx">inteval</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span><span class="c1">//每次位移时间</span>
    <span class="kd">var</span> <span class="nx">speed</span> <span class="o">=</span> <span class="nx">offset</span><span class="o">/</span><span class="p">(</span><span class="nx">time</span><span class="o">/</span><span class="nx">inteval</span><span class="p">);</span><span class="c1">//每次位移距离</span>
    <span class="kd">var</span> <span class="nx">left</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">offset</span><span class="p">;</span>

    <span class="kd">var</span> <span class="nx">go</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(){</span>
        <span class="k">if</span> <span class="p">(</span> <span class="p">(</span><span class="nx">speed</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nx">left</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="nx">speed</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nx">left</span><span class="p">))</span> <span class="p">{</span>
            <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">speed</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
            <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">go</span><span class="p">,</span> <span class="nx">inteval</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="k">else</span> <span class="p">{</span>
            <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">left</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
            <span class="k">if</span><span class="p">(</span><span class="nx">left</span><span class="o">&gt;-</span><span class="mi">200</span><span class="p">){</span>
                <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="nx">len</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">if</span><span class="p">(</span><span class="nx">left</span><span class="o">&lt;</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="nx">len</span><span class="p">))</span> <span class="p">{</span>
                <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="s1">'-600px'</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="nx">animated</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="nx">go</span><span class="p">();</span>
<span class="p">}</span>
<span class="c1">// 小按钮显示效果</span>
<span class="kd">function</span> <span class="nx">showButton</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span><span class="s1">''</span><span class="p">;</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="p">}</span>        
    <span class="p">}</span>
    <span class="nx">button</span><span class="p">[</span><span class="nx">index</span> <span class="o">-</span> <span class="mi">1</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s1">'on'</span><span class="p">;</span>
<span class="p">}</span>

}

自动播放

window.onload = function () {
    var container = document.getElementById('container');
    var list = document.getElementById('list');
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');
    var index = 1;
    var len = 5;
    var animated = false;
    var interval = 3000;
    var timer;

<span class="kd">function</span> <span class="nx">animate</span> <span class="p">(</span><span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>  
    <span class="k">if</span> <span class="p">(</span><span class="nx">offset</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="nx">animated</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
    <span class="kd">var</span> <span class="nx">time</span> <span class="o">=</span> <span class="mi">300</span><span class="p">;</span>
    <span class="kd">var</span> <span class="nx">inteval</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
    <span class="kd">var</span> <span class="nx">speed</span> <span class="o">=</span> <span class="nx">offset</span><span class="o">/</span><span class="p">(</span><span class="nx">time</span><span class="o">/</span><span class="nx">inteval</span><span class="p">);</span>
    <span class="kd">var</span> <span class="nx">left</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">offset</span><span class="p">;</span>

    <span class="kd">var</span> <span class="nx">go</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(){</span>
        <span class="k">if</span> <span class="p">(</span> <span class="p">(</span><span class="nx">speed</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nx">left</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="nx">speed</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nx">left</span><span class="p">))</span> <span class="p">{</span>
            <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">)</span> <span class="o">+</span> <span class="nx">speed</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
            <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">go</span><span class="p">,</span> <span class="nx">inteval</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="k">else</span> <span class="p">{</span>
            <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">left</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
            <span class="k">if</span><span class="p">(</span><span class="nx">left</span><span class="o">&gt;-</span><span class="mi">200</span><span class="p">){</span>
                <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="nx">len</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="k">if</span><span class="p">(</span><span class="nx">left</span><span class="o">&lt;</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="nx">len</span><span class="p">))</span> <span class="p">{</span>
                <span class="nx">list</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="s1">'-600px'</span><span class="p">;</span>
            <span class="p">}</span>
            <span class="nx">animated</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="nx">go</span><span class="p">();</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">showButton</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span> <span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span><span class="p">(</span> <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">){</span>
            <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
            <span class="k">break</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="nx">buttons</span><span class="p">[</span><span class="nx">index</span> <span class="o">-</span> <span class="mi">1</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s1">'on'</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">play</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">timer</span> <span class="o">=</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span><span class="p">();</span>
        <span class="nx">play</span><span class="p">();</span>
    <span class="p">},</span> <span class="nx">interval</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">stop</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">timer</span><span class="p">);</span>
<span class="p">}</span>

<span class="nx">next</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">animated</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="nx">animate</span><span class="p">(</span><span class="o">-</span><span class="mi">600</span><span class="p">);</span>
    <span class="nx">showButton</span><span class="p">();</span>
<span class="p">}</span>
<span class="nx">prev</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">animated</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="nx">index</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="nx">animate</span><span class="p">(</span><span class="mi">600</span><span class="p">);</span>
    <span class="nx">showButton</span><span class="p">();</span>
<span class="p">}</span>

<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">buttons</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">buttons</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">animated</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'on'</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="kd">var</span> <span class="nx">myIndex</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">getAttribute</span><span class="p">(</span><span class="s1">'index'</span><span class="p">));</span>
        <span class="kd">var</span> <span class="nx">offset</span> <span class="o">=</span> <span class="o">-</span><span class="mi">600</span> <span class="o">*</span> <span class="p">(</span><span class="nx">myIndex</span> <span class="o">-</span> <span class="nx">index</span><span class="p">);</span>

        <span class="nx">animate</span><span class="p">(</span><span class="nx">offset</span><span class="p">);</span>
        <span class="nx">index</span> <span class="o">=</span> <span class="nx">myIndex</span><span class="p">;</span>
        <span class="nx">showButton</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="nx">container</span><span class="p">.</span><span class="nx">onmouseover</span> <span class="o">=</span> <span class="nx">stop</span><span class="p">;</span>
<span class="nx">container</span><span class="p">.</span><span class="nx">onmouseout</span> <span class="o">=</span> <span class="nx">play</span><span class="p">;</span>

<span class="nx">play</span><span class="p">();</span>

}