前端学习<四>JavaScript基础——38-offset相关属性和匀速动画(含轮播图的实现)

前言

JS动画的主要内容如下:

1、三大家族和一个事件对象:

  • 三大家族:offset/scroll/client。也叫三大系列。

  • 事件对象/event(事件被触动时,鼠标和键盘的状态)(通过属性控制)。

2、动画(闪现/匀速/缓动)

3、冒泡/兼容/封装

offset 家族的组成

我们知道,JS动画的三大家族包括:offset/scroll/client。今天来讲一下offset,以及与其相关的匀速动画。

offset的中文是:偏移,补偿,位移。

js中有一套方便的获取元素尺寸的办法就是offset家族。offset家族包括:

  • offsetWidth

  • offsetHight

  • offsetLeft

  • offsetTop

  • offsetParent

下面分别介绍。

1、offsetWidth 和 offsetHight

offsetWidthoffsetHight:获取元素的宽高 + padding + border,不包括margin。如下:

  • offsetWidth = width + padding + border

  • offsetHeight = Height + padding + border

这两个属性,他们绑定在了所有的节点元素上。获取元素之后,只要调用这两个属性,我们就能够获取元素节点的宽和高。

举例如下:

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!DOCTYPE html></span><span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">head</span> <span style="color:#0000cc">lang</span>=<span style="color:#aa1111">"en"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">charset</span>=<span style="color:#aa1111">"UTF-8"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></</span><span style="color:#117700">title</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#117700">div</span> {<span style="color:#000000">width</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">padding</span>: <span style="color:#116644">10px</span>;<span style="color:#000000">border</span>: <span style="color:#116644">10px</span> <span style="color:#221199">solid</span> <span style="color:#221199">#000</span>;<span style="color:#000000">margin</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">background-color</span>: <span style="color:#770088">pink</span>;}<span style="color:#117700"></</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>​<span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#770088">var</span> <span style="color:#0000ff">div1</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByTagName</span>(<span style="color:#aa1111">"div"</span>)[<span style="color:#116644">0</span>];​<span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#000000">div1</span>.<span style="color:#000000">offsetHeight</span>);          <span style="color:#aa5500">//打印结果:140(100+20+20)</span><span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#770088">typeof</span> <span style="color:#000000">div1</span>.<span style="color:#000000">offsetHeight</span>);   <span style="color:#aa5500">//打印结果:number</span>​<span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>

2、offsetParent

offsetParent:获取当前元素的定位父元素

  • 如果当前元素的父元素,有CSS定位(position为absolute、relative、fixed),那么 offsetParent 获取的是最近的那个父元素。

  • 如果当前元素的父元素,没有CSS定位(position为absolute、relative、fixed),那么offsetParent 获取的是body

举例:

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!DOCTYPE html></span><span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">head</span> <span style="color:#0000cc">lang</span>=<span style="color:#aa1111">"en"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">charset</span>=<span style="color:#aa1111">"UTF-8"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></</span><span style="color:#117700">title</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box1"</span> <span style="color:#0000cc">style</span>=<span style="color:#aa1111">"position: absolute;"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box2"</span> <span style="color:#0000cc">style</span>=<span style="color:#aa1111">"position: fixed;"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box3"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span>​<span style="color:#770088">var</span> <span style="color:#0000ff">box3</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByClassName</span>(<span style="color:#aa1111">"box3"</span>)[<span style="color:#116644">0</span>];​<span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#000000">box3</span>.<span style="color:#000000">offsetParent</span>);<span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>

打印结果:

3、offsetLeft 和 offsetTop

offsetLeft:当前元素相对于其定位父元素的水平偏移量。

offsetTop:当前元素相对于其定位父元素的垂直偏移量。

备注:从父亲的 padding 开始算起,父亲的 border 不算在内。

举例:

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!DOCTYPE html></span><span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">head</span> <span style="color:#0000cc">lang</span>=<span style="color:#aa1111">"en"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">charset</span>=<span style="color:#aa1111">"UTF-8"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></</span><span style="color:#117700">title</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#555555">.box1</span> {<span style="color:#000000">width</span>: <span style="color:#116644">300px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">300px</span>;<span style="color:#000000">padding</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">margin</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">position</span>: <span style="color:#221199">relative</span>;<span style="color:#000000">border</span>: <span style="color:#116644">100px</span> <span style="color:#221199">solid</span> <span style="color:#221199">#000</span>;<span style="color:#000000">background-color</span>: <span style="color:#770088">pink</span>;}​<span style="color:#555555">.box2</span> {<span style="color:#000000">width</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">background-color</span>: <span style="color:#770088">red</span>;<span style="color:#aa5500">/*position: absolute;*/</span><span style="color:#aa5500">/*left: 10px;*/</span><span style="color:#aa5500">/*top: 10px;*/</span>}<span style="color:#117700"></</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box1"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box2"</span> <span style="color:#0000cc">style</span>=<span style="color:#aa1111">"left: 10px"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>​<span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span>​<span style="color:#770088">var</span> <span style="color:#0000ff">box2</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByClassName</span>(<span style="color:#aa1111">"box2"</span>)[<span style="color:#116644">0</span>];​<span style="color:#aa5500">//offsetTop和offsetLeft</span><span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#000000">box2</span>.<span style="color:#000000">offsetLeft</span>);  <span style="color:#aa5500">//100</span><span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#000000">box2</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span>);  <span style="color:#aa5500">//10px</span>​​<span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span>​<span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>

在父盒子有定位的情况下,offsetLeft == style.left(去掉px之后)。注意,后者只识别行内样式。但区别不仅仅于此,下面会讲。

offsetLeft 和 style.left 区别

(1)最大区别在于:

offsetLeft 可以返回无定位父元素的偏移量。如果父元素中都没有定位,则body为准。

style.left 只能获取行内样式,如果父元素中都没有设置定位,则返回""(意思是,返回空字符串);

(2)offsetTop 返回的是数字,而 style.top 返回的是字符串,而且还带有单位:px。

比如:

<span style="background-color:#f8f8f8"><span style="color:#333333"> ​<span style="color:#000000">div</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">=</span> <span style="color:#116644">100</span>;<span style="color:#000000">div</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"100px"</span>;​</span></span>

(3)offsetLeft 和 offsetTop 只读,而 style.left 和 style.top 可读写(只读是获取值,可写是修改值)

总结:我们一般的做法是:用offsetLeft 和 offsetTop 获取值,用style.left 和 style.top 赋值(比较方便)。理由如下:

  • style.left:只能获取行内式,获取的值可能为空,容易出现NaN。

  • offsetLeft:获取值特别方便,而且是现成的number,方便计算。它是只读的,不能赋值。

动画的种类

  • 闪现(基本不用)

  • 匀速(本文重点)

  • 缓动(后续重点)

简单举例如下:(每间隔500ms,向右移动盒子100px)

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!DOCTYPE html></span><span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">head</span> <span style="color:#0000cc">lang</span>=<span style="color:#aa1111">"en"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">charset</span>=<span style="color:#aa1111">"UTF-8"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></</span><span style="color:#117700">title</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#117700">div</span> {<span style="color:#000000">width</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">background-color</span>: <span style="color:#770088">pink</span>;<span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;}<span style="color:#117700"></</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">button</span><span style="color:#117700">></span>动画<span style="color:#117700"></</span><span style="color:#117700">button</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box"</span> <span style="color:#0000cc">style</span>=<span style="color:#aa1111">"left: 0px"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span>​<span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#770088">var</span> <span style="color:#0000ff">btn</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByTagName</span>(<span style="color:#aa1111">"button"</span>)[<span style="color:#116644">0</span>];<span style="color:#770088">var</span> <span style="color:#0000ff">div</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByTagName</span>(<span style="color:#aa1111">"div"</span>)[<span style="color:#116644">0</span>];​<span style="color:#aa5500">//1、闪动</span><span style="color:#aa5500">//    btn.onclick = function () {</span><span style="color:#aa5500">//        div.style.left = "500px";</span><span style="color:#aa5500">//    }</span>​<span style="color:#aa5500">//2、匀速运动</span><span style="color:#000000">btn</span>.<span style="color:#000000">onclick</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#aa5500">//定时器,每隔一定的时间向右走一些</span><span style="color:#000000">setInterval</span>(<span style="color:#770088">function</span> () {<span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#000000">parseInt</span>(<span style="color:#000000">div</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span>));<span style="color:#aa5500">//动画原理: 盒子未来的位置 = 盒子现在的位置 + 步长;</span><span style="color:#aa5500">//方法1:用offsetLeft获取值,用style.left赋值。</span><span style="color:#000000">div</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#000000">div</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">+</span> <span style="color:#116644">100</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'px'</span>;​<span style="color:#aa5500">// 方法2:必须一开始就在DOM节点上添加 style="left: 0px;"属性,才能用方法2。否则, div.style.left 的值为 NaN</span><span style="color:#aa5500">// div.style.left = parseInt(div.style.left)+100+"px";  //方法2:</span>}, <span style="color:#116644">500</span>);};<span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>

效果如下:

匀速动画的封装:每间隔30ms,移动盒子10px【重要】

代码如下:

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!DOCTYPE html></span><span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">head</span> <span style="color:#0000cc">lang</span>=<span style="color:#aa1111">"en"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">charset</span>=<span style="color:#aa1111">"UTF-8"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></</span><span style="color:#117700">title</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#555555">.box1</span> {<span style="color:#000000">margin</span>: <span style="color:#116644">0</span>;<span style="color:#000000">padding</span>: <span style="color:#116644">5px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">300px</span>;<span style="color:#000000">background-color</span>: <span style="color:#221199">#ddd</span>;<span style="color:#000000">position</span>: <span style="color:#221199">relative</span>;}​<span style="color:#117700">button</span> {<span style="color:#000000">margin</span>: <span style="color:#116644">5px</span>;}​<span style="color:#555555">.box2</span> {<span style="color:#000000">width</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">background-color</span>: <span style="color:#770088">red</span>;<span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;<span style="color:#000000">left</span>: <span style="color:#116644">195px</span>;<span style="color:#000000">top</span>: <span style="color:#116644">40px</span>;}​<span style="color:#555555">.box3</span> {<span style="color:#000000">width</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">100px</span>;<span style="color:#000000">background-color</span>: <span style="color:#770088">yellow</span>;<span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;<span style="color:#000000">left</span>: <span style="color:#116644">0</span>;<span style="color:#000000">top</span>: <span style="color:#116644">150px</span>;}<span style="color:#117700"></</span><span style="color:#117700">style</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box1"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">button</span><span style="color:#117700">></span>运动到 left = 200px<span style="color:#117700"></</span><span style="color:#117700">button</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">button</span><span style="color:#117700">></span>运动到 left = 400px<span style="color:#117700"></</span><span style="color:#117700">button</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box2"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"box3"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>​<span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#770088">var</span> <span style="color:#0000ff">btnArr</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByTagName</span>(<span style="color:#aa1111">"button"</span>);<span style="color:#770088">var</span> <span style="color:#0000ff">box2</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByClassName</span>(<span style="color:#aa1111">"box2"</span>)[<span style="color:#116644">0</span>];<span style="color:#770088">var</span> <span style="color:#0000ff">box3</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByClassName</span>(<span style="color:#aa1111">"box3"</span>)[<span style="color:#116644">0</span>];​<span style="color:#aa5500">//绑定事件</span><span style="color:#000000">btnArr</span>[<span style="color:#116644">0</span>].<span style="color:#000000">onclick</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#aa5500">//如果有一天我们要传递另外一个盒子,那么我们的方法就不好用了</span><span style="color:#aa5500">//所以我们要增加第二个参数,被移动的盒子本身。</span><span style="color:#000000">animate</span>(<span style="color:#000000">box2</span>, <span style="color:#116644">200</span>);<span style="color:#000000">animate</span>(<span style="color:#000000">box3</span>, <span style="color:#116644">200</span>);}​<span style="color:#000000">btnArr</span>[<span style="color:#116644">1</span>].<span style="color:#000000">onclick</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#000000">animate</span>(<span style="color:#000000">box2</span>, <span style="color:#116644">400</span>);<span style="color:#000000">animate</span>(<span style="color:#000000">box3</span>, <span style="color:#116644">400</span>);}​<span style="color:#aa5500">//【重要】方法的封装:每间隔30ms,将盒子向右移动10px</span><span style="color:#770088">function</span> <span style="color:#0000ff">animate</span>(<span style="color:#0000ff">ele</span>, <span style="color:#0000ff">target</span>) {<span style="color:#aa5500">//要用定时器,先清除定时器</span><span style="color:#aa5500">//一个盒子只能有一个定时器,这样的话,不会和其他盒子出现定时器冲突</span><span style="color:#aa5500">//我们可以把定时器本身,当成为盒子的一个属性</span><span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span>);<span style="color:#aa5500">//我们要求盒子既能向前又能向后,那么我们的步长就得有正有负</span><span style="color:#aa5500">//目标值如果大于当前值取正,目标值如果小于当前值取负</span><span style="color:#770088">var</span> <span style="color:#0000ff">speed</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">></span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">?</span> <span style="color:#116644">10</span> : <span style="color:#981a1a">-</span><span style="color:#116644">10</span>;  <span style="color:#aa5500">//speed指的是步长</span><span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">setInterval</span>(<span style="color:#770088">function</span> () {<span style="color:#aa5500">//在执行之前就获取当前值和目标值之差</span><span style="color:#770088">var</span> <span style="color:#0000ff">val</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">-</span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span>;<span style="color:#0055aa">ele</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">+</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;<span style="color:#aa5500">//移动的过程中,如果目标值和当前值之差如果小于步长,那么就不能在前进了</span><span style="color:#aa5500">//因为步长有正有负,所有转换成绝对值来比较</span><span style="color:#770088">if</span> (<span style="color:#000000">Math</span>.<span style="color:#000000">abs</span>(<span style="color:#0055aa">val</span>) <span style="color:#981a1a"><</span> <span style="color:#000000">Math</span>.<span style="color:#000000">abs</span>(<span style="color:#0055aa">speed</span>)) {<span style="color:#0055aa">ele</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;<span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span>);}}, <span style="color:#116644">30</span>)}<span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span></span>

实现的效果:

上方代码中的方法封装,可以作为一个模板步骤,要记住。其实,这个封装的方法,写成下面这样,会更严谨,更容易理解:(将if语句进行了改进)

<span style="background-color:#f8f8f8"><span style="color:#333333">     <span style="color:#aa5500">//【重要】方法的封装:每间隔30ms,将盒子向右移动10px</span><span style="color:#770088">function</span> <span style="color:#0000ff">animate</span>(<span style="color:#0000ff">ele</span>, <span style="color:#0000ff">target</span>) {<span style="color:#aa5500">//要用定时器,先清除定时器</span><span style="color:#aa5500">//一个盒子只能有一个定时器,这样的话,不会和其他盒子出现定时器冲突</span><span style="color:#aa5500">//我们可以把定时器本身,当成为盒子的一个属性</span><span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span>);<span style="color:#aa5500">//我们要求盒子既能向前又能向后,那么我们的步长就得有正有负</span><span style="color:#aa5500">//目标值如果大于当前值取正,目标值如果小于当前值取负</span><span style="color:#770088">var</span> <span style="color:#0000ff">speed</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">></span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">?</span> <span style="color:#116644">10</span> : <span style="color:#981a1a">-</span><span style="color:#116644">10</span>;  <span style="color:#aa5500">//speed指的是步长</span><span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">setInterval</span>(<span style="color:#770088">function</span> () {<span style="color:#aa5500">//在执行之前就获取当前值和目标值之差</span><span style="color:#770088">var</span> <span style="color:#0000ff">val</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">-</span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span>;​<span style="color:#aa5500">//移动的过程中,如果目标值和当前值之差如果小于步长,那么就不能在前进了</span><span style="color:#aa5500">//因为步长有正有负,所有转换成绝对值来比较</span><span style="color:#770088">if</span> (<span style="color:#000000">Math</span>.<span style="color:#000000">abs</span>(<span style="color:#0055aa">val</span>) <span style="color:#981a1a"><</span> <span style="color:#000000">Math</span>.<span style="color:#000000">abs</span>(<span style="color:#0055aa">speed</span>)) {  <span style="color:#aa5500">//如果val小于步长,则直接到达目的地;否则,每次移动一个步长</span><span style="color:#0055aa">ele</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;<span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span>);} <span style="color:#770088">else</span> {<span style="color:#0055aa">ele</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">+</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;}}, <span style="color:#116644">30</span>)}</span></span>

代码举例:轮播图的实现

完整版代码如下:(注释已经比较详细)

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!doctype html></span><span style="color:#117700"><</span><span style="color:#117700">html</span> <span style="color:#0000cc">lang</span>=<span style="color:#aa1111">"en"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">http-equiv</span>=<span style="color:#aa1111">"Content-Type"</span> <span style="color:#0000cc">content</span>=<span style="color:#aa1111">"text/html; charset=utf-8"</span><span style="color:#117700">/></span><span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></span>无标题文档<span style="color:#117700"></</span><span style="color:#117700">title</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">style</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text/css"</span><span style="color:#117700">></span>* {<span style="color:#000000">padding</span>: <span style="color:#116644">0</span>;<span style="color:#000000">margin</span>: <span style="color:#116644">0</span>;<span style="color:#000000">list-style</span>: <span style="color:#221199">none</span>;<span style="color:#000000">border</span>: <span style="color:#116644">0</span>;}​<span style="color:#555555">.all</span> {<span style="color:#000000">width</span>: <span style="color:#116644">500px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">200px</span>;<span style="color:#000000">padding</span>: <span style="color:#116644">7px</span>;<span style="color:#000000">border</span>: <span style="color:#116644">1px</span> <span style="color:#221199">solid</span> <span style="color:#221199">#ccc</span>;<span style="color:#000000">margin</span>: <span style="color:#116644">100px</span> <span style="color:#221199">auto</span>;<span style="color:#000000">position</span>: <span style="color:#221199">relative</span>;}​<span style="color:#555555">.screen</span> {<span style="color:#000000">width</span>: <span style="color:#116644">500px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">200px</span>;<span style="color:#000000">overflow</span>: <span style="color:#221199">hidden</span>;<span style="color:#000000">position</span>: <span style="color:#221199">relative</span>;}​<span style="color:#555555">.screen</span> <span style="color:#117700">li</span> {<span style="color:#000000">width</span>: <span style="color:#116644">500px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">200px</span>;<span style="color:#000000">overflow</span>: <span style="color:#221199">hidden</span>;<span style="color:#000000">float</span>: <span style="color:#221199">left</span>;}​<span style="color:#555555">.screen</span> <span style="color:#117700">ul</span> {<span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;<span style="color:#000000">left</span>: <span style="color:#116644">0</span>;<span style="color:#000000">top</span>: <span style="color:#116644">0px</span>;<span style="color:#000000">width</span>: <span style="color:#116644">3000px</span>;}​<span style="color:#555555">.all</span> <span style="color:#117700">ol</span> {<span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;<span style="color:#000000">right</span>: <span style="color:#116644">10px</span>;<span style="color:#000000">bottom</span>: <span style="color:#116644">10px</span>;<span style="color:#000000">line-height</span>: <span style="color:#116644">20px</span>;<span style="color:#000000">text-align</span>: <span style="color:#221199">center</span>;}​<span style="color:#555555">.all</span> <span style="color:#117700">ol</span> <span style="color:#117700">li</span> {<span style="color:#000000">float</span>: <span style="color:#221199">left</span>;<span style="color:#000000">width</span>: <span style="color:#116644">20px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">20px</span>;<span style="color:#000000">background</span>: <span style="color:#221199">#fff</span>;<span style="color:#000000">border</span>: <span style="color:#116644">1px</span> <span style="color:#221199">solid</span> <span style="color:#221199">#ccc</span>;<span style="color:#000000">margin-left</span>: <span style="color:#116644">10px</span>;<span style="color:#000000">cursor</span>: <span style="color:#221199">pointer</span>;}​<span style="color:#555555">.all</span> <span style="color:#117700">ol</span> <span style="color:#117700">li</span><span style="color:#555555">.current</span> {<span style="color:#000000">background</span>: <span style="color:#770088">yellow</span>;}​<span style="color:#3300aa">#arr</span> {<span style="color:#000000">display</span>: <span style="color:#221199">none</span>;}​<span style="color:#3300aa">#arr</span> <span style="color:#117700">span</span> {<span style="color:#000000">width</span>: <span style="color:#116644">40px</span>;<span style="color:#000000">height</span>: <span style="color:#116644">40px</span>;<span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;<span style="color:#000000">left</span>: <span style="color:#116644">5px</span>;<span style="color:#000000">top</span>: <span style="color:#116644">50%</span>;<span style="color:#000000">margin-top</span>: <span style="color:#116644">-20px</span>;<span style="color:#000000">background</span>: <span style="color:#221199">#000</span>;<span style="color:#000000">cursor</span>: <span style="color:#221199">pointer</span>;<span style="color:#000000">line-height</span>: <span style="color:#116644">40px</span>;<span style="color:#000000">text-align</span>: <span style="color:#221199">center</span>;<span style="color:#000000">font-weight</span>: <span style="color:#221199">bold</span>;<span style="color:#000000">font-family</span>: <span style="color:#aa1111">'黑体'</span>;<span style="color:#000000">font-size</span>: <span style="color:#116644">30px</span>;<span style="color:#000000">color</span>: <span style="color:#221199">#fff</span>;<span style="color:#000000">opacity</span>: <span style="color:#116644">0.3</span>;<span style="color:#000000">border</span>: <span style="color:#116644">1px</span> <span style="color:#221199">solid</span> <span style="color:#221199">#fff</span>;}​<span style="color:#3300aa">#arr</span> <span style="color:#3300aa">#right</span> {<span style="color:#000000">right</span>: <span style="color:#116644">5px</span>;<span style="color:#000000">left</span>: <span style="color:#221199">auto</span>;}<span style="color:#117700"></</span><span style="color:#117700">style</span><span style="color:#117700">></span>​<span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#000000">window</span>.<span style="color:#000000">onload</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {​<span style="color:#aa5500">//需求:无缝滚动。</span><span style="color:#aa5500">//思路:赋值第一张图片放到ul的最后,然后当图片切换到第五张的时候</span><span style="color:#aa5500">//     直接切换第六章,再次从第一张切换到第二张的时候先瞬间切换到</span><span style="color:#aa5500">//     第一张图片,然后滑动到第二张</span><span style="color:#aa5500">//步骤:</span><span style="color:#aa5500">//1.获取事件源及相关元素。(老三步)</span><span style="color:#aa5500">//2.复制第一张图片所在的li,添加到ul的最后面。</span><span style="color:#aa5500">//3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。</span><span style="color:#aa5500">//4.鼠标放到ol的li上切换图片</span><span style="color:#aa5500">//5.添加定时器</span><span style="color:#aa5500">//6.左右切换图片(鼠标放上去隐藏,移开显示)</span>​​<span style="color:#aa5500">//1.获取事件源及相关元素。(老三步)</span><span style="color:#770088">var</span> <span style="color:#0000ff">all</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementById</span>(<span style="color:#aa1111">"all"</span>);<span style="color:#770088">var</span> <span style="color:#0000ff">screen</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">all</span>.<span style="color:#000000">firstElementChild</span> <span style="color:#981a1a">||</span> <span style="color:#0055aa">all</span>.<span style="color:#000000">firstChild</span>;<span style="color:#770088">var</span> <span style="color:#0000ff">imgWidth</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">screen</span>.<span style="color:#000000">offsetWidth</span>;<span style="color:#770088">var</span> <span style="color:#0000ff">ul</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">screen</span>.<span style="color:#000000">firstElementChild</span> <span style="color:#981a1a">||</span> <span style="color:#0055aa">screen</span>.<span style="color:#000000">firstChild</span>;<span style="color:#770088">var</span> <span style="color:#0000ff">ol</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">screen</span>.<span style="color:#000000">children</span>[<span style="color:#116644">1</span>];<span style="color:#770088">var</span> <span style="color:#0000ff">div</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">screen</span>.<span style="color:#000000">lastElementChild</span> <span style="color:#981a1a">||</span> <span style="color:#0055aa">screen</span>.<span style="color:#000000">lastChild</span>;<span style="color:#770088">var</span> <span style="color:#0000ff">spanArr</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">div</span>.<span style="color:#000000">children</span>;​<span style="color:#aa5500">//2.复制第一张图片所在的li,添加到ul的最后面。</span><span style="color:#770088">var</span> <span style="color:#0000ff">ulNewLi</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">ul</span>.<span style="color:#000000">children</span>[<span style="color:#116644">0</span>].<span style="color:#000000">cloneNode</span>(<span style="color:#221199">true</span>);<span style="color:#0055aa">ul</span>.<span style="color:#000000">appendChild</span>(<span style="color:#0055aa">ulNewLi</span>);<span style="color:#aa5500">//3.给ol中添加li,ul中的个数-1个,并点亮第一个按钮。</span><span style="color:#770088">for</span> (<span style="color:#770088">var</span> <span style="color:#0000ff">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>; <span style="color:#0055aa">i</span> <span style="color:#981a1a"><</span> <span style="color:#0055aa">ul</span>.<span style="color:#000000">children</span>.<span style="color:#000000">length</span> <span style="color:#981a1a">-</span> <span style="color:#116644">1</span>; <span style="color:#0055aa">i</span><span style="color:#981a1a">++</span>) {<span style="color:#770088">var</span> <span style="color:#0000ff">olNewLi</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">createElement</span>(<span style="color:#aa1111">"li"</span>);<span style="color:#0055aa">olNewLi</span>.<span style="color:#000000">innerHTML</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">i</span> <span style="color:#981a1a">+</span> <span style="color:#116644">1</span>;<span style="color:#0055aa">ol</span>.<span style="color:#000000">appendChild</span>(<span style="color:#0055aa">olNewLi</span>)}<span style="color:#770088">var</span> <span style="color:#0000ff">olLiArr</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">ol</span>.<span style="color:#000000">children</span>;<span style="color:#0055aa">olLiArr</span>[<span style="color:#116644">0</span>].<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"current"</span>;​<span style="color:#aa5500">//4.鼠标放到ol的li上切换图片</span><span style="color:#770088">for</span> (<span style="color:#770088">var</span> <span style="color:#0000ff">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>; <span style="color:#0055aa">i</span> <span style="color:#981a1a"><</span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span>; <span style="color:#0055aa">i</span><span style="color:#981a1a">++</span>) {<span style="color:#aa5500">//自定义属性,把索引值绑定到元素的index属性上</span><span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">i</span>].<span style="color:#000000">index</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">i</span>;<span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">i</span>].<span style="color:#000000">onmouseover</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#aa5500">//排他思想</span><span style="color:#770088">for</span> (<span style="color:#770088">var</span> <span style="color:#0000ff">j</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>; <span style="color:#0055aa">j</span> <span style="color:#981a1a"><</span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span>; <span style="color:#0055aa">j</span><span style="color:#981a1a">++</span>) {<span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">j</span>].<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">""</span>;}<span style="color:#770088">this</span>.<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"current"</span>;<span style="color:#aa5500">//鼠标放到小的方块上的时候索引值和key以及square同步</span><span style="color:#aa5500">//                    key = this.index;</span><span style="color:#aa5500">//                    square = this.index;</span><span style="color:#000000">key</span> <span style="color:#981a1a">=</span> <span style="color:#000000">square</span> <span style="color:#981a1a">=</span> <span style="color:#770088">this</span>.<span style="color:#000000">index</span>;<span style="color:#aa5500">//移动盒子</span><span style="color:#000000">animate</span>(<span style="color:#0055aa">ul</span>, <span style="color:#981a1a">-</span><span style="color:#770088">this</span>.<span style="color:#000000">index</span> <span style="color:#981a1a">*</span> <span style="color:#0055aa">imgWidth</span>);}}​<span style="color:#aa5500">//5.添加定时器</span><span style="color:#770088">var</span> <span style="color:#0000ff">timer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">setInterval</span>(<span style="color:#000000">autoPlay</span>, <span style="color:#116644">1000</span>);​<span style="color:#aa5500">//固定向右切换图片</span><span style="color:#aa5500">//两个定时器(一个记录图片,一个记录小方块)</span><span style="color:#770088">var</span> <span style="color:#0000ff">key</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;<span style="color:#770088">var</span> <span style="color:#0000ff">square</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;​<span style="color:#770088">function</span> <span style="color:#0000ff">autoPlay</span>() {<span style="color:#aa5500">//通过控制key的自增来模拟图片的索引值,然后移动ul</span><span style="color:#0055aa">key</span><span style="color:#981a1a">++</span>;<span style="color:#770088">if</span> (<span style="color:#0055aa">key</span> <span style="color:#981a1a">></span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span>) {<span style="color:#aa5500">//图片已经滑动到最后一张,接下来,跳转到第一张,然后在滑动到第二张</span><span style="color:#0055aa">ul</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;<span style="color:#0055aa">key</span> <span style="color:#981a1a">=</span> <span style="color:#116644">1</span>;}<span style="color:#000000">animate</span>(<span style="color:#0055aa">ul</span>, <span style="color:#981a1a">-</span><span style="color:#0055aa">key</span> <span style="color:#981a1a">*</span> <span style="color:#0055aa">imgWidth</span>);<span style="color:#aa5500">//通过控制square的自增来模拟小方块的索引值,然后点亮盒子</span><span style="color:#aa5500">//排他思想做小方块</span><span style="color:#0055aa">square</span><span style="color:#981a1a">++</span>;<span style="color:#770088">if</span> (<span style="color:#0055aa">square</span> <span style="color:#981a1a">></span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span> <span style="color:#981a1a">-</span> <span style="color:#116644">1</span>) {<span style="color:#aa5500">//索引值不能大于等于5,如果等于5,立刻变为0;</span><span style="color:#0055aa">square</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>;}<span style="color:#770088">for</span> (<span style="color:#770088">var</span> <span style="color:#0000ff">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>; <span style="color:#0055aa">i</span> <span style="color:#981a1a"><</span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span>; <span style="color:#0055aa">i</span><span style="color:#981a1a">++</span>) {<span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">i</span>].<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">""</span>;}<span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">square</span>].<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"current"</span>;}​<span style="color:#aa5500">//鼠标放上去清除定时器,移开后在开启定时器</span><span style="color:#0055aa">all</span>.<span style="color:#000000">onmouseover</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#0055aa">div</span>.<span style="color:#000000">style</span>.<span style="color:#000000">display</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"block"</span>;<span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">timer</span>);}<span style="color:#0055aa">all</span>.<span style="color:#000000">onmouseout</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#0055aa">div</span>.<span style="color:#000000">style</span>.<span style="color:#000000">display</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"none"</span>;<span style="color:#0055aa">timer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">setInterval</span>(<span style="color:#0055aa">autoPlay</span>, <span style="color:#116644">1000</span>);}​<span style="color:#aa5500">//6.左右切换图片(鼠标放上去显示,移开隐藏)</span><span style="color:#0055aa">spanArr</span>[<span style="color:#116644">0</span>].<span style="color:#000000">onclick</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#aa5500">//通过控制key的自增来模拟图片的索引值,然后移动ul</span><span style="color:#0055aa">key</span><span style="color:#981a1a">--</span>;<span style="color:#770088">if</span> (<span style="color:#0055aa">key</span> <span style="color:#981a1a"><</span> <span style="color:#116644">0</span>) {<span style="color:#aa5500">//先移动到最后一张,然后key的值取之前一张的索引值,然后在向前移动</span><span style="color:#0055aa">ul</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#981a1a">-</span><span style="color:#0055aa">imgWidth</span> <span style="color:#981a1a">*</span> (<span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span>) <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;<span style="color:#0055aa">key</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span> <span style="color:#981a1a">-</span> <span style="color:#116644">1</span>;}<span style="color:#000000">animate</span>(<span style="color:#0055aa">ul</span>, <span style="color:#981a1a">-</span><span style="color:#0055aa">key</span> <span style="color:#981a1a">*</span> <span style="color:#0055aa">imgWidth</span>);<span style="color:#aa5500">//通过控制square的自增来模拟小方块的索引值,然后点亮盒子</span><span style="color:#aa5500">//排他思想做小方块</span><span style="color:#0055aa">square</span><span style="color:#981a1a">--</span>;<span style="color:#770088">if</span> (<span style="color:#0055aa">square</span> <span style="color:#981a1a"><</span> <span style="color:#116644">0</span>) {<span style="color:#aa5500">//索引值不能大于等于5,如果等于5,立刻变为0;</span><span style="color:#0055aa">square</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span> <span style="color:#981a1a">-</span> <span style="color:#116644">1</span>;}<span style="color:#770088">for</span> (<span style="color:#770088">var</span> <span style="color:#0000ff">i</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>; <span style="color:#0055aa">i</span> <span style="color:#981a1a"><</span> <span style="color:#0055aa">olLiArr</span>.<span style="color:#000000">length</span>; <span style="color:#0055aa">i</span><span style="color:#981a1a">++</span>) {<span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">i</span>].<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">""</span>;}<span style="color:#0055aa">olLiArr</span>[<span style="color:#0055aa">square</span>].<span style="color:#000000">className</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"current"</span>;}<span style="color:#0055aa">spanArr</span>[<span style="color:#116644">1</span>].<span style="color:#000000">onclick</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span> () {<span style="color:#aa5500">//右侧的和定时器一模一样</span><span style="color:#0055aa">autoPlay</span>();}​​<span style="color:#770088">function</span> <span style="color:#0000ff">animate</span>(<span style="color:#0000ff">ele</span>, <span style="color:#0000ff">target</span>) {<span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span>);<span style="color:#770088">var</span> <span style="color:#0000ff">speed</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">></span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">?</span> <span style="color:#116644">10</span> : <span style="color:#981a1a">-</span><span style="color:#116644">10</span>;<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">setInterval</span>(<span style="color:#770088">function</span> () {<span style="color:#770088">var</span> <span style="color:#0000ff">val</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">-</span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span>;<span style="color:#0055aa">ele</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">ele</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">+</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;​<span style="color:#770088">if</span> (<span style="color:#000000">Math</span>.<span style="color:#000000">abs</span>(<span style="color:#0055aa">val</span>) <span style="color:#981a1a"><</span> <span style="color:#000000">Math</span>.<span style="color:#000000">abs</span>(<span style="color:#0055aa">speed</span>)) {<span style="color:#0055aa">ele</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">target</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"px"</span>;<span style="color:#000000">clearInterval</span>(<span style="color:#0055aa">ele</span>.<span style="color:#000000">timer</span>);}}, <span style="color:#116644">10</span>)}}<span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>​<span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"all"</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">'all'</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">"screen"</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"screen"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">ul</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"ul"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">li</span><span style="color:#117700">><</span><span style="color:#117700">img</span> <span style="color:#0000cc">src</span>=<span style="color:#aa1111">"images/1.jpg"</span> <span style="color:#0000cc">width</span>=<span style="color:#aa1111">"500"</span> <span style="color:#0000cc">height</span>=<span style="color:#aa1111">"200"</span><span style="color:#117700">/></</span><span style="color:#117700">li</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">li</span><span style="color:#117700">><</span><span style="color:#117700">img</span> <span style="color:#0000cc">src</span>=<span style="color:#aa1111">"images/2.jpg"</span> <span style="color:#0000cc">width</span>=<span style="color:#aa1111">"500"</span> <span style="color:#0000cc">height</span>=<span style="color:#aa1111">"200"</span><span style="color:#117700">/></</span><span style="color:#117700">li</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">li</span><span style="color:#117700">><</span><span style="color:#117700">img</span> <span style="color:#0000cc">src</span>=<span style="color:#aa1111">"images/3.jpg"</span> <span style="color:#0000cc">width</span>=<span style="color:#aa1111">"500"</span> <span style="color:#0000cc">height</span>=<span style="color:#aa1111">"200"</span><span style="color:#117700">/></</span><span style="color:#117700">li</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">li</span><span style="color:#117700">><</span><span style="color:#117700">img</span> <span style="color:#0000cc">src</span>=<span style="color:#aa1111">"images/4.jpg"</span> <span style="color:#0000cc">width</span>=<span style="color:#aa1111">"500"</span> <span style="color:#0000cc">height</span>=<span style="color:#aa1111">"200"</span><span style="color:#117700">/></</span><span style="color:#117700">li</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">li</span><span style="color:#117700">><</span><span style="color:#117700">img</span> <span style="color:#0000cc">src</span>=<span style="color:#aa1111">"images/5.jpg"</span> <span style="color:#0000cc">width</span>=<span style="color:#aa1111">"500"</span> <span style="color:#0000cc">height</span>=<span style="color:#aa1111">"200"</span><span style="color:#117700">/></</span><span style="color:#117700">li</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">ul</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">ol</span><span style="color:#117700">></span>​<span style="color:#117700"></</span><span style="color:#117700">ol</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"arr"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">span</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"left"</span><span style="color:#117700">><</span><span style="color:#117700"></span></span><span style="color:#117700"><</span><span style="color:#117700">span</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"right"</span><span style="color:#117700">></span>><span style="color:#117700"></</span><span style="color:#117700">span</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span>​​</span></span>

实现效果:

温馨提示:动图太大,可以把http://img.smyhvae.com/20180202_2020.gif单独在浏览器中打开。

工程文件:2018-02-02-JS动画实现轮播图.rar

我的公众号

想学习更多技能?不妨关注我的公号:猿小馆

写在最后:希望大家可以点个关注点个赞,这对up真的很重要!谢谢!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/868.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java算法day59 | 单调栈part02 ● 503.下一个更大元素II ● 42. 接雨水

503.下一个更大元素II 思路&#xff1a; 相比于单纯寻找下一个最大元素&#xff0c;要遍历两边数组&#xff0c;注意i%nums.length。 class Solution {public int[] nextGreaterElements(int[] nums) {int[] resnew int[nums.length];for(int i0;i<res.length;i){res[i]-1;…

【大数据】bigtable,分布式数据库的鼻祖

目录 1.概述 2.数据模型 3.API 4.架构 5.一个完整的读写过程 6.如何查找到要的tablet 7.LSM树 1.概述 本文是作者阅读完bigtable论文后对bigtable进行的一个梳理&#xff0c;只涉及核心概念不涉及具体实操&#xff0c;具体实操会在后续的文章中推出。 GFS的出现虽然解…

指针专题(2)

前言 上一节我们学习了指针的相关内容&#xff0c;本节我们继续学习指针专题&#xff0c;更加深入的了解指针&#xff0c;那么废话不多说&#xff0c;我们正式进入今天的学习 1.对数组名的深入理解 在上一节的内容中&#xff0c;我们提到了用指针来访问数组的操作&#xff0c…

Linux 基于 TCP 协议的简单服务器-客户端应用

目录 一、相关函数 1、listen() 2、accept() 3、connect() 4、两种IP地址转换方式 5、TCP和UDP数据发送和接收函数对比 5、log.hpp自定义记录日志 二、udp_server.hpp单进程版本 三、tcp_server.cc 四、Telnet客户端&#xff08;代替tcp_client.cc&#xff09; 五…

ColBERT和ColBERTv2:兼具Bi-encoder和cross-encoder优势的多向量排序模型

文章目录 简介ColBERTColBert 原理ColBERT如何训练ColBERT 如何使用离线索引用ColBERT 实现top-k Re-ranking用ColBERT 实现top-k 端到端的检索 ColBERTv2ColBERTv2原理SupervisionRepresentation IndexingRetrieval 总结参考资料 简介 ColBERT是一种多向量排序模型&#xff0…

数据分析案例-中国黄金股票市场的EDA与价格预测

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

Redis的Stream 和 实现队列的方式【List、SortedSet、发布订阅、Stream、Java】

Redis队列与Stream、Redis 6多线程详解 Redis队列与StreamStream总述常用操作命令生产端消费端单消费者消费组消息消费 Redis队列几种实现的总结基于List的 LPUSHBRPOP 的实现基于Sorted-Set的实现PUB/SUB&#xff0c;订阅/发布模式基于Stream类型的实现与Java的集成 消息队列问…

