Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:06:02+00:00 2026-05-26T17:06:02+00:00

I have a header that has a css background image that I want to

  • 0

I have a header that has a css background image that I want to be side-scrolled at a rate of 1px every 50ms. However, the background image changes depending on the time on the users computer and I cannot work out how I would go about doing this. I did write the code for the side-scrolling itself, but nothing else.

The Javascript used to side-scroll (nothing else):

setInterval("mvheader()",50);
function mvheader () {
window.cssXPos=window.cssXPos+1;
if (window.cssXPos>=window.cssMaxWidth) {
window.cssXPos=0;
}
toMove=document.getElementById("header");
toMove.style.backgroundPosition=window.cssXPos+"px 0px";
}

The JavaScript used to use a different css file depending on the time:

function styleSwitcher() {
  var currentTime = new Date().getHours();
  if (0 <= currentTime&&currentTime < 6) {
   document.write("<link rel='stylesheet' href='css/night.css' type='text/css'>");
  }
  if (6 <= currentTime&&currentTime < 18) {
   document.write("<link rel='stylesheet' href='css/day.css' type='text/css'>");
  }
  if (18 <= currentTime&&currentTime <= 24) {
   document.write("<link rel='stylesheet' href='css/night.css' type='text/css'>");
  }
}

styleSwitcher();

The CSS (the only difference bewteen the other files is the folder name. night instead of day and vise versa):

#header {
background-image:url(../img/day/tile.png);
background-repeat:repeat-x;
position:absolute;
height:100px;
width:100%;
top:0;
left:0;
z-index:9;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:06:03+00:00Added an answer on May 26, 2026 at 5:06 pm

    All right, so there were couple of problems with your side-scrolling script:

    • running the mvheader function before the document was loaded (I’m assuming your code was in external js file)
    • using expression: setInterval("mvheader()", 50) it would work as expected, however using that syntax boils down to fact, that javascript has to interpret string passed as a first arguemnt. You can use setInterval(mvheader, 50) as an equivalent – as you can see, you would now pass function as a parameter to setInterval
    • not using “var” keyword when defining toMove variable – it wouldn’t work as expected in IE
    • polluting global scope (window object) with additional variables cssXPos and cssMaxWidth

    I’ve modified your code and you can check working version here: http://jsfiddle.net/wtk_pl/ydJhr/4/.

    It’s working fine, however I’ve also created seconds, cleaner version that don’t pollute global scope with new variables and should perform better because I “cache” the #header node in a variable, rather than searching for it on every iteration of a mvheader(). Check it out here: http://jsfiddle.net/wtk_pl/ydJhr/9/

    As to loading different css based on time of the day – It’s totally doable from javascript, however I would recommend you do that on server side (meaning determine and embed proper css file before sending page content to user) – why bother the client side script with that?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a background image centered that Chrome displays offset by one pixel. CSS
Guys, I have a CSS Layout that I am using that has a header,
I want to construct a div that has a header with background like that
I have an css background-image that needs to be linked to the site index.
I have a global header and footer that has a Database connection and a
I have a flat-file schema that has a header and detail records. It looks
I would like to have a table header that looks like the image shown
So I have a tiny header image that repeats on the x axis, but
I have 2 #header elements with different info in each that I want to
Background: I have a css and a js that is used only by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.