需要实现的功能列表:
- 左右按钮切换
- 图片无限滚动
- 小按钮切换
- 动画函数
- 自动播放
html和css代码如下:
<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"><</a> <a href="javascript:;" id="next" class="arrow">></a> </div>
</body> </html>
下面依次按照功能列表完成Js代码
左右按钮切换功能
<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>
}
这样就是实现了左右按钮动画切换效果,但是当切换到最左边或者最右边的时候,就会出现空白,所以还需要实现无限滚动效果。
无限滚动
<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">></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"><</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="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">></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"><</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"><</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>
}
小按钮切换
<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"><</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">></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"><</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"><</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>
}
动画函数
<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"><</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">></span> <span class="mi">0</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">left</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="nx">speed</span> <span class="o"><</span> <span class="mi">0</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">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">>-</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"><</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"><</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>
}
自动播放
<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">></span> <span class="mi">0</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">left</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="nx">speed</span> <span class="o"><</span> <span class="mi">0</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">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">>-</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"><</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"><</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"><</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>
}