Tips for implementing scrolling shadow effect with CSS (share)
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.
[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 |
|
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 |
|
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 |
|
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 backgroundbackground 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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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 the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

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 <head> section. Add Bootstrap JavaScript file to the <body> section. Use the Bootstrap component and customize the stylesheet to suit your needs.

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.

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)

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).

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.
