js实现图片加载特效(从左到右,百叶窗,从中间到两边)

/*
  网上百度的,感觉”从中间到两边“的效果写的不是很好,改了一下,感觉可以了!
*/
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        var image;
        var intervalId1;
        var intervalId2;
        var intervalId3;
        var drawW1 = 0;
        var canvas;
        var context;
        var drawLeft;
 
        var baiyechuangItemW ;
        function init(){
            image = new Image();
            image.src="eg_mouse.jpg";
            canvas = document.getElementById("canvas1");
            context = canvas.getContext("2d");
            drawLeft = image.width/2;
        }
         
        function startLeftToRight(){
            clearInterval(intervalId1);
            context.clearRect(0,0,image.width,image.height);
            intervalId1 = setInterval("leftToRight();",50);
        }
         
        function leftToRight(){
            context.drawImage(image,0,0,drawW1, image.height,0,0,drawW1,image.height);
            drawW1 += 2;
            if(drawW1>image.width){
                drawW1 =0;
                clearInterval(intervalId1);
            }
        }
          
        function centerToOuter(){
            drawW1 += 2;
       //drawLeft-drawW1/2是左端点,drawLeft是中间, drawLeft+drawW1/2是右端点
            context.drawImage(image,drawLeft-drawW1/2,0,drawW1,image.height,drawLeft-drawW1/2,0,drawW1,image.height);
            if(drawW1>drawLeft*2){
                drawW1 = 0;
                clearInterval(intervalId2);
            }
        }
         
        function startCenterToOuter(){
            clearInterval(intervalId2);
            context.clearRect(0,0,image.width,image.height);
            intervalId2 = setInterval("centerToOuter();",50);
        }
 
        function baiyechuang(){
            for(i=0;i<10;i++){
                context.drawImage(image,baiyechuangItemW*i,0,drawW1,image.height,baiyechuangItemW*i,0,drawW1,image.height);
            }
            drawW1 += 0.2;
            if(drawW1>baiyechuangItemW){
                clearInterval(intervalId3);
            }
        }
         
        function startBaiyechuang(){
            drawW1=0;
            baiyechuangItemW = image.width/10.0;
            clearInterval(intervalId3);
            context.clearRect(0,0,image.width,image.height);
            intervalId3 = setInterval("baiyechuang();", 50);
        }
         
    </script>
  </head>
  <body οnlοad="init();">
      <h1><input type="button" value="从左到右装载图片"  οnclick="startLeftToRight();"/></h1>
      <h1><input type="button" value=“从中间到两边加载图片"  οnclick="startCenterToOuter();"/></h1>
      <h1><input type="button" value="百叶窗方式装载图片"  οnclick="startBaiyechuang();"/></h1>
       
      <div>
          <canvas id="canvas1" width="400px" height="400px"></canvas>
      </div>
  </body>
</html>









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/4106523.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值