运动:多图片展开、无缝滚动、无限运动、分步运动

1.运动框架封装

//json:{width:400,heigth:500}
//easing:匀速  加速  缓冲
// linear  ease-in  ease-out
/*
opations={
 duration:1000,
 easing:'ease-out',
 complete:function(){}
}

 */
//获取计算后的样式(兼容所有浏览器)
function getStyle(obj,sName){
    return (obj.currentStyle||getComputedStyle(obj,false))[sName];
}

////运动
function move(obj,json,options){
    var options=options||{};
    options.duration=options.duration||700;
    options.easing=options.easing||'ease-out';
    var start={};
    var dis={};
    for(var name in json){
        start[name]=parseInt(getStyle(obj,name));
        dis[name]=json[name]-start[name];
    }
    var count=Math.floor(options.duration/30);
    var n=0;
    
    clearInterval(obj.timer);
    obj.timer=setInterval(function (){
        n++;
        
        for(var name in json){
            switch(options.easing){
                case 'linear':
                    var cur=start[name]+dis[name]*n/count;
                    break;
                case 'ease-in':
                    var a=n/count;
                    var cur=start[name]+dis[name]*Math.pow(a,3);
                    break;
                case 'ease-out':
                    var a=1-n/count;
                    var cur=start[name]+dis[name]*(1-Math.pow(a,3));
                    break;
            }
            if(name=='opacity'){
                obj.style.filter='alpha(opacity:'+cur*100+')';
                obj.style.opacity=cur;
            }else{
                obj.style[name]=cur+'px';
            }
        }
        
        if(n==count){
            clearInterval(obj.timer);
        }
    },16);
}

2.多图片展开:

    1、布局转化
        将浮动布局转化成定位布局
    2、鼠标移入修改每个li 的left/top
    3、鼠标移出修改每个li 的left/top为 数组中的值
实例:
<style>

*{margin:0; padding:0; list-style: none;}

ul{

width: 366px;

margin: 100px auto;

}

li{

width: 100px;

height: 100px;

border: 1px solid #000;

float: left;

margin: 10px;

}

img{

width: 100%;

height:100%;

}

</style>

<script src="js/move.js"></script>

<script>

window.onload = function(){

var aLi = document.getElementsByTagName('li');

//布局转化

var arrPos = [];

for(var i = 0;i < aLi.length; i++){

arrPos[i] = {

left:aLi[i].offsetLeft,

top:aLi[i].offsetTop

};

}

for(var i = 0; i<aLi.length; i++){

aLi[i].style.position = 'absolute';

aLi[i].style.left = arrPos[i].left + 'px';

aLi[i].style.top = arrPos[i].top + 'px';

aLi[i].style.margin = 0;

}

for(var i = 0; i < aLi.length; i++){

aLi[i].index = i;

aLi[i].onmouseover = function(){

/*this.style.width = '200px';

this.style.height = '200px';

this.style.left = arrPos[this.index].left - 50 + 'px';

this.style.top = arrPos[this.index].top - 50 + 'px';*/

this.style.zIndex = 2;

move(this,{width:200,height:200,left:arrPos[this.index].left - 50,top:arrPos[this.index].top - 50});

};

aLi[i].onmouseout = function(){

/*this.style.width = '100px';

this.style.height = '100px';

this.style.left = arrPos[this.index].left + 'px';

this.style.top = arrPos[this.index].top+ 'px';*/

this.style.zIndex = 1;

move(this,{width:100,height:100,left:arrPos[this.index].left,top:arrPos[this.index].top});

};

}

};

</script>

</head>

<body>

<ul>

<li><img src="img/1.jpg" alt=""></li>

<li><img src="img/2.jpg" alt=""></li>

<li><img src="img/3.jpg" alt=""></li>

<li><img src="img/4.jpg" alt=""></li>

<li><img src="img/5.jpg" alt=""></li>

<li><img src="img/6.jpg" alt=""></li>

<li><img src="img/7.jpg" alt=""></li>

<li><img src="img/8.jpg" alt=""></li>

<li><img src="img/9.jpg" alt=""></li>

</ul>

</body>

多图片展开效果

多图片展开.png

3.无缝滚动

    1)复制一份接到后面
    2)修改UL 的left值
        left--  ->左
        left++  ->右
    3) 左:
        if(left < -oUl.offsetWidth/2){
                    left = 0;
                }
    4)右:
        if(left >= 0){
                    left = -oUl.offsetWidth/2;
                }
DOM  DOM树
    页面上的元素、标签
操作DOM元素,是一件非常耗性能的事情

数字规律:
向左滚动: left
left <= 0
left        %400        实际值
0       0           0
-100        -100            -100
-200        -200            -200
-300        -300            -300
-400        0           0
-500        -100            -100
-600        -200            -200

向右滚动:
left>=0

left      %400       -400       %400           实际值
0         0     -400        0           0
100     100     -300        -300            -300
200     200     -200        -200            -200
300     300     -100        -100            -100
400     0         -400      0               0
500     100     -300        -300            -300


实例:
<style>

*{margin:0;padding:0;list-style:none;}

#box{

width: 572px;

height: 200px;

border: 1px solid #000;

overflow: hidden;

margin: 100px auto;

position: relative;

}

#box ul{

position: absolute;

left:0;

top:0;

}

#box ul li{

width: 133px;

height: 200px;

padding: 0 5px;

float: left;

}

#lnkLeft{

width: 50%;

height: 100%;

background: red;

position: absolute;

top:0;

left:0;

z-index: 1000;

opacity: 0;

}

#lnkRight{

width: 50%;

height: 100%;

background: blue;

position: absolute;

top:0;

right:0;

z-index: 1000;

opacity: 0;

}

</style>

<script>

window.onload = function(){

var oBox = document.getElementById('box');

var oUl = document.getElementById('ul1');

var aLi = oUl.children;

var oLeft = document.getElementById('lnkLeft');

var oRight = document.getElementById('lnkRight');

oUl.innerHTML += oUl.innerHTML;

oUl.style.width = aLi.length * aLi[0].offsetWidth + 'px';

var timer = null;

var W = oUl.offsetWidth / 2;

var left = 0;

function toLeft(){

clearInterval(timer);

timer=setInterval(function(){

left -= 2;

if(left < 0){

oUl.style.left = left % W + 'px';

}

else{

oUl.style.left = (left%W-W)%W + 'px';

}

document.title = left + '--' + oUl.style.left;

},30);

}

function toRight(){

clearInterval(timer);

timer=setInterval(function(){

left += 2;

if(left < 0){

oUl.style.left = left % W + 'px';

}

else{

oUl.style.left = (left%W-W)%W + 'px';

}

document.title = left + '--' + oUl.style.left;

},30);

}

oLeft.onmouseover = function(){

toLeft();

};

oRight.onmouseover = function(){

toRight();

};

toLeft();

};

</script>

</head>

<body>

<div id="box">

<a href="javascript:;" id="lnkLeft">←</a>

<a href="javascript:;" id="lnkRight">→</a>

<ul id="ul1">

<li><img src="images/1.jpg" alt=""></li>

<li><img src="images/2.jpg" alt=""></li>

<li><img src="images/3.jpg" alt=""></li>

<li><img src="images/4.jpg" alt=""></li>

</ul>

</div>

</body>

无缝滚动效果:

无缝滚动.png

4.无限运动:

无限运动:
    一直运动下去,不停止
    原理:递归调用
<style>

*{margin:0; padding:0;}

#div1{

width: 100px;

height: 100px;

background: blue;

position: absolute;

left:0;

top:0;

}

</style>

<script src="js/move.js"></script>

<script>

window.onload = function(){

var oDiv = document.getElementById('div1');

var arrPos=[

{left:0,top:0},

{left:700,top:0},

{left:200,top:200},

{left:700,top:400},

{left:0,top:400},

];

var iNow = 0;

function next(){

move(oDiv,arrPos[iNow%arrPos.length],{duration:1000,complete:function(){

iNow++;

next();

}});

}

next();

/*for(var i = 0; i < 2; i++){

setTimeout(function () {

alert(i);

},0)

}*/

};

</script>

</head>

<body>

<div id="div1"></div>

</body>
无限运动.png

5.分步运动:

分步运动:
    一个接一个出现
    原理:定时器里加运动
打字效果:
<style>

*{margin:0; padding:0}

body{

background: #000;

}

span{

font-family: 微软雅黑;

font-size: 30px;

opacity: 0;

color: #fff;

}

</style>

<script src="js/move.js"></script>

<script>

window.onload = function(){

var str = '自1971年建交以来,中秘关系';

for(var i = 0; i < str.length; i++){

var oSpn = document.createElement('span');

oSpn.innerHTML = str.charAt(i);

document.body.appendChild(oSpn);

}

var aSpan = document.getElementsByTagName('span');

var timer = null;

var iNow = 0;

timer = setInterval(function(){

move(aSpan[iNow],{opacity:1});

iNow++;

if(iNow>=aSpan.length){

clearInterval(timer);

}

},100);

};

</script>

</head>

<body>

</body>

分布运动效果:


分布运动.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容