search
HomeWeb Front-endHTML Tutorial移动端圆环进度动画方案(透明背景-透明度圆环-css3版)_html/css_WEB-ITnose

适逢元旦假期,妹子逛街吃火锅看电影陪女朋友之际,像风一样的写完了这篇文章!文章画风略微粗糙,但是这个动画的实现还是很费了一番脑子的,话说程序猿之间交流并不需要过多解释,上代码就搞定。

首先说一下我们这个要实现的圆环进度动画,有三个特点: 1、背景是透明的,所以用遮挡实现的方法就不用考虑了; 2、圆环颜色是有透明度的,所以用两个半圆环实现大于180度的圆环效果不能有叠加部分; 3、最好用纯css3简洁的实现。 然后要备注下重点: 我们的动画效果要应用在移动端,尤其是有些性能较差的安卓手机,所以一定要考虑性能问题。

先上效果图:

所以我们很快的出了我们被pass掉的第一版方案

: 对于不可用遮挡来实现的圆环动画效果,我们可以用两个半圆环的运动来组合,动画效果使用transition和transform实现。 圆环小于50%时:

圆环大于50%时,由两个不重叠的圆环组合而成:

所以对于大于50%的圆环旋转动画,是需要两段动画拼接的,左半边的圆环先旋转180度到右半边,右半边的圆环再旋转相应的度数至左半边,这里旋转角度不是固定的,需要根据具体进度确定,所以这种方案右半边的圆环旋转多少度是通过js赋值的,那么问题来了,当我们在pc上看到流畅的旋转动画时,在安卓手机上,两个半圆环动画的衔接处, 有时差!不流畅!

思考下第一版方案失败的根本原因,就是整个圆环进度是由两个半圆环分别动画形成的,右侧圆环的旋转角度不是固定的,使用transition实现需要通过js动态添加旋转角度样式,js语句的执行使得两个圆环执行动画的时间差无法确定,在性能较差的安卓手机上会明显感觉到两个动画直接的衔接卡顿。

那么换一种思维方式,我们有了第二版解决方案,利用纯css控制两个半圆环动画的执行,让两个半圆环动画都固定旋转180度,时间差固定,我们要控制的就是 可视区域的角度 。 这里只讨论角度大于50%的情况,因为小于50%的情况无论用哪种方案都能实现。 上面我们讨论过,右半边不可见区域的圆环旋转到左半边时,整个左半边都是可见区域,所以我们旋转180度后当然能看见完整的半圆环,那我们尝试着根据我们需要的角度缩小左半边的可见区域不就可以了。(最后一幅图太困了,改天再补!)

具体来讲,我们实现右半边可见圆环用了两层,实现左半边可见圆环用了三层,两个圆环从左到右、从右到左分别旋转180度,最后我们可以完整的看到整个右半侧圆环,而左半侧的圆环因为还有一层遮挡,只能看到我们所需进度的相应角度。

<div class="my-circle" id="my-circle">  <div class="right-outter">    <div class="right-inner"></div>  </div>  <div id="left-outter-patch" class="left-outter-patch">    <div id="left-outter" class="left-outter">      <div id="left-inner" class="left-inner"> </div>    </div>  </div></div>
.my-circle { position: relative; width: 58px; height: 58px; }    .right-outter { position: absolute; width: 29px; height: 58px; top: 0; right: 0; overflow: hidden; transform: rotate(180deg); -webkit-transform: rotate(180deg); }    .right-inner { position: absolute; top: 0; left: 0; width: 50px; height: 50px; border-radius: 50%; border: 4px solid rgba(113, 222, 130, 0.3); clip: rect(0 58px 58px 29px); }    .left-outter { position: absolute; width: 58px; height: 58px; top: 0; left: 0; overflow: hidden; clip: rect(0 58px 58px 29px); }    .left-inner { position: absolute; top: 0; left: 0; width: 50px; height: 50px; border-radius: 50%; border: 4px solid rgba(113, 222, 130, 0.3); clip: rect(0 58px 58px 29px); transform: rotate(180deg); -webkit-transform: rotate(180deg); }    .left-outter-patch { position: absolute; width: 58px; height: 58px; top: 0; left: 0; overflow: hidden; clip: rect(0 58px 58px 29px); }    .test-animation .right-inner { transform: rotate(180deg); -webkit-transform: rotate(180deg); transition: transform 1s linear; -webkit-transition: -webkit-transform 1s linear; }    .test-animation .left-inner { transform: rotate(360deg); -webkit-transform: rotate(360deg); transition: transform 1s linear 1s; -webkit-transition: -webkit-transform 1s linear 1s; }

所以动画是由css固定实现的,而左半侧的可视区域是由js代码控制的,这样无任何时差问题

    var degree = 75;    document.getElementById("left-outter-patch").style.transform = "rotate(" + ((degree-50)*360/100) + "deg)";    document.getElementById("left-outter-patch").style.WebkitTransform = "rotate(" + ((degree-50)*360/100) + "deg)";    document.getElementById("left-outter").style.transform  = "rotate(" + (180-(degree-50)*360/100) + "deg)";    document.getElementById("left-outter").style.WebkitTransform  = "rotate(" + (180-(degree-50)*360/100) + "deg)";    document.getElementById("my-circle").className = "my-circle test-animation";

戳这里查看源代码及实现效果

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
Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version