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 8805855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:58:18+00:00 2026-06-14T01:58:18+00:00

In my website I have a calendar, whenever a user hovers over a day

  • 0

In my website I have a calendar, whenever a user hovers over a day of the calendar I want the background to change, but of course I don’t want all that loading overhead to happen at page load, but rather only when the user hovers over one of the days.

I tried to do it by following what I found in this question, but it seems to only work the first time. This is what I did after modifying the plugins code as a test:

counter = 0;
  $('.calendar-day').hover(
    function() {
      if (counter == 0) {
        $('body').attr('data-background', template_url + '/images/bg_2.jpg'); 
        counter++;
      } else if (counter == 1) {
        $('body').attr('data-background', template_url + '/images/bg_3.jpg'); 
        counter++;
      } else if (counter == 2) {
        $('body').attr('data-background', template_url + '/images/bg_4.jpg'); 
        counter++;
      } else if (counter == 3) {
        $('body').attr('data-background', template_url + '/images/bg_1.jpg'); 
        counter = 0;
      }  
      $('body').lazyload();
    },
    function() {

    }  
  );

Even though it does trigger all the cases, the lazy loading doesn’t happen and the background doesn’t change after the first time. Any thoughts on this? Other approaches to managing this are welcome as well.

  • 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-06-14T01:58:19+00:00Added an answer on June 14, 2026 at 1:58 am

    This might do the trick for you. Give it a try:

    var counter = -1,
        loadImage = function(onCase) {
            return function(e) {
                if (counter % 4 === onCase) {
                    $('body').css('background-image', 'url(' + this.src + ')');
                }
            };
        };
    
    $('body').on('mouseenter', '.calendar-day', function() {
        counter++;
        switch (counter % 4) {
            case 0:
                $('<img>').on('load', loadImage(0))
                    .attr('src', template_url + '/images/bg_2.jpg');
                break;
            case 1:
                $('<img>').on('load', loadImage(1))
                    .attr('src', template_url + '/images/bg_3.jpg');
                break;
            case 2:
                $('<img>').on('load', loadImage(2))
                    .attr('src', template_url + '/images/bg_4.jpg');
                break;
            case 3:
                $('<img>').on('load', loadImage(3))
                    .attr('src', template_url + '/images/bg_1.jpg');
                break;
        }
    });
    

    onCase is meant to verify we still wanna add “this” background and haven’t moved yet to another, in case of a race condition.

    EDIT: a cleaner version

    var counter = -1,
        loadImage = function(index, backgroundImages) {
            $('<img>').on('load', function(e) {
                if (counter % backgroundImages.length === index) {
                    $('body').css('background-image', 'url(' + this.src + ')');
                }
            }).attr('src', backgroundImages[index]);
        },
        backgroundImages = [
            template_url + '/images/bg_2.jpg',
            template_url + '/images/bg_3.jpg',
            template_url + '/images/bg_4.jpg',
            template_url + '/images/bg_1.jpg'
        ];
    
    $('body').on('mouseenter', '.calendar-day', function() {
        counter++;
        loadImage(counter % backgroundImages.length, backgroundImages);
    });
    

    When the mouseenter (the first state of an hover, the second being mouseleave) event is triggered by a .calendar-day element, we increase counter and call for loadImage. Using counter % backgroundImages.length will return our current index, a number between 0 – backgroundImages.length (not including the value of backgroundImages.length itself). If you don’t know the modulo sign (%) you could read about it here.

    loadImage creates an img element which is not attached to the dom, adds an event listener to catch when the image has been loaded, then sets the img element src to our current image. Once the image is loaded we check whether or not we still wanna put that image as background (or have we moved to another image already), if so then we set the body background image, confident it was already loaded. Once the image as been loaded once it should be in our browsers catch, so the next time we attach an event listener to see when an image is loaded, it should fire immediately.

    Hope I didn’t miss anything, if so please let me know.

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

Sidebar

Related Questions

I have website, usually all looks fine but sometimes one div is on the
I have a website that is a calendar with all the basic features (ASP.NET
A prospective client wants to have a calendar feature on their website. They want
I have a question about XSLT. On a website i have a simple calendar,
I have a calendar on my website, generated in Perl using Template::Toolkit and Template::Plugin::Date
What I want to do is to embed music files within a website (Have
i have website of application which sells applications online.on the home page i want
On a website I have implemented the login using OpenID (based on StackOverflow). But
I run a church website and we have an event calendar page. We've been
I have a custom template tag which shows a calendar. I want to populate

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.