Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

dynamic css - Changing background based on time of day (using javascript)

Okay, I have two different background .jpgs that I want to used as the backgroud depending on what time of day it is. I want the sunny background from 7am to 8pm and the night background from 8pm to 7am.

I'm working on a remote html file and I have the backgrounds and graphics stored on a separate server from the actual html file. Most of the css is in the html file except for the the body section that contains the background code which I separated and moved to an external style sheet. I made two separate style sheets, one w/ the day bg image & one with the night. This is the body css that's on the night css style sheet.

@charset "UTF-8";
/* CSS Document */

         body { 
                background-image: url('http://itsnotch.com/tumblr/images/daytime_bg.jpg') !important;
                background-repeat: repeat-x !important;
                background-attachment: fixed !important;
                background-position: top center !important;
                background-color:#b68e59 !important;
                margin-top:-5px !important;
                font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
                font-size:14px !important;
         }

I've written a javascript code that I thought would work. But it didn't. I placed it right below the and it still didn't work. Why didn't it work? I just want to be able to switch the background out... time-sensitively.

<script type="text/JavaScript">
<!--
function getStylesheet() {
      var currentTime = new Date().getHours();
      if (7 <= currentTime&&currentTime < 20) {
       document.write("<link rel='stylesheet' href='http://itsnotch.com/tumblr/files/daytime.css' type='text/css'>");
      }
      else {
       document.write("<link rel='stylesheet' href='http://itsnotch.com/tumblr/files/nighttime.css' type='text/css'>");
      }
      rel='stylesheet' href='night.css' type='text/css'>");
      }
}

getStylesheet();
-->
</script>

<noscript><link href="http://itsnotch.com/tumblr/files/daytime.css" rel="stylesheet" type="text/css"></noscript>

