Amazing Pure CSS Scrolling Indicator Effect
Tags: css, javascript, html, web
Published: 2022-07-18 07:12:04

---
title: Amazing Pure CSS Scrolling Indicator Effect
published: true
description: Using Linear-Gradient Implement Scrolling Indicator Effect
tags: CSS, Javascript, HTML, Web
# cover_image: https://user-images.githubusercontent.com/8554143/50879370-a5cc2900-1415-11e9-89ba-40713de326f5.gif
# Use a ratio of 100:42 for best results.
---

An interesting question is how to implement the following scrollbar effect using CSS?

![](https://user-images.githubusercontent.com/8554143/50879370-a5cc2900-1415-11e9-89ba-40713de326f5.gif)

It is the yellow scrolling progress bar at the top, which changes in length as the page scrolls.

Before continuing to read below, you can take a moment. Try to think about the above effect or try it yourself, whether you can achieve the above effect skillfully without using JavaScript.

OK, this effect is a similar little problem I encountered in the process of my daily development. In fact, even if we can use Javascript, it feels very troublesome. So I’ve been wondering, is it possible to accomplish this effect using only CSS?

## Analyze requirements

The first time I saw this effect, I felt that this scrolling animation could not be accomplished with CSS alone, because it involved the calculation of the scrolling distance of the page.

If you want to do it only with CSS, you have to find another way and use some clever methods.

Well, let’s use CSS to achieve this effect step by step with the help of some tricks and tricks. Analyze the difficulty:

+ **How to know how far the user is currently scrolling the page and notify the top progress bar**?

Normal analysis should be like this, but this falls into traditional thinking. The progress bar is just the progress bar, receiving the scrolling distance of the page and changing the width. What if the page scrolling and the progress bar were a whole?

## Fulfill the requirement

Let’s not sell it, let’s use `linear-gradients` to achieve this function.

Assuming our page is wrapped `<body>` in , the entire body can be scrolled, and add this linear gradient from bottom left to top right to it:

``` CSS
body {
    background-image: linear-gradient(to right top, #ffcc00 50%, #eee 50%);
    background-repeat: no-repeat;
}
```

Then, we can get an effect like this:

![](https://user-images.githubusercontent.com/8554143/50879862-643c7d80-1417-11e9-8600-4133b0751626.gif)

Wow, the color change of the yellow block can actually express the overall progress. In fact, here, wise you should already know what to do next.

We use a pseudo-element to cover the extra part:

```CSS
body::after {
    content: "";
    position: fixed;
    top: 5px;
    left: 0;
    bottom: 0;
    right: 0;
    background: #fff;
    z-index: -1;
}
```

In order to facilitate the demonstration, I changed the white base above to a black transparent base:：

![](https://user-images.githubusercontent.com/8554143/50880203-871b6180-1418-11e9-8588-f0be26721fbe.gif)

The actual effect is as follows:

![](https://user-images.githubusercontent.com/8554143/50880270-d6619200-1418-11e9-97c7-601f7aca01e7.gif)

Those who are careful may notice that the progress bar does not end when the page scroll to the end.

![](https://user-images.githubusercontent.com/8554143/50880349-19236a00-1419-11e9-8a85-8d14ace2ac86.png)

The reason is that because bodythe height of the linear gradient sets the size of the entire `body`, let's adjust the height of the gradient:

``` CSS
body {
    background-image: linear-gradient(to right top, #ffcc00 50%, #eee 50%);
    background-size: 100% calc(100% - 100vh + 5px);
    background-repeat: no-repeat;
}
```

Here, the operation `calc` is performed , and subtraction `100vh`, that is, the height of a screen is subtracted, so that the gradient just fits the upper right corner when it slides to the bottom.

And `+ 5px` is the height of the scrolling progress bar, reserved for `5px` the height of . Take a look at the effect, perfect:

![](https://user-images.githubusercontent.com/8554143/50879370-a5cc2900-1415-11e9-89ba-40713de326f5.gif)

So far, this requirement has been fulfilled perfectly, which is a good trick, complete Demo:

[CodePen Demo -- Using Linear-Gradient Implement Scrolling Indicator Effect](https://codepen.io/Chokcoco/pen/KbBXQM?editors=1100)

## Finally

More wonderful CSS technical articles are summarized in my
[Github-iCSS](https://github.com/chokcoco/iCSS).

Well, that’s all for this article, I hope it helps you. :)