算法打卡day39

今日任务&#xff1a; 1&#xff09;卡码网57. 爬楼梯&#xff08;70. 爬楼梯进阶版&#xff09; 2&#xff09;322.零钱兑换 3&#xff09;279.完全平方数 4&#xff09;复习day14 卡码网57. 爬楼梯&#xff08;70. 爬楼梯进阶版&#xff09; 题目链接&#xff1a;57. 爬楼梯…

ipv4Bypass:一款基于IPv6实现的IPv4安全绕过与渗透测试工具

关于ipv4Bypass ipv4Bypass是一款基于IPv6实现的安全绕过与渗透测试工具&#xff0c;该工具专为红队研究人员设计&#xff0c;可以帮助广大研究人员通过IPv6绕过目标安全策略&#xff0c;以此来检测安全检测机制的健壮性。 20世纪90年代是互联网爆炸性发展时期&#xff0c;随着…

Llama 3王者归来,可与GPT-4分庭抗礼,开源模型即将追上闭源模型了?

“有史以来最强大的开源大模型”Llama 3引爆AI圈&#xff0c;马斯克点赞&#xff0c;英伟达高级科学家Jim Fan直言&#xff0c;Llama 3将成为AI大模型发展历程的“分水岭”&#xff0c;AI顶尖专家吴恩达称Llama3是他收到的最好的礼物。 4月18日&#xff0c;AI圈再迎重磅消息&a…

