Time Change是一个最小的jQuery脚本,它可以根据当前时间(12点之前或之后)自动切换元素的CSS类
当你想鼓励用户经常登录或在你的网站上做一些事情时(可能是在12点之前或之后),你可以在你的站点中使用它。
1.在文档中加载必要的jQuery库(slim build)。
- <script src="/path/to/cdn/jquery.slim.min.js"></script>
2.根据当前时间从元素中添加和删除CSS类的主脚本。
- $(document).ready(function(){
- let date1 = new Date();
- let year = date1.getFullYear();
- let hours = 12;
- let month = date1.getMonth();
- let date = date1.getDate();
- let date2 = new Date(year,month,date,hours);
- // before 12:00
- if(date1 < date2){
- $('body').addClass("before");
- $('body').removeClass("after");
- }
- // after before 12:00
- else{
- $('body').addClass("after");
- $('body').removeClass("before");
- }
- });
3.将您自己的CSS样式应用于元素。
- .before {
- background: red;
- color: white;
- }
- .after {
- background: blue;
- color: white;
- }