search
HomeWeb Front-endH5 TutorialHTML5实现的Loading缓冲效果

       HTML5在移动设备上表现,相信已经不用我多说了,干掉了Flash之后,它已经坐上了移动应用程序的第一把交椅。几乎所有稍微高端一点的设备(乔帮主的iPad,iPhone和Andriod的平板手机等)的浏览器都支持HTML5,而且据权威人士测试,这些支持HTML5的设备对Canvas标签的支持也是相当的好。

        大家都知道Web2.0以来,应用程序的实现使用了大量Ajax,而Loading的小图标也有很多,甚至还有专门提供Loading图片的网站,所以我就想能不能让HTML5一并解决这个以前用gif文件才能解决的问题。出乎我意料的是,实现的过程非常简单,只用了不到一小时的时间我就用HTML5实验出了两个Loading效果,而且这样做出来的Loading图标是可定制的,既可以定制颜色,也可以定制大小等属性。

        第一个带着小尾巴转动的loading图标画图的思路是,首先画一个圆,然后在圆的边上按顺序画大小逐渐减小的小圆点,在每次刷新画布时改变这一系列的小圆点在大圆边上的位置。

        这里是案例的演示代码:



  1.   
  2.    
  3.    loading
  4.    
  5.     function loading(canvas,options){
  6.       this.canvas = canvas;
  7.       if(options){
  8.         this.radius = options.radius||12;
  9.         this.circleLineWidth = options.circleLineWidth||4;
  10.         this.circleColor = options.circleColor||'lightgray';
  11.         this.dotColor = options.dotColor||'gray';
  12.       }else{
  13.         this.radius = 12;
  14.         this.circelLineWidth = 4;
  15.         this.circleColor = 'lightgray';
  16.         this.dotColor = 'gray';
  17.       }
  18.     }
  19.     loading.prototype = {
  20.       show:function (){
  21.         var canvas = this.canvas;
  22.         if(!canvas.getContext)return;
  23.         if(canvas.__loading)return;
  24.         canvas.__loading = this;
  25.         var ctx = canvas.getContext('2d');
  26.         var radius = this.radius;
  27.         var rotators = [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];
  28.         var me = this;
  29.         canvas.loadingInterval = setInterval(function(){
  30.           ctx.clearRect(0,0,canvas.width,canvas.height);
  31.           var lineWidth = me.circleLineWidth;
  32.           var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};
  33.           ctx.beginPath();
  34.           ctx.lineWidth = lineWidth;
  35.           ctx.strokeStyle = me.circleColor;
  36.           ctx.arc(center.x,center.y,radius,0,Math.PI*2);
  37.           ctx.closePath();
  38.           ctx.stroke();
  39.           for(var i=0;i
  40.             var rotatorAngle = rotators[i].currentAngle||rotators[i].angle;
  41.             //在圆圈上面画小圆
  42.             var rotatorCenter = {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};
  43.             var rotatorRadius = rotators[i].radius;
  44.             ctx.beginPath();
  45.             ctx.fillStyle = me.dotColor;
  46.             ctx.arc(rotatorCenter.x,rotatorCenter.y,rotatorRadius,0,Math.PI*2);
  47.             ctx.closePath();
  48.             ctx.fill();
  49.             rotators[i].currentAngle = rotatorAngle+4/radius;
  50.           }
  51.         },50);
  52.       },
  53.       hide:function(){
  54.         var canvas = this.canvas;
  55.         canvas.__loading = false;
  56.         if(canvas.loadingInterval){
  57.           window.clearInterval(canvas.loadingInterval);
  58.         }
  59.         var ctx = canvas.getContext('2d');
  60.         if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);
  61.       }
  62.     };
  63.    
  64.   
  65.   
  66.    
  67.    


  68.    
  69.    

  70.    


  71.     <script><br> </script>
  72.     var loadingObj = new loading(document.getElementById('canvas'),{radius:8,circleLineWidth:3});
  73.     loadingObj.show();
  74.    
  75.   
复制代码


        演示地址:http://f200-8.bbs.hexun.com/e/111219/loading.htm

        第二个较为简单,在一个圆环上有一个相同圆心相同半径的圆弧在不停的转动。画图的步骤是首先画一个圆环,然后画一个不同颜色相同圆心半径的圆弧,在每次刷新画布时改变圆弧的起始角度。

        这里是案例的演示代码:




  1.   
  2.   loading
  3.   <script><br> </script>
  4.    /*
  5.     html5 loading 鎺т欢
  6.     浣滆€咃細鐜夊紑 鍗氬
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor