To get straight to the point, there is a very common situation for some scrollable elements. Usually when scrolling, a shadow is added to the side perpendicular to the scroll to indicate that an element is currently scrolled out of the visible area, similar to this: You can see that during the scrolling process, a shadow appears: For the problem that the columns on both sides remain stationary and attached to the border during scrolling, CSS usually uses However, for the shadow that appears during the scrolling process (if the content in the scrolling container is not touching the edge, the shadow appears, and if it is touching the edge, the shadow disappears), the previous approach has always required the use of JS. So, is there a solution that can be achieved with pure CSS? Hehehe, yes. There is a very clever trick, let us unveil it step by step. Magical To achieve the above scrolling shadow using pure CSS, the most important element to use is In an earlier article - CSS parallax effect, Of course, our protagonist today is not First, let’s talk about Simply put, it determines how the background pattern moves in a scrollable container. Through two simple demos, understand If you still don't understand the difference between them, you can check out the following DEMO to see for yourself: CodePen Demo -- bg-attachment Demo Use At this point, many students may still be confused. What exactly should we do? How does this relate to the scrolling shadow in this article? Don't worry, the difficulty of scrolling shadows is that there is no shadow when there is no scrolling at the beginning. The shadow will only appear when the scrolling starts. So here, we use the two properties of Um? What's the meaning. We add two gradient effects to the scroll container, using <!-- Scrollable container --> <ul> <li>...</li> ... <li>...</li> </ul> // Scenario 1: .g-one { background: linear-gradient(#fff, #f00); background-size: 100% 10px; background-repeat: no-repeat; background-attachment: local; } // Scenario 2: .g-two { background: radial-gradient(at 50% 0, #000, #0f0 70%); background-size: 100% 10px; background-repeat: no-repeat; background-attachment: scroll; } // Scenario 3: .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; } The actual effect is this: one background scrolls with the container, and the other background is fixed with the container. The background that scrolls with the container acts as an initial mask: OK, you can see that when scrolling, the last superimposed picture is actually the effect we need to show different colors (shadows) when scrolling. We adjust the colors of the two gradients, set the mask layer ( The CSS code looks like this: .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; } A gray shadow is simulated using OK, it’s done. For all the above DEMOs, you can check them out here: CodePen Demo -- Pure CSS Scroll shadow As shown at the beginning of the article, this technique can also be directly applied to CodePen Demo -- Pure CSS Table scroll shadow Some questions cascading order Of course, there is actually a problem in the above process. Because the shadow is simulated by the compatibility Well, of course there is another problem, which is the compatibility of The comments under Can i use indicate that most compatibility issues are caused by at last The techniques in this article are not original. I first saw them in this article: Exploring the practical value of the CSS attribute *-gradient, and further exploring whether it can be used in practice. This is the end of this article about using pure CSS to achieve scroll shadow effects. For more relevant CSS to achieve scroll shadow content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! |
<<: Several CSS3 tag shorthands (recommended)
>>: An example of using Lvs+Nginx cluster to build a high-concurrency architecture
Insert data into mysql database. Previously commo...
Preface: This article mainly introduces the query...
When developing a mobile page recently, I encount...
This article example shares the specific code of ...
MySQL's MyISAM and InnoDB engines both use B+...
Data Sheet: Column to row: using max(case when th...
I believe everyone is familiar with the trashcan,...
1. Implement call step: Set the function as a pro...
To split a string into an array, you need to use ...
Install Enter the following command to install it...
I always thought that Docker had no IP address. I...
Table of contents 1. Introduction 2. Main text 2....
Note: All pictures in this article are collected ...
This article mainly describes how to implement fo...
What is the difference between the green version ...