JavaScript完美运动框架

完美运动框架

多物体运动,多属性同步运动,可进行透明度运动

        function getStyle(obj,name){
            if(obj.currentStyle){
                return obj.currentStyle[name];
            }
            else{
                return getComputedStyle(obj,1)[name];
            }
        }
		/*json格式,startMove(oDiv,{'width':300,'height':300,'opacity':100},
		function(){
                    alert('完成');
                });*/
        function startMove(obj,json,func){
            clearInterval(obj.timer);
            obj.timer=setInterval(function(){
                var isStop=true;
                var cur=0;
                for(var attr in json){
                    if(attr=='opacity'){
                        cur=parseFloat(getStyle(obj,attr))*100;
                    }
                    else{
                        cur=parseInt(getStyle(obj,attr));
                    }
                    if(cur!=json[attr]){
                        isStop=false;
                    }
                    var speed=(json[attr]-cur)/6;
                    speed=speed>0?Math.ceil(speed):Math.floor(speed);
                    if(attr=='opacity'){
                        obj.style.opacity=(cur+speed)/100;
                    }
                    else{
                        obj.style[attr]=cur+speed+'px';
                    }
                }
                if(isStop){
                    clearInterval(obj.timer);
                    if(func){
                        func();
                    }
                }
            },30);
        }

