【问题标题】:How to change elements' appearance with time of the day?如何随时间改变元素的外观?
【发布时间】:2021-12-20 06:40:00
【问题描述】:

我想根据一天中的时间更改元素的背景图像并更改标题的内容。如果时间在6点到12点之间,bg和文字必须按照早上来。

【问题讨论】:

    标签: html css darkmode


    【解决方案1】:

    您不能只使用 css 和 html。 你应该使用js。

    如果你设置你的夜间、白天时间 CSS,你可以使用这个技巧:

    var date = new Date;
    var hour = date.getHours();
    if(hour > 18 || hour < 6){
        document.body.className += ' nighttime-css';
        // JQUERY => $('body').addClass('nighttime-css')
    }else{
        document.body.className += ' daytime-css';
        // JQUERY => $('body').addClass('daytime-css')
    }
    

    css:

    .nighttime-css{背景:url('nighttime-bg.jpeg');

    .daytime-css{背景:url('daytime-bg.jpeg');

    【讨论】:

    • 我不明白 document.body.className += 'daytime-css' 的意思; // JQUERY => $('body').addClass('daytime-css')
    • @HunainAhmed 如果你添加班级背景会改变
    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2015-12-23
    • 2015-04-03
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多