Is there a way to verify if an element possesses a specific style, and if so, alter the style of a different element accordingly?

Could you assist me in achieving a similar effect as demonstrated below:

I have two menus (http://osiyo-nur.uz/osiyo-nur.uz/test6/), one of which is initially invisible. When I hover over the first menu, the second menu becomes visible with an overlay effect on the background. Instead of traditional menus, I am using buttons for this purpose. I attempted to use a condition but it did not yield the desired result. Your help in resolving this would be greatly appreciated.

$(document).ready(function() {
  if ($(".none").css('display') == 'block') {
    $("#overlay").css('display', 'block');
  } else {
    $("#overlay").css('display', 'none');
  }

});
.none {
  display: none;
  width: 60%;
  background: red;
  color: #fff;
  text-align: center;
}

.block {
  display: block;
  width: 60%;
  text-align: center;
  background: red;
  color: #fff;
  margin-bottom: 5rem;
}

.block:hover+.none {
  display: block;
}

.block:hover .overlay {
  display: block;
}

.overlay {
  height: 100%;
  width: 100%;
  position: fixed;
  z-index: 999;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: block;
  background-color: rgba(0, 0, 0, 0.6);
  overflow-x: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>GoodGross</title>

  <link href="css\style.css" rel="stylesheet">
</head>

<body>
  <div>
    <!-- HEADER PART STARTS -->
    <div class="main_content">
      <div class="test">
        <a class="btn btn-danger block">Block</a>
        <a class="btn btn-danger none">None</a>
      </div>
    </div>
    <div class="overlay" id="overlay"></div>
    <div class="clear"></div>

  </div>

</body>

</html>

Answer №1

Give this code a whirl:

 $(document).ready(function() {
    $('.block').hover(function(){
        $('.overlay').css('display', 'block');
        $('.none').css('display', 'block');
    });
    $('.none').hover(function(){
        $('.overlay').css('display', 'none');
        $('.none').css('display', 'none');
    });

});

Make sure to incorporate the following CSS into your stylesheet as well:

/*Additional CSS*/
.none{
    position: relative;
    z-index: 9999;
}

Answer №2

using this

$('.block').on('mouseover', function() {
$("#overlay").css('display', 'block');
});

$('.block').on('mouseleave', function() {
$("#overlay").css('display', 'none');
});

$('.block').on('mouseover', function() {
$("#overlay").css('display', 'block');
});

$('.block').on('mouseleave', function() {
$("#overlay").css('display', 'none');
});
.none {
  display: none;
  width: 60%;
  background: red;
  color: #fff;
  text-align: center;
}

.block {
  display: block;
  width: 60%;
  text-align: center;
  background: red;
  color: #fff;
  margin-bottom: 5rem;
}

.block:hover+.none {
  display: block;
}

.block:hover .overlay {
  display: block;
}

.overlay {
  height: 100%;
  width: 100%;
  position: fixed;
  z-index: 999;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: none;
  background-color: rgba(0, 0, 0, 0.6);
  overflow-x: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <title>GoodGross</title>

  <link href="css\style.css" rel="stylesheet">
</head>

<body>
  <div>
    <!-- HEADER PART STARTS -->
    <div class="main_content">
      <div class="test">
        <a class="btn btn-danger block">Block</a>
        <a class="btn btn-danger none">None</a>
      </div>
    </div>
    <div class="overlay" id="overlay"></div>
    <div class="clear"></div>

  </div>

</body>

</html>

Answer №3

Implementing the jquery hover() method in your code can enhance user interaction:

$(document).ready(function() {
  $("a.link").hover(function() {
    $("#overlay").css('display', 'block');
  }, function() {
    $("#overlay").css('display', 'none');
  });
});

To see a demonstration, visit the following link: https://example.com/demo

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

"Encountered a problem when trying to access file with node

An error has occurred: module.js:471 throw err; ^ Error: Module not found at '/Users/vinclo/app.js' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.runMain (module.js:604:10) at run ( ...

I am encountering challenges and confusion with CSS

I am encountering an issue with my code that is causing some difficulties. The goal is to create a basic div with left, right, and center panes. The left pane loads perfectly, but the right pane, which has the same css styling, displays its children with a ...

Utilizing the data sent to a modal to enhance the functionality of the code by combining it with the src attribute of an <embed> element

I am trying to pass a dynamic string of data to a Bootstrap modal using the data-val attribute in an HTML table. <td <button type="button" class="btn btn-success btn-xs" data-val="<?php echo $row['order_id']; ?&g ...

Vue paired with Rainyday.js

I attempted to incorporate Vue with rainyday.js by following different resources, but unfortunately could not find any relevant information. Can anyone provide guidance on how to successfully implement rainyday.js with Vue? <body onload="run();"> ...

When I increase the length of my website's header and footer, I notice that the data in the footer is only displayed on the homepage and not on the other

I am facing an issue with extending my header and footer in Django. The problem is that when I pass data to the footer, it only appears on the home page and not on other pages. I understand that I need to pass the data to every page individually, but I am ...

Depending on external software packages

Can you explain the potential risks associated with using third-party packages and npm in a broader sense? If I were to install a third-party package like semantic-ui-react using npm, is there a possibility that I may not be able to use it on my website i ...

The animation speed of the jQuery hashchange event is set to zero, causing the animation

I'm facing an issue with jQuery where my animation inside a hashchange event is not smooth; it happens instantly when triggered. I'm looking for a way to make the animation smoother. jQuery( document ).ready(function() { jQuery(window).on(&a ...

Adding JSON data to an array for Flot Diagram

I've been struggling with the task of organizing data into an array. The existing temp array is structured as follows: tmp[0][0] = "NY" tmp[0][1] = "52" tmp[1][0] = "FL" tmp[1][1] = "25" My goal is to transfer this data into a new array called data. ...

Utilizing jQuery to display only one element from a given pair

Managing a set of message elements that come in pairs and should show or hide opposite each other can be quite tedious. While I have managed to make it work with jQuery, the code seems to be quite verbose: if (conditionA) { $("#a1").show(); $("#a2 ...

Updating the status of a 2D array with N elements in React: A step-by-step guide

Before I dive into the topic, I must acknowledge that I have come across questions similar to this one before but was unable to find a solution on my own. Updating React state as a 2d array? Let's imagine this as my state object state = { graph ...

WordPress Ignoring PHP Generated Elements When Processing Divs

Whenever I attempt to utilize the get_date function, it seems to disregard all div elements and instead places itself right after the header, at the beginning of the <div class="entry-content">. <div class="entry-content"> May 16, ...

Error: The AJAX request encountered an unexpected token in the JSON response

I am working with the following code snippet: $.ajax({ dataType: 'text', url: '/_/js/answers.json', type: "GET", success: function (data) { alert(data); ...

Tips for passing a JavaScript array to an MVC controller

In my asp.net application, I have implemented a functionality where users can order food. Additionally, there is an admin page where admins can login and create a menu for a specific week. I have successfully developed the "Menu maker" feature where all me ...

Tips for customizing the selected and hover color of ListItem in Material UI

Having trouble getting the 'selected' or 'hover' colors to work for the ListItem component. I've tried setting its classes like this: <ListItem selected button key="home" classes={{ selected: classes.listItemSelected } ...

If the element contains a specific class, then remove that class and apply a new

Encountering an issue here. I have 4 HTML elements structured like this: <!-- Light Color Scheme - Header False and Show Faces True --> <div class="fb-like-box h-f_dsf-t visible" data-href="http://www.facebook.com/pages/Alpine-Adaptiv ...

Using jQuery to create transitions when a class is changed and adding a delay to the transition

If you want to check out the section I created on CodePen, feel free to visit it by clicking here. I've noticed that my JavaScript code has a lot of repetitive elements and I'm looking to optimize it for better performance. Any advice on how I c ...

When would using <style> be more appropriate than using a css file?

Is it necessary to use the style element when you can simply write in the CSS file? Are there any circumstances where using 'style' is more beneficial than the CSS file? ...

Exciting jQuery animations and transitions

Is there a way in JavaScript to call function or method names using a string? For example, when writing jQuery code that applies an effect to specific targets, can the effect be dynamic and changeable by the user? I'm hoping for something like jQuer ...

The 'resp' parameter is assumed to have an unspecified type, shown as 'any'. This is an error in Angular

} ErrorHandler(response){ console.debug(response.json()); } DataHandler(response){ this.ClientModels = response.json(); } I have developed two functions to handle error and success responses, but an error message is showing up saying "para ...

Issue with iOS iframe scrolling - unable to scroll as expected

On iOS devices like the iPad and iPhone 6, scrolling doesn't seem to work as intended. Instead of the iframe scrolling, it's the 'body' that is moving. Javascript $(document).on(clickHandler, '#content a', function(){ href ...