Gradually fade out top of the div while scrolling

Hi,

I am working on the following layout:

http://test'dot'tutsandtips'dot'com/test-page-1/

The page consists of two independently scrollable columns. If you bring the cursor over one of the columns and scroll up/down, you will notice that as the column content goes up, its top fades out. The fade out approach I am using checks the distance of each element in the column from the fade-out line and applies an opacity value to the element accordingly.

The issue is that for elements that are high, such as a long paragraph or an image, it does not look well, so my question is if a perfectly gradual fade out effect was possible via CSS or JS. For example, if the top of the image reaches the fade-out line, the top of the image will be invisible and the rest will have a gradual increasing opacity till the bottom, like a transparent visibility gradient.

Thanks for any ideas.

Hi,

You could probably do this with css3 blend modes or css3 mask images but support is sparse at the moment.

An easier method would be to shim a fading image over the top. i.e. create the same image in your paint package but fade it out to transparent somewhere from the top. This image is then placed on top of the content to be faded out. The image must match the dimensions of the original to keep pace with the background-size.

Here is a live example and a codepen here:

It seems to work in modern browsers but there could be an issue in older browsers where links are not clickable because of the overlay so extensive testing is required.

**Note that the above uses no JS ** for anything and there is no need to manipulate the heights of any elements assuming the header is to remain a fixed height (and even minor difference in header height could be masked by that fade effect anyway).

With css3 blend modes or mask-image you should be able to create the mask image automatically but the rules are complex and poorly supported at present.

As mentioned above you will need to test this method out in the browsers you want to support as I have only just thought of this method a few minutes ago :smile:

1 Like

Thanks a lot! I will see if I can use this approach. Could you please remove the link in the quote from your comment? I don’t want search engines to follow that link. Thanks.

Ok removed and I also obfuscated your original link in case anyone else did as I did and just quoted the reply because it automatically gets turned into a link :smile:

1 Like

Thanks will be using that trick next time :smile:

Hi there nayen,

I would suggest that you don’t use background images behind
scrolling text, as they can possibly cause readability problems. :cold_sweat:

This problem will certainly be magnified with the implementation
of a rather distracting fading effect. :scream:

If your heart is set on having the fading effect though, then a
solid colour should really be your choice for the background. :sunglasses:

Using …[quote=“PaulOB, post:2, topic:220606”]
live example
[/quote]
…as a basis, the follow code provides a responsive example…

[code]

Untitled Document html { box-sizing: border-box; } * { box-sizing: inherit; } html,body { height: 100%; margin: 0; padding: 0; background-color: rgba(240,240,240,1); font: 1em/1.62em arial,helvetica sans-serif; } #header { position: fixed; top: 0; left: 0; right: 0; z-index: 4; min-height: 6em; text-align: center; color: rgba(51,51,51,1); } h1 { margin: 0.62em 0; font-size: 1.2em; } h2 { font-size: 1.2em; text-align: center; } /* could use body:after but IE breaks with it*/ #overlay { position: fixed; top: 0; left: 0; bottom: 0; right: 0; z-index: 3; pointer-events: none; background: linear-gradient(to bottom,rgba(240,240,240,1), rgba(240,240,240,1) 16%, rgba(240,240,240,0) 48%); } #left,#right { position: fixed; left: -1.5em; top: 6.5em; bottom: -1.5em; width: 65%; overflow: hidden; } #right { left: auto; right: 0; width: 35%; } #left-inner,#right-inner { position: absolute; top: 0; left: 0; bottom: 0; right: -1.5em; padding: 1.5em 2em; font-size: 1.25em; color: rgba(51,51,51,1); overflow: scroll; } #right-inner { padding-left:0; } #left-inner p:last-of-type, #right-inner p:last-of-type { padding-bottom: 2em; } @media screen and (max-width:40em){ #content { position: fixed; left: 0; top: 6.5em; bottom: -1.5em; width: 100%; overflow: hidden; } #content-inner { position: absolute; top: 0; left: -1.5em; bottom: 0; right: -1.5em; padding:1.5em 0; overflow: scroll; } #left,#right,#left-inner,#right-inner { position:static; width: 100%; padding-bottom: 0; overflow: hidden; } #right-inner { padding-left: 2em; } #left-inner p:last-of-type { padding: 0; margin-bottom: 0; } h2 { margin:0; } }

Wheel or up and down keys scrolls content

Left content

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet sem sed libero bibendum tempus faucibus quis mi. Nulla rhoncus vel ipsum in volutpat. Nam tortor nibh, posuere ac lorem ut, suscipit tincidunt leo. Duis interdum justo ac justo vehicula consequat. Curabitur et volutpat nibh. Phasellus rutrum lacus at dolor placerat feugiat. Morbi porta, sapien nec molestie molestie, odio magna vestibulum lorem, vitae feugiat est leo sit amet nunc. Curabitur ornare tempor turpis. In nibh sem, porta ac magna sed, pretium commodo odio. Sed porttitor augue velit, quis placerat diam sodales ac. Curabitur vitae porta ex. Praesent rutrum ex fringilla tellus tincidunt interdum. Proin molestie lectus consectetur purus aliquam porttitor. Fusce ac nisi ac magna scelerisque finibus a vitae sem.

Donec vehicula diam non leo efficitur, id euismod odio tincidunt. Vivamus auctor viverra purus vitae lobortis. Sed et libero non diam tempor sagittis. Quisque vel egestas ipsum. Integer sed elit eu orci blandit commodo in in erat. Donec blandit, mi at gravida varius, nulla tellus vulputate ex, vitae euismod erat lectus rutrum diam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur tristique varius ullamcorper.

Nam venenatis diam ante, et cursus elit porttitor at. Morbi mattis leo at ex vehicula, non vestibulum ligula aliquam. Maecenas non nibh sollicitudin, porttitor odio in, elementum arcu. Donec pulvinar orci enim. In pulvinar congue ante, ac rutrum odio bibendum volutpat. Phasellus ac sem consequat lorem congue malesuada vitae vitae diam. Donec eu imperdiet augue. Curabitur ullamcorper sit amet libero in ullamcorper. Donec sed laoreet quam. Phasellus malesuada libero felis, non elementum ex tincidunt eget. Quisque cursus arcu vel diam consectetur, ac imperdiet est cursus. Fusce id nunc nibh.

Right content

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet sem sed libero bibendum tempus faucibus quis mi. Nulla rhoncus vel ipsum in volutpat. Nam tortor nibh, posuere ac lorem ut, suscipit tincidunt leo. Duis interdum justo ac justo vehicula consequat. Curabitur et volutpat nibh. Phasellus rutrum lacus at dolor placerat feugiat. Morbi porta, sapien nec molestie molestie, odio magna vestibulum lorem, vitae feugiat est leo sit amet nunc. Curabitur ornare tempor turpis. In nibh sem, porta ac magna sed, pretium commodo odio. Sed porttitor augue velit, quis placerat diam sodales ac. Curabitur vitae porta ex. Praesent rutrum ex fringilla tellus tincidunt interdum. Proin molestie lectus consectetur purus aliquam porttitor. Fusce ac nisi ac magna scelerisque finibus a vitae sem.

Donec vehicula diam non leo efficitur, id euismod odio tincidunt. Vivamus auctor viverra purus vitae lobortis. Sed et libero non diam tempor sagittis. Quisque vel egestas ipsum. Integer sed elit eu orci blandit commodo in in erat. Donec blandit, mi at gravida varius, nulla tellus vulputate ex, vitae euismod erat lectus rutrum diam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur tristique varius ullamcorper.

[/code]

coothead

Yes good point and that was why in my original demo I placed a transparent background behind the text to keep the contrast readable.(I thought blend modes might provide an answer but I obviously didn’t get the right combination :smile: )

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.