网络照片翻看模板(源码)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{margin: 0; padding: 0;}
        img{position: absolute;}
        ul{margin: 0; padding: 0;}
        li{list-style: none;}
        #main{position: relative; width: 400px; height: 550px; margin: 50px auto; background: #ccc;} 
        .big_pic{width: 400px; height: 400px; position: absolute; overflow: hidden;}
        .big_pic li{
            width: 400px; height: 400px; position: absolute;
            z-index: 0;overflow: hidden;
        }
        .mark_left{
            width: 200px;height: 400px; position: absolute;
            top: 0;left: 0;background: red;opacity: 0;
            z-index: 3000;
        } 
        .mark_right{
            width: 200px;height: 400px;position: absolute;
            top: 0;right: 0;background: green;opacity: 0;
            z-index: 3000;
        }
        .prev{
            width: 60px;height: 60px;position: absolute;
            top: 160px;left: 10px;
            background: url(pic/左箭头.jpg) no-repeat center center;
            opacity: 0; z-index: 3001;cursor: pointer;
        }
        .next{
            width: 60px;height: 60px;position: absolute;
            top: 160px; right: 10px;
            background: url(pic/右箭头.jpg);
            opacity: 0;z-index: 3001;cursor: pointer;
        }
        .text{
            width: 200px;height: 20px;position: absolute;
            top: 400px;left: 0;z-index: 3000;
        }
        .length{
            width: 200px;height: 20px;position: absolute;
            top: 400px;right: 0;z-index: 3000;text-align: right;
        }
        .bg{
            width: 400px;height: 20px;
            position: absolute;background: red;
            top: 400px;left: 0;z-index: 2999;
        }
        .small_pic{
            width: 390px;height: 130px;position: relative;
            background:#ccc;
            top: 420px;left: 5px;
            overflow: hidden;
        }
        .small_pic ul{
            position:absolute;
        }
        .small_pic li{
            width: 130px;height: 130px;float: left;opacity: 0.6;
        }
    </style>
    <script>
        window.onload=function(){
            var oDiv=document.getElementById('main');
            var oBtnPrev=getByClass(oDiv,'prev')[0];
            var oBtnNext=getByClass(oDiv,'next')[0];
            var oMarkLeft=getByClass(oDiv,'mark_left')[0];
            var oMarkRight=getByClass(oDiv,'mark_right')[0];
            var oDivSmall=getByClass(oDiv,'small_pic')[0];
            var oUlSmall=oDivSmall.getElementsByTagName('ul')[0];
            var aLiSmall=oDivSmall.getElementsByTagName('li');
            var oUlBig=getByClass(oDiv,'big_pic')[0];
            var aLiBig=oUlBig.getElementsByTagName('li');
            var nowIndex=2;
            var now=0;
            oUlSmall.style.width=aLiSmall.length*aLiSmall[0].offsetWidth+'px';
            //左右切换
            oBtnPrev.onmouseover=oMarkLeft.onmouseover=function(){
                startMove(oBtnPrev,'opacity',100);
            };
            oBtnPrev.onmouseout=oMarkLeft.onmouseout=function(){
                startMove(oBtnPrev,'opacity',0);
            };
            oBtnNext.onmouseover=oMarkRight.onmouseover=function(){
                startMove(oBtnNext,'opacity',100);
            };
            oBtnNext.onmouseout=oMarkRight.onmouseout=function(){
                startMove(oBtnNext,'opacity',0);
            };

            //大图切换
            for(var i=0;i<aLiSmall.length;i++){
                aLiSmall[i].index=i;
                aLiSmall[i].onclick=function(){
                    if(this.index==now)return;
                    now=this.index;
                    tab();
                };
            function tab(){
                aLiBig[now].style.zIndex=nowIndex++;
                for(var i=0;i<aLiSmall.length;i++){
                    startMove(aLiSmall[i],'opacity',60);
                }
                startMove(aLiSmall[now],'opacity',100);
                aLiBig[now].style.height=0;
                startMove(aLiBig[now],'height',400);
                if(now==0){
                    startMove(oUlSmall,'left',0);
                }
                else if(now==aLiSmall.length-1){
                    startMove(oUlSmall,'left',-(now-2)*aLiSmall[0].offsetWidth);
                }
                else{
                startMove(oUlSmall,'left',-(now-1)*aLiSmall[0].offsetWidth);
                }
            }  
                aLiSmall[i].onmouseover=function(){
                    startMove(this,'opacity',100);
                };
                aLiSmall[i].onmouseout=function(){
                    if(this.index!=now)
                    startMove(this,'opacity',60);
                };
            //左右按钮
            oBtnPrev.onclick=function(){
                now--;
                if(now==-1){now=aLiSmall.length-1;}
                tab();
            };
            oBtnNext.onclick=function(){
                now++;
                if(now==aLiSmall.length){now=0;}
                tab();
            }
            }
        };
        function getByClass(oParent,sClass){
            var aEle=oParent.getElementsByTagName('*');
            var aResult=[];
            for(var i=0;i<aEle.length;i++){
                if(aEle[i].className==sClass){
                    aEle[i],timer=null;
                    aResult.push(aEle[i]);
                }
            }
            return aResult;
        }

        function getStyle(obj,name){
            if(obj.currentStyle){
                return obj.currentStyle[name];
            }
            else{
                return getComputedStyle(obj,1)[name];
            }
        }

        function startMove(obj,attr,iTarget){
            clearInterval(obj.timer);
            obj.timer=setInterval(function(){
                var cur=0;
                if(attr=='opacity'){
                    cur=parseFloat(getStyle(obj,attr))*100;
                }
                else{
                    cur=parseInt(getStyle(obj,attr));
                }
                var speed=(iTarget-cur)/6;
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                if(iTarget==cur){
                    clearInterval(obj.timer);
                }
                else{
                    if(attr=='opacity'){
                        obj.style.opacity=(cur+speed)/100;
                    }
                    else{
                    obj.style[attr]=cur+speed+'px';
                }
            }
            },30);
        }
    </script>
</head>
<body>
    <div id="main" class="pic">
        <ul class="big_pic">
            <div class="prev"></div>
            <div class="next"></div>
            <a href="#" class="mark_left"></a>
            <a href="#" class="mark_right"></a>
            <li style="z-index: 1;"><img src="pic/A.jpg"></li>
            <li ><img src="pic/B.jpg"></li>
            <li><img src="pic/C.jpg"></li>
            <li><img src="pic/D.jpg"></li>
            <li><img src="pic/E.jpg"></li>
            <li><img src="pic/F.jpg"></li>
        </ul>
        <div class="text">加载图片说明...</div>
        <div class="length">计算图片数量...</div>
        <div class="bg"></div>
        <div class="small_pic">
            <ul style="width: 390px;">
                <li style="opacity: 1;"><img src="pic/11.jpg"></li>
                <li><img src="pic/12.jpg"></li>
                <li><img src="pic/13.jpg"></li>
                <li><img src="pic/14.jpg"></li>
                <li><img src="pic/15.jpg"></li>
                <li><img src="pic/16.jpg"></li>
            </ul>
        </div>
    </div>
</body>
</html>

上述程序的演示实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值