Manipulating the visibility and opacity of a div element using CSS

Here is the code snippet I am working with:

<div class="contact-item"
    onmouseover="div_ContactItem_mouseover(this)">
</div>

<div id="div_ItemOver" 
 style="display: none;"
 class="contact-item-hover" 
 onmouseout="div_ItemOver_mouseout(this)">

 <div style="color: red; opacity: 1; width: 20px; height: 20px;">link1</div>
 <div style="color: red; opacity: 1; width: 20px; height: 20px;">link1</div>
 <div style="color: red; opacity: 1; width: 20px; height: 20px;">link1</div>
</div>

Additionally, I have implemented the following JavaScript functions:

function div_ContactItem_mouseover(e) {
 $("#div_ItemOver").show().offset($(e).offset());
};

function div_ItemOver_mouseout(e) {        
 $("#div_ItemOver").hide();
};

And applied these CSS styles:

.contact-item, .contact-item-hover {

 cursor: pointer;
 display: inline-block;
 border-radius: 5px; 
 margin: 0px 3px 3px 0px; 
 width: 340px; 
 height: 90px;
 border: 1px solid #244f56; 
 background-color: #f8f8f8;
}

.contact-item-hover {

 background-color: #000000;
 position: absolute;
 opacity:0.12;
 filter:alpha(opacity=12);
}

My objective is to display the second div with id div_ItemOver when hovering over the first div with the contact-item css class. Additionally, ensure that the div_ItemOver has an opacity of 0.12.

The issues I am facing are:

  1. When moving the mouse over the links within the div_ItemOver, there is a flashing effect due to mouseout and mouseover events occurring. How can this be resolved to prevent the flashing?

  2. I want to remove the opacity reflection on the links within the second div by setting their opacity to 1 using an inline style, but it doesn't work as expected. What is the correct way to show those links without any opacity and completely clear?

You can view the issue in action on my jsFiddle: http://jsfiddle.net/am1r_5h/1zrytzjw/2/

To replicate the problem, hover over the rectangle and then move to the links on the top left corner.

Answer №1

To eliminate flickering, assign the same handlers to both divs:

Check out this example on JsFiddle

<div id="div_ItemOver" style="display: none;" class="contact-item-hover" onmouseover="div_ContactItem_mouseover(this)" onmouseout="div_ItemOver_mouseout(this)">
...
</div>

<div class="contact-item"  onmouseover="div_ContactItem_mouseover(this)" onmouseout="div_ItemOver_mouseout(this)"></div>

By adding console.logs to your original fiddle, you can observe that mouseout events occur every time a link is hovered over, leading to the flickering effect.

Answer №2

Firstly, let's address the issue of event bubbling when a child element is triggered and affects the parent element automatically. To prevent flickering, you can simply include e.stopPropagation(); at the start of your div_ItemOver_mouseout() function. This modification will ensure smooth behavior as shown below:

function div_ItemOver_mouseout(e) {        
   e.stopPropagation();
   $("#div_ItemOver").hide();
};

Secondly, consider replacing the opacity/filter setting with

background-color: rgba(R, G, B, A);
format to achieve the desired effects in your CSS. By adjusting the styles as mentioned below, your elements will be displayed as intended:

.contact-item-hover {
    background-color:rgba(240, 240, 240, 0.25);
    position: absolute;
}

.contact-item-hover > div{
   background-color:rgba(240, 240, 240, 0.85);
   width:50px;
   height:20px;
   color:red;
}

The selector .contact-item-hover > div specifically targets your links, eliminating the need for inline styles on each individual link.

Answer №3

1) I have updated the Javascript mouseover/mouseout functions to now use jQuery .hover(), which allows for mouseon and mouseout arguments http://api.jquery.com/hover/. The flickering effect has been eliminated.

2) If you need to adjust the level of opacity, you can do so in the .css() events for both mouseon and mouseout, as well as in the initial opacity setting within the CSS style.

 $("#div_ItemOver").css({"opacity":"1"})

I specifically targeted #div_ItemOver in the hover event to ensure that the opacity change remains when hovering over this section.

$(".contact-item, #div_ItemOver").hover()

In addition, I made some other modifications:

  • I consolidated your styling into CSS to eliminate redundancy
  • I utilized Position and Float properties to position your links above the contact-item DIV

You can view the updated version on jsfiddle http://jsfiddle.net/1zrytzjw/8/

Answer №4

  1. To solve this issue, simply add event handlers for both mouseover and mouseout to the div elements.
  2. Another solution is to replace background-color: #000000; with background: rgba(0, 0, 0, 0.12); and opacity:0.12;

http://jsfiddle.net/1zrytzjw/10/

Answer №5

To solve this issue, try nesting the hidden div within the initial div. This way, the hidden div is enclosed within the first div. Simply move the closing div tag of the first div to the end.

The flashing may occur because when the mouse encounters the "link" divs, the mouseover state breaks and hides the div containing the links. However, since the mouse remains in the same area, it quickly triggers a mouseover event on the first div, revealing the links again. By placing the hidden div inside the main div, the links become part of the same element affected by the mouseover, ensuring that the links remain visible during interactions. I hope this explanation clarifies things for you.

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

The error message "Type 'string | number' is not assignable to type 'number'" indicates a type mismatch in the code, where a value can be either

I encountered an error code while working with AngularJS to create a countdown timer. Can someone please assist me? //Rounding the remainders obtained above to the nearest whole number intervalinsecond = (intervalinsecond < 10) ? "0" + intervalinseco ...

Developing pagination functionality in ReactJS

I came across this piece of code in a forum (how to implement Pagination in reactJs): constructor() { super(); this.state = { todos: ['a','b','c','d','e','f','g','h ...

Searching for repeated values across disparate object fields?

Inquiring about the method to utilize mongoose for identifying duplicate values across different fields. Providing a sample document for reference: { "followers": { { "_id": "5bf6d610d3a3f31a6c75a9f4" }, ...

Angular SPA boasting an impressive one million pages

Currently, I am in the process of transitioning my webshop, which contains numerous products, to an Angular SPA application using ngRoute and HTML5 mode. I have come up with a method where I receive some of my routes from the server as a JSON object, but s ...

Displaying a component in a router view based on specific conditions

Currently, I am delving into the world of Vue3 as a newcomer to VueJS. In my project, there is an App.vue component that serves as the default for rendering all components. However, I have a Login.vue component and I want to exclude the section when rende ...

Having trouble accessing JSON response properties within an Angular.js controller

My Angular.js controller is called 'forecastController'. This is the code for the Angular.js Controller: weatherApp.controller('forecastController', ['$scope','$routeParams','cityService', 'weat ...

Calculating the length of an array in Vue/Nuxt using a custom plugin: Watch vs Computed

I am trying to show the length of an array within my Vue component. The array is nested within a Vue plugin. Each time I initiate an API Call, I add a new element to this array located here: this.$apiCall.apiCallCache Is it possible to monitor changes i ...

What is the best way to connect an image to a different webpage?

I have a question regarding a div that contains an image acting as a link to another page. The image has text overlaying it and can be found at this link: This code was sourced from the internet, but I made some modifications to it. My goal is simply to ...

Transferring the dirty state of the view to the parent form

Within my main form's markup, there is a specific structure that includes a tabset and a selectView method in the controller: <tabset vertical="true" type="pills"> <tab ng-repeat="tab in tabsViews" sele ...

Can the server-side manipulate the browser's address bar?

Picture this scenario: a public display showcasing a browser viewing a web page. Can you send a GET or POST request from a mobile device to an HTTP server, causing an AJAX/pubsub/websocket JavaScript function to alter the displayed page on the screen? Per ...

"Managing output buffers, styling with CSS, and sending emails with PHP

I am currently using a script to send emails that result in a couple of HTML tables: $from = "example.com <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82efebe7ef83898ce0ede1e9e3e6d2edeee8">[email protected]</a ...

Is it possible to nest an HTML <span> inside a label tag in a Rails form_for?

Is it possible to nest a span element inside a form_for label tag? I am trying to achieve this in order to style a specific part of the label using CSS, such as making the text red. While it seems like valid HTML based on my research, it is causing some is ...

Utilize jQuery to access the text content of the desired element

Looking to extract the text from a targeted element within an unordered list <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> using this code snippet $('ul').click(function() { th ...

Mismatched data types for function arguments

const x: Example = { toY: (y: Maple) => { return y.p; } }; interface Example { toY: (y: Pine) => void; } interface Pine { c: string; } interface Maple extends Pine { p: boolean; } Despite the warning for interface names ...

Counting each item with jQuery and assigning them numbers 02, 03, 04, etc., with the exception of the first item which will display as "Up Next

I'm still learning jQuery and here's the code I've put together after researching on stackoverflow and other platforms: var counter = 1; $('.next-page .nav-item').each(function () { if ($(this, ':gt(0)')) { $(this ...

What is the method for creating a checkbox in CSS that includes a minus symbol inside of it?

Can you create a checkbox with a minus "-" symbol inside using just html and css? I attempted to achieve this with JavaScript by using: document.getElementsByTagName("input")[0].indeterminate = true; However, the requirement is to accomplish this using ...

Updating the DOM does not occur by simply adding an object to the Array; instead, the database is updated once the data has

My database has verified data that is being updated, however, the DOM is not reflecting these updates. <ul> <li ng-repeat="aReview in reviewList"> .... .... </li> </ul> <script> if(globalMethods.stringVa ...

The code "Grunt server" was not recognized as a valid command in the

I recently set up Grunt in my project directory with the following command: npm install grunt However, when I tried to run Grunt server in my project directory, it returned a "command not found" error. Raj$ grunt server -bash: grunt: command not found ...

What are the steps for implementing a two-column layout in reveal.js?

When creating my slides using reveal.js, I write them in Markdown code. I am now looking to present my content (text, bulleted lists, or images) in a traditional two-column text layout. While I am open to a more complex initial setup, I want to ensure that ...

Verifying Dates with Bootstrap DatePicker

My bootstrap datepicker has a startDate value of '1/1/1990', but when a user manually enters a date like '1/1/201' (possibly a typo for '1/1/2014'), the date field becomes blank when they move to a different section. Is there ...