写一个uniapp的登录注册页面

目录 一、效果图 二、代码 1、登录 &#xff08;1&#xff09;页面布局代码 &#xff08;2&#xff09;逻辑实现代码 &#xff08;3&#xff09;css样式 2、注册 &#xff08;1&#xff09;页面布局代码 &#xff08;2&#xff09;逻辑实现代码 &#xff08;3&#x…

一个完全用rust写的开源操作系统-Starry

1. Starry Starry是2023年全国大学生计算机系统能力大赛操作系统设计赛-内核实现赛的二等奖作品。Starry是在组件化OS的arceos的基础上&#xff0c;进行二次开发的操作系统内核&#xff0c;使用宏内核架构&#xff0c;能够运行Linux应用的内核。 原始的操作系统大赛的仓库为 …

51-42 NÜWA:女娲,统一的多模态预训练模型

21年11月&#xff0c;微软、北大联合发布了NUWA模型&#xff0c;一个统一的多模态预训练模型&#xff0c;在 8 个下游任务上效果惊艳。目前该项目已经发展成为一系列工作&#xff0c;而且都公开了源代码。 Abstract 本文提出了一种统一的多模态预训练模型N̈UWA&#xff0c;该…

【精简改造版】大型多人在线游戏BrowserQuest服务器Golang框架解析(1)——功能清单

1.匿名登录 2.服务连接 3.新手引导 4.随机出生点 5.界面布局 6.玩法帮助 7.NPC会话 8.成就系统 9.成就达成 10.用户聊天 11.战斗&信息展示 12.药水使用 13.副本传送 14.玩家死亡 15.超时断开

实验:使用FTP+yum实现自制yum仓库

实验准备 FTP服务器端&#xff1a;centos-1&#xff08;IP:10.9.25.33&#xff09; 客户端&#xff1a;centos-2 两台机器保证网络畅通&#xff0c;原yum仓库可用&#xff0c;已关闭防火墙和selinux FTP服务器端 ①安装vsftpd并运行&#xff0c;设定开机自启动 安装vsftpd…

金融数字化能力成熟度指引

1 范围 本文件提出了金融数字化能力成熟度模型、成熟度计算方法&#xff0c;明确了不同维度金融数字化转型能力 相应的分档要求。 本文件适用于金融机构衡量金融科技应用和数字化转型发展水平&#xff0c;检视自身数字化发展优势与短板&#xff0c; 加快数字化转型&#xff0c…

金蝶云星空和金蝶云星空单据接口对接

金蝶云星空和金蝶云星空单据接口对接 来源系统:金蝶云星空 金蝶K/3Cloud结合当今先进管理理论和数十万家国内客户最佳应用实践&#xff0c;面向事业部制、多地点、多工厂等运营协同与管控型企业及集团公司&#xff0c;提供一个通用的ERP服务平台。K/3Cloud支持的协同应用包括但…

Linux Makefile

1.开发背景 linux 下编译程序需要用到对应的 Makefile&#xff0c;用于编译应用程序。 2.开发需求 编写 Makefile 编译应用程序 1&#xff09;支持多个源文件 2&#xff09;支持多个头文件 3&#xff09;支持只编译修改的文件&#xff0c;包括源文件和头文件 4&#xff09;支持…

Web程序设计-实验03 JavaScript语言基础

题目 【实验主题】 素数问题求解。计算&#xff08;判断&#xff09; 1~100中哪些是素数、哪些是合数。 素数也称为质数&#xff0c;是只能被1及其自身整除的自然数。与素数相对应的是合数&#xff0c;合数可以被分解为若干个素数的乘积&#xff0c;这些素数称为这个合数的质…

数据结构从入门到实战——顺序表的应用

目录 一、基于动态顺序表实现通讯录 二、代码实现 2.1 通讯录的初始化 2.2 通讯录的销毁 2.3 通讯录的展示 2.4 通讯录添加联系人信息 2.5 通讯录删除联系人信息 2.6 通讯录修改联系人信息 2.7 通讯录的查找联系人信息 2.8 将通讯录中联系人信息保存到文件中 2.9…