本文翻译自:Using CSS for fade-in effect on page load

Can CSS Transitions be used to allow a text paragraph to fade-in on page load? CSS Transitions可用于允许文本段落在页面加载时淡入吗?

I really like how it looks on http://dotmailapp.com/ and would love to use a similar effect using CSS. 我真的很喜欢它在http://dotmailapp.com/上的样子,并且喜欢使用CSS使用类似的效果。

Note: The domain has since been purchased and no longer has the effect mentioned. 注意:该域名已被购买,不再具有上述效果。 An archived copy can be viewed on the Wayback Machine . 可以在Wayback Machine上查看存档副本。

Illustration 插图

Having this markup: 有这个标记:

<div id="test">    <p>​This is a test</p>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

With the following CSS rule: 使用以下CSS规则:

#test p {opacity: 0;margin-top: 25px;font-size: 21px;text-align: center;-webkit-transition: opacity 2s ease-in;-moz-transition: opacity 2s ease-in;-o-transition: opacity 2s ease-in;-ms-transition: opacity 2s ease-in;transition: opacity 2s ease-in;
}​

How can the transition be triggered on load? 如何在加载时触发转换?


#1楼

参考:https://stackoom.com/question/n0O7/使用CSS对页面加载的淡入效果


#2楼

Method 1: 方法1:

If you are looking for a self-invoking transition then you should use CSS3 Animations , they aren't supported as well but this is exactly the kind of thing they were made for. 如果您正在寻找一个自我调用的过渡,那么您应该使用CSS3动画 ,它们也不受支持,但这正是它们的目的。

CSS CSS

#test p {margin-top: 25px;font-size: 21px;text-align: center;-webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */-moz-animation: fadein 2s; /* Firefox < 16 */-ms-animation: fadein 2s; /* Internet Explorer */-o-animation: fadein 2s; /* Opera < 12.1 */animation: fadein 2s;
}@keyframes fadein {from { opacity: 0; }to   { opacity: 1; }
}/* Firefox < 16 */
@-moz-keyframes fadein {from { opacity: 0; }to   { opacity: 1; }
}/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {from { opacity: 0; }to   { opacity: 1; }
}/* Internet Explorer */
@-ms-keyframes fadein {from { opacity: 0; }to   { opacity: 1; }
}/* Opera < 12.1 */
@-o-keyframes fadein {from { opacity: 0; }to   { opacity: 1; }
}

Demo 演示

  • http://jsfiddle.net/SO_AMK/VV2ek/ http://jsfiddle.net/SO_AMK/VV2ek/

Browser Support 浏览器支持

All modern browsers, IE 10+: http://caniuse.com/#feat=css-animation 所有现代浏览器,IE 10+: http : //caniuse.com/#feat=css-animation

Method 2: 方法2:

Alternatively, you can use jQuery (or plain JS, see third code block) to change the class on load: 或者,您可以使用jQuery(或普通JS,请参阅第三个代码块)来更改加载时的类:

jQuery jQuery的

$("#test p").addClass("load");​

CSS CSS

#test p {opacity: 0;font-size: 21px;margin-top: 25px;text-align: center;-webkit-transition: opacity 2s ease-in;-moz-transition: opacity 2s ease-in;-ms-transition: opacity 2s ease-in;-o-transition: opacity 2s ease-in;transition: opacity 2s ease-in;
}#test p.load {opacity: 1;
}

Plain JS (not in demo) 普通JS(不在演示中)

document.getElementById("test").children[0].className += " load";

Demo 演示

  • http://jsfiddle.net/SO_AMK/a9dnW/ http://jsfiddle.net/SO_AMK/a9dnW/

Browser Support 浏览器支持

All modern browsers, IE 10+: http://caniuse.com/#feat=css-transitions 所有现代浏览器,IE 10+: http : //caniuse.com/#feat=css-transitions

Method 3: 方法3:

Or, you can use the method that .Mail uses: 或者,您可以使用.Mail使用的方法:

jQuery jQuery的

$("#test p").delay(1000).animate({ opacity: 1 }, 700);​

CSS CSS

#test p {opacity: 0;font-size: 21px;margin-top: 25px;text-align: center;
}

Demo 演示

  • http://jsfiddle.net/SO_AMK/a9dnW/3/ http://jsfiddle.net/SO_AMK/a9dnW/3/

Browser Support 浏览器支持

jQuery 1.x : All modern browsers, IE 6+: http://jquery.com/browser-support/ jQuery 1.x :所有现代浏览器,IE 6+: http : //jquery.com/browser-support/
jQuery 2.x : All modern browsers, IE 9+: http://jquery.com/browser-support/ jQuery 2.x :所有现代浏览器,IE 9+: http : //jquery.com/browser-support/

This method is the most cross-compatible as the target browser does not need to support CSS3 transitions or animations. 此方法是最交叉兼容的,因为目标浏览器不需要支持CSS3过渡动画。


#3楼

In response to @AMK's question about how to do transitions without jQuery. 回应@AMK关于如何在没有jQuery的情况下进行转换的问题。 A very simple example I threw together. 一个非常简单的例子我一起扔了。 If I had time to think this through some more, I might be able to eliminate the javascript altogether: 如果我有时间考虑更多,我可以完全消除javascript:

<style>
body {background-color: red;transition: background-color 2s ease-in;
}
</style>
<script>
window.onload = function() {document.body.style.backgroundColor = '#00f';
}
</script>
<body><p>test</p>
</body>

#4楼

You can use the onload="" HTML attribute and use JavaScript to adjust the opacity style of your element. 您可以使用onload="" HTML属性并使用JavaScript来调整元素的不透明度样式。

Leave your CSS as you proposed. 保留你提出的CSS。 Edit your HTML code to: 将您的HTML代码编辑为:

<body onload="document.getElementById(test).style.opacity='1'"><div id="test"><p>​This is a test</p></div>
</body>

This also works to fade-in the complete page when finished loading: 这也适用于在完成加载时淡入整个页面:

HTML: HTML:

<body onload="document.body.style.opacity='1'">
</body>

CSS: CSS:

body{ opacity:0;transition: opacity 2s;-webkit-transition: opacity 2s; /* Safari */
}

Check the W3Schools website: transitions and an article for changing styles with javascript . 查看W3Schools网站: 转换和使用javascript更改样式的文章。

使用CSS对页面加载的淡入效果相关推荐

  1. css 实现页面加载中等待效果

    <!DOCTYPE html> <html> <head> <title>css实现页面加载中,请稍候效果</title><meta ...

  2. python能做页面加载动画吗_HTML+CSS实现页面加载(loading)动画效果

    大家在浏览页面时有没有遇到页面正在loading(加载)的情况,那作为一个前端开发人员,你知道如何用CSS3和HTML制作页面加载动画效果吗?这篇文章就和大家分享一个超级简单实用的CSS3 圆圈加载( ...

  3. css3加载图片淡入效果

    代码如下: <!DOCTYPE html> <html> <head><title></title><meta charset=&qu ...

  4. 在一个html加载多个echarts,Echarts一个页面加载多个图表及图表自适应

    Echarts一个页面加载多个图表及图表自适应 模块化加载 //入口 require.config({ paths: { echarts: 'http://echarts.baidu.com/buil ...

  5. html实现图片加载动画效果,HTML5+javascript实现图片加载进度动画效果

    在网上找资料的时候,看到网上有图片加载进度的效果,手痒就自己也写了一个. 图片加载完后,隐藏loading效果. 想看加载效果,请ctrel+F5强制刷新或者清理缓存. 效果预览: 0% 代码如下: ...

  6. html禁止页面动画,如何在页面加载时阻止CSS动画?

    我有一个动画,它负责按钮悬停状态下的淡入和淡出过渡. 问题是默认动画(-webkit-animation:off-state 1s;)在页面加载时触发.如何在第一个悬停状态后才使其处于活动状态? 我知 ...

  7. 页面加载完毕相关信息淡入效果

    前言: 年关将至,公司一部分同事已经回老家了,虽然过年不回去,但想到明天上完班就放假了内心多少有点激动.工作上的事情不要紧的已经没心情再看了,加之今天领导不在 哈哈哈... 搞点自己的爱好! 看boo ...

  8. asp.net使用httphandler打包多CSS或JS文件以加快页面加载速度

    介绍 使用许多小得JS.CSS文件代替一个庞大的JS或CSS文件来让代码获得更好的可维护性,这是一个很好的实践.但这样做反过来却损失了网站的性能.虽然你应该将你的Javascript代码写在小文件中并 ...

  9. ASP.NET 打包多CSS或JS文件以加快页面加载速度的Handler

    ASP.NET 打包多CSS或JS文件以加快页面加载速度的Handler, 使用<link type="text/css" rel="Stylesheet" ...

最新文章

  1. 刘道成mysql视频教程_燕十八刘道成Mysql 系列视频教程 Mysql视频教程打包下载
  2. 不要把为师我说出来就不错了
  3. kotlin学习之泛型(十四)
  4. linux 系统课程-进程控制01
  5. SVN更新时报403错误
  6. 2万8千张图片如何用python组成一张(简洁明了附源码)
  7. 常用的模型评估指标(转)
  8. JetBrains DataGrip 2018.2.3中文破解版 含jar文件注册码激活教程(转)
  9. 我常去的编程技术网站
  10. windows10显示文件后缀名
  11. 香港身份证正则表达式
  12. java正则匹配下划线_正则表达式(匹配英文、中文、数字、下划线)
  13. 软件实施工程师需要掌握的技能
  14. 购买阿里云GPU虚拟化型实例规格族vgn6i抢占式实例并搭建CUDA 11.5和cuDNN 8.3.0
  15. Redis 的 RDB 和 AOF
  16. dash linux命令,Linux shell语言——dash和bash
  17. 功利主义穆勒思维导图_穆勒——《功利主义》
  18. springboot毕设项目在线智能办公系统bxl9i(java+VUE+Mybatis+Maven+Mysql)
  19. 十二大行的金融科技子公司,哪家注册资本最高?
  20. 简帛企业云智库知识管理系统

热门文章

  1. Android中事件的传递
  2. Handler消息机制(三):一个线程有几个Looper?如何保证?
  3. [置顶] ActivityGroup自我堆栈管理(复用现有activity)
  4. 多线程编程 之 (生产者与消费者(N多))同步常用的方法。
  5. 【Android】Java回调原理并结合Android源码进行理解
  6. jQuery方法大全
  7. android源码中常用的Rect方法
  8. iOS进阶之协议Protocol(13)
  9. (0013)iOS 开发之集成友盟第三方登录
  10. C# — 通过点击回车执行任务