Positioning CSS background in relation to the cursor

Can anyone guide me on achieving a similar effect to this website:

(specifically referring to the moving blue background in the top blue menu, and how it is relative to cursor position)

Is there a script available that can track cursor movements and adjust background position styles accordingly?

Answer №1

To create this effect, a small piece of JavaScript code can be used:

Implementation in JavaScript

document.addEventListener('mousemove', function (event) {
    if (window.event) { // Addressing compatibility with IE
        event = window.event;
    }

    // Obtain the X-position of the mouse.
    var mousex = event.clientX;
    var header = document.getElementById('header');
    header.style.backgroundPosition = mousex/3 + 'px 0';
}, false);

See Demo

Explanation of the Code :

  1. The script attaches a function to the mousemove event on the document.
  2. It retrieves the current position of the mouse using event.clientX.
  3. This code updates the background-position of the element #header at one-third speed based on the mouse movement (mousex/3). Learn more here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the CSS issue following the recent webpack update?

I've taken on the task of updating an older VUE project with the latest npm packages. I managed to update everything successfully and get webpack to compile without any errors, but for some unknown reason, the CSS is no longer rendering in the browser ...

Creating interactive navigation bar effects with scroll and click functionality using jQuery

Is there a way to make the navigation highlight when a user clicks on it and also scrolls to the corresponding section? Currently, I am facing an issue where only the third navigation event highlights, most likely because when navigating to the fourth or f ...

Developing numerous test suites within Selenium WebDriver

I am working on a testing script using selenium webdriver, phantomJS, and Mocha. My scripts are written in JavaScript. These are the variables declared outside the test modules: var afterLoginURL; var afterLoginTitle; var loginFlag = 0; I have two test ...

Retrieving and updating data in IE/Edge through dataTransfer methods

While developing a React Application, I encountered an issue in Internet Explorer and Edge where the following lines of code were causing errors: Error (1) Message: Element not found. Code: onDragStart={event => { event.dataTransfer.setData(&a ...

When a selection is made, the tab changes to a new URL. However, what happens if the tab is closed during the next selection change?

Is there a way for me to detect if the user has closed the browser tab that I opened and redirected to a URL? Here is the code I have so far. It checks if the user changes the value of a select dropdown, then it verifies whether the browser window was alr ...

Assigning classes and data to each attribute by mapping a Json object using a for-each loop

I am struggling to separate the Jason data into individual divs with the class .name and the data-key za data attribute. I am encountering issues with getting the correct index for the data. My attempt so far has been buggy. While console.log successfully ...

Integrate a loading screen on the website

Check out the awesome animation I created! Can this be used as a preloader on my website? Should I stick to creating a simple .gif or opt for a CSS animation instead? If it’s feasible, how do I go about integrating it into my site? Most tutorials suggest ...

Add up the monthly totals of an array of objects using JavaScript

I have a set of objects arranged in the following manner: var json = [ { day: '01-01-2018', hour: '00:00', value: '121' }, { day: '01-02-2018', hour: '05:24', value: '131' }, { day: '26-01-2 ...

What is the process for invoking a server-side Java function from HTML with JavaScript?

Can someone help me figure out the best way to invoke a Java method from HTML (I'm working with HTML5) using JavaScript? I attempted using Applets but that approach didn't yield any results. My goal is to extract the value from a drop-down menu i ...

How to retrieve the filename from a URL using Node.js

In my Node.js script, I have the following type of link: 148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f& ...

Using JavaScript to automate keystrokes programmatically

Is there a way to programmatically close a webpage using the keyboard shortcut control + W? I'm wondering if I can write code that will mimic this specific shortcut (control+W). ...

How can the border of the select element be removed when it is active in select2

After examining the CSS code, I am still unable to locate the specific property that is being applied to the main element. I am currently customizing the select2 library to suit my needs. However, I am stuck in the CSS as I cannot determine which property ...

Ways to display JSON objects containing nested key-value pairs

In my JSON structure, each Mealplan includes a key and a corresponding value which is an object called meal. id: 1, mealsPerWeek: { Monday: { id: 4, name: "Burger", }, Tuesday: { id: 3, name: "Salad&qu ...

Create PDFs using PhantomJS when the HTML content is fully loaded and ready

I am a newcomer to utilizing phantomjs and encountering difficulties in rendering my website as a PDF file. Although I can successfully render a simple version of the page, issues arise when multiple images and web fonts are involved, causing the DOM not t ...

How to create a basic calculator in AngularJS with two textboxes specifically designed for calculating squares?

My code snippet is as follows: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <div ng-app="app"> <div ng-controller="Calcu ...

Tips for formatting the initial appearance of a class within any page or element

Is it possible to apply the style text-decoration:line-through to the initial instance of the span class="price" element within my table? The HTML structure in question is as follows: <table class="data-table" id="product-attribute-specs-table"> ...

Multiple onClick events being triggered unexpectedly upon component re-render

My react component is a form that triggers a function to handle data saving and other tasks when the send/submit button is clicked. The issue arises when the component seems to re-render multiple times after the button click, most likely due to updated ex ...

Obtain data from an HTML form and send it to PHP using AJAX

I can't seem to get this ajax POST request working properly. I've tried a few different approaches to extract user data and send it to a mock server (my php file) such as: 1. serializing the form data, 2. adding action="reserveA.php" and method ...

Dynamic Interval Update - Using jQuery or Vanilla JavaScript

In my code, I currently have two "stopwatches" (and possibly more in the future). The existing code works well, but I am looking for a way to avoid repeating it multiple times. I believe creating a function would be an ideal solution to reduce redundancy. ...

Customize the font size in Bootstrap 5 using REM units

When it comes to setting font sizes for easier calculations in REM values, I currently use this approach: html { font-size: 62.5%; } body { font-size: 1.6rem; } /* 16px */ However, Bootstrap 5 uses the root value (which is typically set at 62.5%) to calc ...