Display or conceal several elements upon hover using JQuery and HTML

Here is the current progress I have made:

<div style = "position: relative;">
<a href = "#games">
<div class="sidenavOff">
<img src = "images/card_normal.png" />
<img src = "images/category_icons/icon_games.png" style = "position: absolute; top: 10px; left: 40px;" />
<img src = "images/category_titles/title_games.png" style = "position: absolute; top: 160px; left: 40px;" />
</div>
<div class = "sidenavOver">
<img src = "images/hover/card_hover.png" />
<img src = "images/category_titles/title_games.png" style = "position: absolute; top: 10px; left: 40px;" />
<img src = "images/hover/card_hover_separator.png" style = "position: absolute; top: 40px; left: 40px;" />
Show various text content here
<img src = "images/button_start_normal.png" style = "position: absolute; top: 200px; left: 40px;" />
/div>
</a>
</div>

The setup involves a notecard image (card_normal.png) with multiple transparent images overlaying it. When the mouse hovers over the card, icon Games and title Games are displayed on it. The goal is to show additional elements like the title_games.png, a description, and button_start_normal.png in vertical order when hovering over different areas of the card.

Below is the jQuery code snippet attempting to achieve this effect:

$(document).ready(function() {
$(“div.sidenavOff”).mouseover(function(){
$(this).removeClass().addClass(“sidenavOver”);
}).mouseout(function(){
$(this).removeClass().addClass(“sidenavOff”);
});
});

Here is how it looks without hover:

And with hover effect applied:

Answer №1

This is the jquery script that I have been working on [...]. It's a bit confusing to me

$(document).ready(function () {/* code for the function */});

Once the document (represented by jQuery object $) is fully loaded and ready, execute a specific function as defined in /* code for the function */

$("div.sidenavOff")

Utilize jQuery $ to select all matching HTMLElements identified by the CSS Selector div.sidenavOff

.mouseover(function () {
    $(this).removeClass().addClass("sidenavOver");
})

Upon mouseover event (mouseover), remove the current class from the element (referred to as this) and replace it with sidenavOver

.mouseout(function () {
    $(this).removeClass().addClass("sidenavOff");
})

When the mouse exits an element (mouseout), similar action occurs as with mouseover, but now adding sidenavOff class back to that particular element.


You're on the right track, here's a suggested modification for improved clarity:

$(document).ready(function () {
    $("div.sidenavOff").mouseover(function () { // introducing visibility control
        $("div.sidenavOver").addClass("showme"); // apply "showme" to div.sidenavOver
    }).mouseout(function () { // end of visibility setting
        $("div.sidenavOver").removeClass("showme"); // remove "showme" from div.sidenavOver
    });
});

In this case, the class showme corresponds to certain CSS rules enabling the display of the element

.showme {display: block;}

Answer №2

It seems like the goal is to toggle the visibility of the "sidenavOver" div when hovering over the container. Is my understanding correct?

I have modified your HTML code in this jsfiddle by adding a class to the container div and commenting out the missing images. http://jsfiddle.net/joshvito/GaZQ6/

For example:

//on hover over the container; you can use the a tag too, whichever element you want to bind the event to 
$('.container').on({
    mouseenter: function () {
        $(".sidenavOff").hide(); //On mouseover, hide the first div
        $(".sidenavOver").show();
    },
    mouseleave: function () {
        $(".sidenavOff").show();
        $(".sidenavOver").hide();
    }
});

A helpful resource for jQuery event handlers can be found at JQUERY API Documentation for .on

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 is the best method to extract information from JavaScript tables using Python and Selenium?

As a beginner in Python, JavaScript, and Web-Scraping, I am facing the challenge of extracting data from tables on a specific webpage (https://www.mcmaster.com/cam-lock-fittings/material~aluminum/) and saving it into a csv file. https://i.sstatic.net/xQBO ...

Utilizing Redux in my ASP.NET MVC application with the help of RequireJS and TypeScript (Issue: Encountering a script error for "redux" dependency)

I'm currently working on integrating the Redux state library with my ASP.NET MVC application using TypeScript and RequireJS. For this simple application, I have set up a new ASP.NET MVC Project and stripped it down to only include the necessary node ...

Guide to invoking an HTML document through PHP

I'm currently in the process of developing an administrator login page using a combination of HTML and PHP, with PHP serving various other functions as well. Once the administrator successfully logs in, I need to execute an HTML file. Below, you&apos ...

Sending POST Requests with Next.js Version 14

How can I send a POST request using axios in Next.js 14, I prefer axios but fetch is also okay. I have been getting an AxiosError: Network Error when I try to use axios and TypeError: fetch failed when using fetch. However, it works fine with Postman. I ...

The functionality of CSS transform appears to be malfunctioning on Firefox browser

My website, located at , features a logo that is centered within a compressed header. I achieved the centering effect using the following CSS command: .html_header_top.html_logo_center .logo { transform: translate(-63%, -2%); } This setup works perfe ...

A guide on transforming a JSON object representing an HTML element into a live HTML element for display on a webpage using C#, JavaScript, or jQuery

I am looking to retrieve a JSON object from a database and convert it into HTML format. Here is an example of the JSON structure: {"input":{ "name":"fname", "type":"text", "placeholder":"Ente ...

How to extract hrefs within <li> tags from a <ul> element with Cheerio

I'm facing some difficulties with this question, so I need your help. What I want to achieve is to extract the hrefs from the HTML provided below. <ul id="nav-products"> <li><a class="" href="/shop/hats/">yellow good looking ha ...

Testing the selection of dropdown values in Angular2 using unit tests

I have a dropdown menu for selecting countries. It defaults to a pre-selected value, and when the user changes it, they are redirected to the corresponding country page. However, I am facing an issue while trying to unit test the default value in the sele ...

Is Eslint functioning in just one Sublime Text project?

I have encountered an issue where I duplicated the .eslintrc and package.json files for two projects, but only the first project is able to run linting properly. The second project does not show any errors. I am using sublime-linter with the eslint modul ...

Side Menu with Fixed Position Created with Tailwind CSS

Can anyone help me create a social media sidebar that stays fixed on the bottom right side of the screen, regardless of its size? Currently, the sidebar moves when I scroll down, but I want it to remain in place. How can I achieve this fixed positioning? ...

Is it possible to stack CSS pseudo-elements?

What is the most efficient way to create nested square-elements with minimal markup? I am aware that :after/:before cannot be stacked multiple times on the same element, so what would be the best way to achieve a structure like this? I am also facing chal ...

Is it necessary for every single product to have its very own webpage?

As a newcomer to the world of web development, I'm uncertain about the standard practices. Currently, I am working on creating a small e-commerce website with a wide range of products. Is it recommended to give each item its own dedicated page? Furthe ...

Secret icon revealing on mouseover

I devised a simple technique to showcase an element overlapping another, like an overlay, and it was functioning flawlessly until the point where I needed to incorporate a "button" (styled as a Bootstrap anchor) beneath it. Here is the initial setup and h ...

Adding negative values to the Tailwind CSS utility plugin is a simple process that can greatly enhance

Adding Negative Values to Tailwind CSS Utility Plugin Quick Summary: I've developed a custom Tailwind utility plugin that includes numeric values. I'm looking for a way to introduce negative numbers by adding a - at the start of the class, simi ...

Generate an Array reference using the index of elements instead of duplicating the values

When initializing b, it appears that the array data is being copied from a instead of referencing it, as originally intended: let a = [0,1]; let b = [a[0], 2]; a[0]=3; console.log(b); The resulting output is 0,2. What is the reason for the output not ...

Convert the class to a file upload using jQuery

I am currently working on a project involving a file upload script. I have multiple directories where the files need to be uploaded, and I'm attempting to use a single form for this purpose. So far, I have been able to change the form class using jQu ...

Learn how to dynamically chain where conditions in Firebase without prior knowledge of how many conditions will be added

Currently, I am working on a project using Angular and firebase. My goal is to develop a function that can take two arguments - a string and an object, then return an Observable containing filtered data based on the key-value pairs in the object for a spe ...

Ways to display HTML elements depending on the width of the window

Is there a way to create visually appealing hexagon containers for text that work well across all window widths? Currently, the design is only satisfactory at certain window sizes. I am looking to implement a solution where specific code will be displayed ...

Tips for receiving alerts on external updates to a mongo collection using sails

Currently, I am in the process of creating an application utilizing mongo and sails. As part of my development, I am investigating how the real-time update feature in sails functions. Although I am currently using sails 0.9.16, I am also curious about ans ...

jQuery Carousel: Adjusting the Displayed Items

I've been experimenting with the jQuery Roundabout plugin and I'm curious if there's a way to control the number of images displayed. I have a total of ten pictures, but I only want three of them visible at a time, similar to what is shown i ...