Switch up the position of the background with onmouseover interactions

I'm currently seeking assistance with a problem I've encountered while attempting to change the backgroundPosition of a block element when a specific link from a list is hovered over. I am looking for a solution that can be implemented in an external .js file, as inline JS appears messy and unprofessional in my opinion. Unfortunately, I am not comfortable using jQuery at this time and need a more complex solution than CSS-only methods allow due to separated elements.

Ideally, I would like to implement an if-else statement to achieve this. The desired functionality should look something like this:

var block = document.getElementById('block');
if (onmouseover *link1*) {
    block.style.backgroundPosition='-200px 0px';
} else if (onmouseover *link2*) {
    block.style.backgroundPosition='-400px 0px';
} else if (onmouseover *link3*) {
    block.style.backgroundPosition='0px -200px';
}

If additional code sections are required, I will gladly provide them. Thank you in advance!

EDIT: Just to clarify, I am not asking for an entire JS program, just guidance on how to set up the if statement to recognize when an element is being moused-over and adjust another element accordingly. So far, my attempts have been unsuccessful.

Answer №1

It seems like you are overcomplicating something that should be simple.

Do you just want to achieve this without using any JavaScript?

Take a look at the JSFiddle example provided: http://jsfiddle.net/ionko22/fjTUA/

Here is the basic HTML structure:

<a id="hover-me1">Link 1</a>
<a id="hover-me2">Link 2</a>
<a id="hover-me3">Link 3</a>

<div id="change-my-background"></div>

And here is the basic CSS styling:

#hover-me1:hover ~ #change-my-background{
    background:#aa3233 url(http://i.imgur.com/DMv02cc.jpg) repeat-x -200px -200px;
}

#hover-me2:hover ~ #change-my-background{
    background:#aa3233 url(http://i.imgur.com/DMv02cc.jpg) repeat-x -400px -200px;
}

#hover-me3:hover ~ #change-my-background{
    background:#aa3233 url(http://i.imgur.com/DMv02cc.jpg) repeat-x -600px -200px;
}

#change-my-background {
    width:300px;
    height:300px;
}

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

Create responsive divs that contain images, ensuring there is no margin on either the left or right sides

Utilizing server-side script, I am dynamically adding divs that display images of album covers. Below is the structure of the HTML: <div id="releases"> <div class="album"> <div class="cover"><img src="cover.jpg" /><b ...

Collapsed Navigation Bar in Bootstrap 4

I'm in the process of reorganizing my collapsed navbar, which was created using Bootstrap 4. Currently, it appears like this: https://i.sstatic.net/yxvvA.png Is there a method to align the social media buttons on one line, shift 'About' fu ...

The type 'Node' does not have the required attributes

Could anyone shed some light on what might be causing the issue described below? Your insights are greatly appreciated. The problem lies within the line of code: clone = thead.cloneNode(true); The error message reads: 'Type 'Node' is missin ...

Sort a JSON array alphabetically in Angular.js even when there are no key/value pairs specified

I'm having trouble alphabetizing a list using a JSON array in my code. Despite my efforts, the sorting doesn't seem to be working correctly. You can view my current code by following this link to the jsfiddle http://jsfiddle.net/hxxLaxL3/ Here i ...

Is the parameter retrieval method correct or erroneous?

I am looking to retrieve data by clicking a link. Here are the links available: <a class="note" id="' . $data["p_id"] . '" value="1" href="javascript:void(0)">+1</a> <a class="note" id="' . $data["p_id"] . '" value="-1" ...

What is the best way to calculate the total of all files within a folder structure using JavaScript

Here is the array I'm working with: Array (7812) 0 {foldername: "", amount_of_files: 12, children: 86} 1 {foldername: "/pm", amount_of_files: 3, children: 7} 2 {foldername: "/pm/css", amount_of_files: 1, children: 0} 3 {f ...

Recording JavaScript Cookie Visit Counts and Tracking Last Login Dates

I am a beginner in JavaScript and cookies, and I am attempting to create a cookie that can show the number of times someone has visited a website, the date of their last visit, and the expiration date of the cookie. Initially, I tried modifying code from ...

Setting an icon as a binding in a dropdown menu using Vue Js

Looking for some guidance with Vue.js and adding Font Awesome icons to dropdown items. I am a beginner with Vue.js and I'm struggling with binding icons after each dropdown item. Below is the code I am currently using: <select id="testID" ...

How can I utilize the LED flash on a Windows Phone using WinJS?

Currently delving into the world of WinJS and endeavoring to create an application that involves operating the LED Flash. I am seeking guidance on how to properly access the LED Flash functionality to allow for toggling it ON or OFF. Your insights on how ...

Unlocking the power of event binding in Vanilla JavaScript

Hey everyone, I'm relatively new to JavaScript and have some basic knowledge of jQuery. While browsing through this article on using jQuery in plain JavaScript, I came across the following example: $('a').on('click', fn); In thi ...

Preserving the chosen selection from a drop-down menu

I am working on implementing a dropdown menu with various options. My goal is to ensure that the selected option remains at the top of the dropdown even if the user navigates away from the component and then returns. Currently, when I select an option and ...

What is the process for combining and compressing an Angular 2 application?

I am currently trying to concatenate and minify an angular2 application. My approach so far involved concatenating all my *.js files (boot.js, application.js then all components) into one file and injecting it into my index.html. I also removed the <s ...

Discover the maximum length of an element in an array using JavaScript

I am trying to determine the length of the longest string in a given array. If the array is empty, the function should return 0. Here is my attempted solution: function getLengthOfLongestElement(arr) { var longestLength = 0; for(var i=0; i< arr.le ...

Assistance required for activating an unidentified function or plugin within a Chrome extension

I am currently working on a project involving a chrome extension designed to automate tasks on a specific website. My main focus right now is trying to trigger the click event of a link that has an event handler set up through an anonymous function as show ...

What's better in React: using pure components or non-pure components? Is it okay to fetch data in componentDidMount or

Exploring React in Meteor has led me to observe two distinct approaches... Take the meteor leaderboard example, where a list of players displays their names and scores. The pure approach involves fetching all players and passing them into the playersList ...

An introduction to integrating Paged.js with Vue.js3

index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" /> < ...

Error message triggered by custom ajax request in C#

I need to change the default xhr.responseText error message in C# to a custom one called cSharpErrorMessage. How can I pass this custom error message to the ajax error? Below is my C# code snippet: [WebMethod] [ScriptMethod(ResponseFormat ...

Tips for setting up a Vue application (using vue-cli) to include nonce attributes in created script tags

My vue application, compiled using vue-cli, has a vue.config.js file structured as follows: 'use strict'; module.exports = { publicPath: `${process.env.CDN_URL || ''}/dist/`, lintOnSave: true, transpileDependencies: [], outputD ...

Prevent the ReactJS submit button from being activated if the text area contains any missp

I've been working with ReactJS and recently designed a form. One of the requirements is to disable the send button if the text area in the form contains spelling errors. I attempted using a spell check feature, however, it only underlines the errors ...

Javascript SQL query not returning the expected multiple rows, only returning one instead

I have a table containing multiple rows of test results, each row includes the ID of the test taker. Using the logged-in user information, I've added additional rows to the table to ensure accuracy. However, when printing the result to the console, on ...