here is the entire html code so you guys can see where I placed the javascript code.(i updated the javascript code and it still doesn't work.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>

        {block:Description}<meta name="description" content="{MetaDescription}" />{/block:Description}

        <!-- DEFAULT COLORS -->
        <meta name="color:Background" content="#d7a769"/>
        <meta name="color:Title" content="#444"/>
        <meta name="color:Description" content="#666"/>
        <meta name="color:Post Title" content="#c00"/>    
        <meta name="color:Text" content="#444"/>
        <meta name="color:Inline Link" content="#c00"/>
        <meta name="color:Quote" content="#666"/>
        <meta name="color:Quote Source" content="#444"/>
        <meta name="color:Link Post" content="#c00"/>
        <meta name="color:Conversation Background" content="#f8f8f8"/>
        <meta name="color:Conversation Border" content="#ddd"/>
        <meta name="color:Conversation Text" content="#444"/>
        <meta name="color:Conversation Label" content="#111"/>
        <meta name="color:Photo Border" content="#eee"/>
        <meta name="color:Date" content="#fff"/>
        <meta name="color:Date Background" content="#ccc"/>
        <!-- END DEFAULT COLORS -->


        <!-- DEFAULT SETTINGS -->
        <meta name="image:Background" content="http://assets.tumblr.com/images/x.gif" />
        <meta name="font:Title" content="Arial" />
        <meta name="font:Body" content="Lucida Sans" />
        <meta name="text:Disqus Shortname" content="" />

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>{block:SearchPage}Search results for "{SearchQuery}" - {/block:SearchPage}{block:PostSummary}{PostSummary} - {/block:PostSummary}{Title}</title>
        <link rel="icon" href="{Favicon}"/>
        <link rel="alternate" type="application/rss+xml" title="RSS" href="{RSS}"/>
        <meta name="viewport" content="width=775"/> <!-- iPhone -->








        <style type="text/css">

            #content {
                width:            850px;
                margin:           auto;
                margin-top:       -44px;
                padding:          15px;
                background-color: {color:Content Background};
                position:         relative;
            }

            a {
                color:            {color:Inline Link};
            }

            h1 {
                padding:          30px 0px 50px 0px;
                margin:           0px;
                text-align:       center;
                font:             Bold 55px {font:Title};
                letter-spacing:   -2px;
                line-height:      50px;
            }

            h1 a {
                color:            {color:Title};
                text-decoration:  none;
            }

            #description {
                position:         absolute;
        left:        -189px;
                top:             150px;
            }

            #description div {
                font:             normal 18px {font:Body};
                line-height:      20px;
                width:            150px;
                color:            {color:Description};
            }

                        #description div#search {
                                text-align: left;
                        }

            #description div a {
                color:            {color:Description};
            }

            #description #nav_container {
                font-size: 13px;
                font-weight: bold;
            }

            #description #nav_container .dim {
                filter: alpha(opacity=50); 
                -moz-opacity: 0.5; 
                opacity: 0.5;
            }

                        #searchresultcount {
                            margin: 0 0 30px;
                            text-align: center;
                        }

            .post {
                position:         relative;
                margin-bottom:    40px;
            }

            .post div.labels {
                position:         absolute;
                right:            435px;
                text-align:       right;
                width:            150px;
            }

            .post div.date {
                background-color: {color:Date Background};
                white-space:      nowrap;
                font:             Normal 20px {font:Body};
                letter-spacing:   -1px;
                color:            {color:Date};
                display:          inline;
                margin-right:     -550px;
                padding:          3px 5px 0px 5px;
                line-height:      20px;
            }

            .post div.date a {
                color:            {color:Date};
                text-decoration:  none;
            }

            .post img {
              max-width: 100%;
            }

            .post h2 {
                font-size:        18px;
                font-weight:      Bold;
                color:            {color:Post Title};
                letter-spacing:   -1px;
                margin:           0px 0px 10px 0px;
            }

            .post h2 a {
                color:            {color:Post Title};
                text-decoration:  none;
            }

            /* Regular Post */
            .post .regular {
                font-size:        12px;
                color:            {color:Text};
                line-height:      17px;
            }

            .post .regular blockquote {
                font-style: italic;
            }

            /* Photo Post */
            .post .photo img {
                border:           solid 10px {color:Photo Border};
            }

            .post .photo div.caption {
                font-size:        11px;
                color:            {color:Text};
                margin-top:       5px;
            }

            .post .photo div.caption a {
                color:            {color:Text};
            }

            /* Quote Post */
            .post .quote span.quote {
                font:             Bold 28px {font:Body};
                letter-spacing:   -1px;
                color:            {color:Quote};
            }

            .post .quote span.quote a {
                color:            {color:Quote};
            }

            .post .quote span.quote big.quote {
                font:             Bold 60px Georgia, serif;
                line-height:      8px;
                vertical-align:   -20px;
            }

            .post .quote span.source {
                font-size:        16px;
                font-weight:      Bold;
                color:            {color:Quote Source};
                letter-spacing:   -1px;
            }

            .post .quote span.source a {
                color:            {color:Quote Source};
            }

            /* Link Post */
            .post .link a.link {
                font:             Bold 20px {font:Body};
                letter-spacing:   -1px;
                color:            {color:Link Post};
            }

            .post .link span.description {
                font-size: 13px;
                font-weight: normal;
                letter-spacing: -1px;
            }

            /* Conversation Post */
            .post .conversation ul {
                background-color: {color:Conversation Background};
                list-style-type:  none;
                margin:           0px;
                padding:          0px;
                border-left:      solid 5px {color:Conversation Border};
            }

            .post .conversation ul li {
                border-bottom:    solid 1px {color:Conversation Border};
                font-size:        12px;
                padding:          4px 0px 4px 8px;
                color:            {color:Conversation Text};
            }

            .post .conversation ul li span.label {
                font-weight:      bold;
                color:            {color:Conversation Label};
            }

            /* Audio Post */      
            .post .audio div.caption {
                font-size:        11px;
                color:            {color:Text};
                margin-top:       5px;
            }

            .post .audio div.caption a {
                color:            {color:Text};
            }

            /* Video Post */
            .post .video {
                width:            400px;
                margin:           auto;
            }

            .post .video div.caption {
                font-size:        11px;
                color:            {color:Text};
                margin-top:       5px;
            }

            .post div.video div.caption a {
                color:            {color:Text};
            }

            /* Twitter Text */
            .twtr-tweet-text {
             font: 11px Verdana, Arial, Helvetica, sans-serif !important;
            }

            .twtr-tweet

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'd recommend something slightly different than neo - rather than setting the image only, have two CSS classes, one for day and one for night - both can be in the same stylesheet. You can set the body's class depending on the time of day. This will allow you to do more than just the background.

document.body.className = "day";
or
document.body.className = "night";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...