简体   繁体   中英

How to fade-in content with graceful degradation

I'm working with a team that makes heavy use of the "fade-in" effect to animate the rendering of images, and even text sometimes.

You can see the kind of effect I'm describing here: http://www.seafireresidencescayman.com/seven-mile-beach

在此处输入图片说明

The fade-in isn't really "my thing", but what actually troubles me the most is that all implementations that I've seen so far, make the site pretty useless when javascript isn't enabled.

Here's what the same site above looks like without javascript:

在此处输入图片说明

My team's preferred tool for this is AOS , which uses more-or-less the same approach that I see elsewhere:

  1. Hide designated content with CSS
  2. Reveal it in a jazzy way using javascript

If javascript isn't available, #2 doesn't happen and the content stays hidden. Not good.

I'm interested to find a way to accomplish the same/similar effect, but in a way that fails gracefully if javascript isn't available. I'm guessing the tricky part is to keep the content from flickering initially, and that's why most approaches need to hide the content "by default." So maybe it's not as simple as just hiding things with javascript instead of CSS...

Is there a non-evil way to do this?

I agree with @RobG's comment, the only progressive enhancement way of accomplishing this is to not hide the content with CSS, but to use JS to hide it. This could easily be done without jQuery, but for the sake of brevity...

<h1 class="fade-in">Yay, it's 2002!</h1>
<div>
   <img class="fade-in" src="/path/to/cat.gif" />
</div>

<script>
var $faded = $('.fade-in');
$faded.hide();
// could easily use a more intelligent mechanism for determining when to load
$('img.fade-in').on('load', function() {
   $faded.fadeIn();
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM