Table of Contents
Magical background-attachment" >Magical background-attachment
background-attachment: srcoll" >background-attachment: srcoll
srcoll Used simultaneously with local to achieve blindness" >srcoll Used simultaneously with local to achieve blindness
" >##Some questions
Home Web Front-end CSS Tutorial Tips for implementing scrolling shadow effect with CSS (share)

Tips for implementing scrolling shadow effect with CSS (share)

Feb 22, 2021 am 10:02 AM
css shadow effect

This article will introduce to you how to use pure CSS to achieve the rolling shadow effect. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Tips for implementing scrolling shadow effect with CSS (share)

[Recommended tutorial: CSS video tutorial]

Let’s get straight to the point, there is such a very common situation, for For some scrollable elements. Usually when scrolling, a shadow is added to the side perpendicular to the scrolling to indicate that an element is currently being scrolled out of the visible area, similar to this:

You can see that during the scrolling process, a shadow will appear:

For the columns on both sides During the scrolling process, the problem of standing still and adsorbing on the boundary can usually be solved by using CSS position: sticky.

But for the shadow that only appears during the scrolling process (the content in the scroll container has no welt, the shadow appears, and the welt, the shadow disappears), the previous approach has always been required Done with the help of JS.

So, is there a solution that can be achieved with pure CSS? Hehehe, yes. There is a very clever deception, let us uncover it step by step.

Magical background-attachment

To implement the above scrolling shadow using pure CSS, The core element to be used is background-attachment.

In an earlier article - CSS to achieve parallax effect, background-attachment was introduced in detail, with the help of background-attachment: fixed can easily realize the scrolling parallax of the website or the watermark effect similar to the picture click, similar to this:

Of course, today we The protagonist is not background-attachment: fixed, but background-attachment: srcoll.

background-attachment: srcoll

First of all, let me introduce background- attachment, if background-image is specified, then background-attachment determines whether the background is fixed in the viewport or scrolls with the block containing it.

To put it simply, it determines how the background pattern moves in a scrollable container. Through two simple demos, understand background-attachment: srcoll and background-attachment: local.

background-attachment: local, this is consistent with our daily usage. The background pattern of the scrollable container scrolls with the container:

##background-attachment: scroll, this is today’s protagonist, it indicates that the background is fixed relative to the element itself, rather than random. Scroll along with its content:

If you haven’t figured out the difference yet, you can click on the DEMO below to feel it for yourself:

Demo address: https://codepen.io/Chokcoco/pen/xJJorg

srcoll Used simultaneously with local to achieve blindness

Go here, Maybe many students are still confused, what should we do? How does this relate to the scrolling shadow in this article?

Don't worry, the difficulty with rolling shadows is that there is no shadow when there is no scrolling initially. The shadow will only appear when you start scrolling.

So here, we use the two attributes background-attachment: srcoll and background-attachment: local. During the initial scrolling, Use two layers of backgrounds superimposed together to hide the shadow background. When you actually scroll, remove the superimposed part and only the shadow part will leak out.

Um? What's the meaning. We use the scroll container and add two gradient effects, using background-attachment: srcoll and background-attachment: local respectively, and then superimpose them, like this:

1

2

3

4

5

6

<!-- 可滚动容器 -->

<ul>

    <li>...</li>

    ...

    <li>...</li>

</ul>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

// 情形一:

.g-one {

    background: linear-gradient(#fff, #f00);

    background-size: 100% 10px;

    background-repeat: no-repeat;

    background-attachment: local;

}

 

// 情形二:

.g-two {

    background: radial-gradient(at 50% 0, #000, #0f0 70%);

    background-size: 100% 10px;

    background-repeat: no-repeat;

    background-attachment: scroll;

}

 

// 情形三:

.g-combine {

    background:

        linear-gradient(#fff, #f00),

        radial-gradient(at 50% 0%, #000, #0f0 70%);

    background-size: 100% 10px, 100% 10px;

    background-repeat: no-repeat;

    background-attachment: local, scroll;

}

Copy after login

The actual effect is like this. One background scrolls with the container, and the other background is fixed with the container. The background that scrolls with the container serves as the initial mask layer:

OK, you can see the big picture, when scrolling, the last picture is superimposed , in fact, what we need is the effect of displaying different colors (shadows) when scrolling. Let’s adjust the colors of the two gradients. The mask layer (background-attachment: local) is white, and then the fixed shadow layer (background-attachment: scroll) is set. Use a radial gradient to simulate the shadow color we want.

The CSS code is probably like this:

1

2

3

4

5

6

7

8

.g-final {

    background:

        linear-gradient(#fff, transparent 100%),

        linear-gradient(rgba(0, 0, 0, .5), transparent 100%);

    background-size: 100% 30px, 100% 10px;

    background-repeat: no-repeat;

    background-attachment: local, scroll;

}

Copy after login

Use linear-gradient(rgba(0, 0, 0, .5) , transparent 100%) A linear gradient simulates a shade of gray:

OK, you’re done. For all the above DEMOs, you can click here to take a look:

Demo address: https://codepen.io/Chokcoco/pen/QWKmjKd

As shown at the beginning of the article, this technique can also be used directly in table:

##Demo address: https://codepen.io/Chokcoco/pen/abmqMJQ

##Some questions

Layering order

Of course, in the above process, there is always a problem, because it is using background

background simulation In fact, the final effect is that the content is in the shadow (above the background), but the actual effect is not very different. If you can tolerate this, this solution is completely available.

Compatibility

Well, of course there is another problem:

background-attachment compatibility issues. Let's look at CAN I USE:

Can i use The comments below indicate that most compatibility issues are actually

background- attachment: fixed, has little impact on the effect of this article.

Finally

The tips in this article are not original. I first saw them from this article: Exploring CSS Properties* The practical value of -gradient, we did some research on whether it can be used in practice.

Okay, this article ends here.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of Tips for implementing scrolling shadow effect with CSS (share). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to build a bootstrap framework How to build a bootstrap framework Apr 07, 2025 pm 12:57 PM

To create a Bootstrap framework, follow these steps: Install Bootstrap via CDN or install a local copy. Create an HTML document and link Bootstrap CSS to the &lt;head&gt; section. Add Bootstrap JavaScript file to the &lt;body&gt; section. Use the Bootstrap component and customize the stylesheet to suit your needs.

How to layout bootstrap How to layout bootstrap Apr 07, 2025 pm 02:24 PM

To use Bootstrap to layout a website, you need to use a grid system to divide the page into containers, rows, and columns. First add the container, then add the rows in it, add the columns within the row, and finally add the content in the column. Bootstrap's responsive layout function automatically adjusts the layout according to breakpoints (xs, sm, md, lg, xl). Different layouts under different screen sizes can be achieved by using responsive classes.

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles