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 onload="init();">
      <h1><input type="button" value="从左到右装载图片"  onclick="startLeftToRight();"/></h1>
      <h1><input type="button" value=“从中间到两边加载图片"  onclick="startCenterToOuter();"/></h1>
      <h1><input type="button" value="百叶窗方式装载图片"  onclick="startBaiyechuang();"/></h1>
       
      <div>
          <canvas id="canvas1" width="400px" height="400px"></canvas>
      </div>
  </body>
</html>

目录
相关文章
|
4月前
|
人工智能 运维 IDE
阿里云百炼Coding Plan全解析 套餐规则、模型适配与工具接入实操教程
在AI深度融入软件开发、智能体开发的当下,各类代码助手、AI编程Agent已经成为开发者日常工作的标配。但传统大模型按量计费模式存在明显短板,调用量突增时账单无法预估,长期高频使用的综合成本居高不下,同时部分接口还存在限流、访问不稳定等问题。为解决这一行业痛点,阿里云百炼推出**Coding Plan**专属AI编程订阅套餐,主打固定月费、定额调用额度、多模型聚合、全工具兼容的核心特性,专门面向个人开发者、研发团队、智能体开发者打造高性价比解决方案。
838 1
|
安全 网络协议 数据安全/隐私保护
|
Dubbo 应用服务中间件
|
SQL Oracle 关系型数据库