search
HomeWeb Front-endCSS TutorialClip implements ring progress bar in css3

Clip implements ring progress bar in css3

Feb 09, 2018 am 11:14 AM
clipcsscss3

This article mainly introduces to you the relevant information about the sample code of css3 clip to implement the ring progress bar. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

There is a property in CSS called clip, which means trimming.

The clip property clips absolutely positioned elements. This property is used to define a clipping rectangle. For an absolutely defined element, only the content within the rectangle is visible. Content outside this clipping area will be processed according to the value of overflow.

Ring progress bar.gif

How to achieve the effect of such a ring progress bar, you can use canvas, svg, GIF, etc., today Let’s talk about how to implement it using css3.

Implementation ideas

The circle is very simple. One line of cssborder-radius:50% can be realized, and there is no compatibility problem. What, you said IE, let it Get out...

We need three rings here, one full and two half. I roughly drew the picture below

I used clip to cut the semicircle here. The main code is as follows,


.left{
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 10px solid lightblue;
    position:absolute;
    top: -10px;   /* 10的原因是因为边框是10个像素 */
    right: -10px;
    clip: rect(0 100px 200px 0);  /* 上面为0 右边到100px 下面200px 左边到0 这个区域的我们裁剪出来 */ 
}

The right side is similar except that the cropping position has been changed


.right{
    width: 200px;
    height: 200px;
    border-radius: 50%;
    border: 10px solid lightblue;
    position:absolute;
    top: -10px;  /* 10的原因是因为边框是10个像素 */
    right: -10px;
    clip: rect(0 200px 200px 100px);  /* 位置更改,计算可以参考上图 */ 
}

Complete code


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        p{
            box-sizing: border-box;
        }
        .box{
            width: 200px;
            height: 200px;
            position: relative;
            background-color: #ccc;
            border-radius: 50%;
            left: 40%;
            top: 200px;

        }
        .num{
            position: absolute;
            top: 50%;
            left: 50%;
            background: #fff;
            border-radius: 50%;
            width: 180px; 
            height: 180px;
            transform: translate(-50%, -50%);
            text-align: center;
            line-height: 180px;
            font-size: 32px;
        }
        
        
        .clip{
            width: 200px;
            height: 200px;
            position: absolute;
            border: 10px solid #ccc;
            border-radius: 50%;
            clip: rect(0, 200px, 200px, 100px);
        }
        .left{
            width: 200px;
            height: 200px;
            position: absolute;
            border: 10px solid lightblue;
            border-radius: 50%;
            clip: rect(0 100px 200px 0);
            top: -10px;
            left: -10px;
        }
        .right{
            width: 200px;
            height: 200px;
            position: absolute;
            border: 10px solid lightblue;
            border-radius: 50%;
            clip: rect(0 200px 200px 100px);
            top: -10px;
            left: -10px;
        }
        .width-none{
            width: 0;
        }
        .auto{
            clip: auto;
        }
    </style>
</head>
<body>
    <p class="box">
        <p class="clip">
            <p class="left"></p>
            <p class="right width-none"></p>
        </p>
        <p class="num">

        </p>
    </p>
    <script >
        let clip = document.querySelector(&#39;.clip&#39;),
        left = document.querySelector(&#39;.left&#39;),
        right = document.querySelector(&#39;.right&#39;),
        num = document.querySelector(&#39;.num&#39;),
        rotate = 0;
    
        let loop = setInterval(() => {
            if(rotate >= 100){
                rotate = 0;
                right.classList.add(&#39;width-none&#39;);
                clip.classList.remove(&#39;auto&#39;);
            } else if(rotate > 50){
                right.classList.remove(&#39;width-none&#39;);
                clip.classList.add(&#39;auto&#39;);
            }
            rotate++;
            left.style.transform = &#39;rotate(&#39;+ 3.6*rotate + &#39;deg)&#39;;
            num.innerHTML = `${rotate}%`
        },100)

    </script>
</body>
</html>

Let’s briefly talk about the above Code

1. First, the right semicircle is hidden. This is because what we need to rotate is the left semicircle. We can wait for the left semicircle to move to the position of the right circle and then display the right side, that is, wait until it is rotated to 180 degrees. when.

2. At the same time, we see that the clip: rect(0, 200px, 200px, 100px); cropping style is added to the main circle. This is because by default our progress is 0%, and we can only hide it if we display the right side. On the left, but isn't our right hidden? Then what does it show? Because when you rotate it to the left, you can see the circle that turns to the right. It's a bit convoluted, please combine it with the code and understand it more

3. When the left side is rotated 180 degrees, we need to display the right side, and set the cropping of the box element to the default value, which is no cropping, so that it can be displayed completely. The two circles on the left and right.

4. Finally, we use js to control the rotation angle and display the percentage on the page

Write at the end

If you can’t see the above explanation Understand, just don't read it, put the code in local debugging, and understand it yourself.

Don’t be too impatient, code is the best language.

Related recommendations:

css3 method of realizing circular progress bar

Detailed explanation of canvas to realize arc and circular progress bar Example method

Using CSS clip to implement audio playback ring progress bar tutorial example

The above is the detailed content of Clip implements ring progress bar in css3. For more information, please follow other related articles on the PHP Chinese website!

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
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

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 Tools

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools