JavaScript display attribute malfunctioning

In this scenario, the team_hover() function is triggered by an onmouseover event, and the team_hover_bbye() function is triggered by an onmouseout event. While team_hover() works as expected, the display property of gl2 does not change to "none" when I move my mouse away from the element.

My goal is to reset the display property of gl2 back to "none" when the mouse is removed.

(Please note that this is my first post)

function team_hover() {
  var a = document.getElementById("gl1");
  var b = document.getElementById("gl2");
  var myVar = setInterval(opac, 20);
  var i = 0;

  function opac() {
    i = i + 0.1;
    a.style.opacity = 1 - i;
    if (i >= 1) {
      a.style.display = "none";
      b.style.display = "block";
    } else {}
  }
}

function team_hover_bbye() {
  var i = document.getElementById("gl1");
  var j = document.getElementById("gl2");
  i.style.opacity = 1;
  i.style.display = "block";
  j.style.display = "none";
}

team_hover_bbye();
#gl1, #gl2 { display: inline-block; width: 2em; height: 2em; margin: 0.5em; }
#gl1 { background: #F00; }
#gl2 { background: #0F0; }
<div id="gl1" onmouseover="team_hover()"></div>
<div id="gl2" onmouseover="team_hover()"></div>

Answer №1

Your issue is with the infinite scrolling interval and incorrect event usage.

Consider the following solution:

var myVar;

function team_hover() {
  var a = document.getElementById("gl1");
  var b = document.getElementById("gl2");
  clearInterval(myVar); // Added here
  myVar = setInterval(opac, 20);
  var i = 0;

  function opac() {
    i = i + 0.1;
    a.style.opacity = 1 - i;
    if (i >= 1) {
      a.style.display = "none";
      b.style.display = "block";

      clearInterval(myVar); // Improved performance
    } else {}
  }
}

function team_hover_bbye() {
  clearInterval(myVar); // Corrected fix
  var i = document.getElementById("gl1");
  var j = document.getElementById("gl2");
  i.style.opacity = 1;
  i.style.display = "block";
  j.style.display = "none";
}

team_hover_bbye();
#gl1, #gl2 { display: inline-block; width: 2em; height: 2em; margin: 0.5em; }
#gl1 { background: #F00; }
#gl2 { background: #0F0; }
<div id="gl1" onmouseenter="team_hover()"></div>
<div id="gl2" onmouseout="team_hover_bbye()"></div>

It's important to note that achieving what you want can also be done using CSS.

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

Avoid Repeating an Animation with Jquery

When you continuously press a button that triggers a function with animation, the function is called multiple times and the animation plays one after another. How can you prevent the function from being triggered while the animation is still in progress? ...

In JavaScript, the event.defaultPrevented property will never be set to true

I've been experimenting with events in my script, but I'm struggling to understand the functionality of event.preventDefault()/event.defaultPrevented: var e = new Event('test'); e.defaultPrevented; > false e.preventDefault(); e.d ...

What is the best method to transfer information between main.js and a specific directory?

Is there a way to efficiently pass data between the main and directory components? I would like to automatically activate the directive when main.js loads. Directive code: angular.module('dmv.shared.components'). directive('doImportPackag ...

Sorting Vue.js properties based on object keys

Currently, I am dealing with a complex object that contains multiple network interfaces. Each interface is represented by a key-value pair in the object: https://i.stack.imgur.com/klkhH.png My goal is to create a Vue.js computed property that filters thi ...

Using React to trigger a child component's function upon clicking a tab

I need assistance with creating a React app that includes two tabs (Tab1, Tab2). When Tab2 is clicked, I want a message to appear in the console saying 'Function A is called'. Unfortunately, the code I have currently does not display the message ...

I am interested in rotating the yAxes scaleLabel in Angular using Chart.js

I have a vertical chart that I want to rotate horizontally. Unfortunately, there are no maxRotation and minRotation parameters for the yAxes, so I'm unsure of how to achieve this rotation. Although I found a similar question, I struggled to implement ...

The tweet button is not displaying correctly on the website

Visit my website here, where I have integrated a tweet button generated from Twitter.com. It was working fine for the initial few posts, but now it is failing to load and only displaying text. I have checked the console for any JavaScript errors, but so f ...

Responsive div that reacts to scrolling

I am looking for a div that can dynamically change its position as I scroll up or down the page. For example, it could start 30px from the top but then move to 100px as I scroll down further. This div will be located on the right-hand side of my page, clo ...

PHP: Using Jquery or Ajax, learn the technique of initializing a variable first and then using it in PHP

Currently, I am working on an HTML file within a PHP environment. My goal is to display the value of $count in the HTML using a for each loop later on. How can I achieve this? Here is the code snippet: <div>Total class: <span>$count</span&g ...

Google-play-scraper encounters an unhandled promise rejection

I'm trying to use the google-play-scraper library with Node.js, but I keep encountering an error when passing a variable as the 'appId'. How can I resolve this issue? Example that works: var gplay = require('google-play-scraper') ...

Choose the Nth option in the dropdown list using programming

Currently, I have a script that dynamically populates a select box with unknown values and quantities of items. I am looking to create another script that mimics the action of selecting the Nth item in that box. Despite my research, I have been unable to ...

"Enhance search functionality by integrating live search results with clickable hyperlinks

I am currently working on implementing a live search feature in Laravel 7. Everything is functioning correctly, however, I am facing an issue where I am unable to add a (href a tag) to the result list. The results are being displayed in a select option usi ...

React.js and Visual Studio Code have recently been causing unexpected and massive "Module not found" errors

Everything was going smoothly with my project until I uploaded it to Github and then cloned it. Suddenly, I started encountering various "Module not found: Can't resolve..." import errors. For example: Module not found: Can't resolve './co ...

Step-by-step guide on setting up a click counter that securely stores data in a text file, even after the

Can anyone help me make this link actually function as intended? Right now it only runs the JavaScript code, but I would like it to run the code and redirect to a webpage. Additionally, I need the data to be saved to a text file. Please provide assistanc ...

Preventing the selection of 'None selected' upon dropdown change

When using the NameTextBox Dropdown, all names are available for selection. Upon changing a name and clicking on search, the associated details are retrieved. However, by default, 'None Selected' is displayed. Is there a way to prevent 'None ...

The scrolling speed of my news div is currently slow, and I am looking to increase its

This is the news div with bottom to top scrolling, but it is slow to start scrolling. I want to increase the speed. The current behavior is the div appears from the y-axis of the system, but I want it to start exactly where I define. The scrolling div is ...

Creating a table-style format from a JSON string fetched via AJAX: a step-by-step guide

Hi, I recently started learning web programming and I've been facing some challenges with using AJAX to parse data as a string and display it in a table. [{"source":"1","name":"random","amount":"5"},{"source":"1","name":"random","amount":"5"}] After ...

Implementing Title Attribute in Grid View Template Field

I have implemented a Grid View with a "TemplateField" that includes properties for Header Text and SortExpression set to true. Upon inspecting the browser, I noticed that it generates an anchor element with some JavaScript. How can I add a title tag to t ...

Angular.js - organizing a list of items and preserving the outcome

Here is a compilation of randomly arranged items: <ul class="one" drag-drop="page.items"> <li ng-repeat='item in page.items|orderBy:page.random as result'> <img ng-src="http://placecage.com/{{item.id*100}}/{{item.id*100}}"& ...

In certain situations, Chrome and Safari fail to trigger the unload function

Struggling with a persistent issue lately and really in need of some assistance. My goal is to perform a server-side callback to clear certain objects when the user navigates away from our page, without needing to click logout. Due to business requirements ...