Is it possible to deactivate the CSS hover effect using JavaScript?

There is a parent element with an associated child element.

Right now, the child element is hidden and only appears when hovering over the parent. It vanishes when the mouse moves away from the parent due to css settings.

The goal is for the child element to remain visible even if the mouse pointer leaves the parent area, but only if the mouse button is pressed down. The hover effect should be disabled if the mouse button is clicked down and re-enabled once it is released.

This functionality is needed for a draggable feature where the handle (child element) must remain in view when dragging.

Please note that this has to be accomplished without using jquery or other external libraries.

Answer №1

UPDATE: Oops... I missed the "without jquery" requirement... Apologies for that. I have kept my jQuery solution just in case and provided a non-jQuery alternative below.

To enable hover effect using CSS only, you can add a specific class like this:

.parentElement.hoverCSS:hover {
    .childElement {
        display: none;
    }
}

For achieving the same functionality without jQuery, follow these steps:

document.querySelector(".parentElement").addEventListener("mousedown", function() {
    document.querySelector(".parentElement").classList.remove("hoverCSS");
});

document.querySelector(".parentElement").addEventListener("mouseup", function() {
    document.querySelector(".parentElement").classList.add("hoverCSS");
});

This code removes the 'hoverCSS' class on mouse down and adds it back on mouse up to control the visibility of the child element on hover of the parent.

WITHOUT JQUERY ALTERNATIVE

If you want to achieve the hover effect without any JavaScript, you can convert your parent element into a button and use the :active pseudo-class in CSS like this:

.parentElement.hoverCSS:hover {
    .childElement {
        display: none;
    }
}

.parentElement.hoverCSS:hover:active {
    .childElement {
        display: block;
    }
}

By making the button active when clicked, the child element will be displayed until the button is released.

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

Do I need to recompile anything after making changes to a node module in my vue.js project in order for the modifications to take effect? The updates are not appearing as expected

Currently tackling a Vue.js project and came across an issue with the Bootstrap-vue module that I would like to remove. After locating the section in the module responsible for this behavior, I commented it out. But strangely, the changes are not showing ...

Mastering the Art of Trimming with Jquery

function DisplayDataForEdit(id) { $("#editContainer").slideDown("medium"); var parentId ='item'+ id; var colIndex = 0; var $row = $("#" + parentId).parent().parent(); $row.find('td').each(f ...

How can you reposition a component within the dom using Angular?

Just started learning Angular, so I'm hoping this question is simple :) Without getting too specific with code, I could use some guidance to point me in the right direction. I'm currently developing a small shopping list application. The idea i ...

What is the best way to save a JavaScript variable in local storage?

I am facing an issue with a JavaScript variable named addNumber. This variable is responsible for adding 1 to a div every time a button is clicked. However, the problem arises when I navigate between different pages on my website - the counter resets back ...

Personalized Map Designs for the Store Finder Plugin on WordPress

I am attempting to customize the appearance of a Google Map using the Store Locator Plus plugin for WordPress. I came across a support thread where someone successfully applied custom styles to the map with the following code snippet: <script> $(win ...

Difficulty in displaying accurate visuals for Albers US Choropleth Map using d3.js and React

I'm currently troubleshooting my code as I'm only seeing a large square of one color when I attempt to create a colorScale for each element with the class "county" on this Albers US map. The desired outcome is something similar to this project: ...

Avoiding server requests in Firefox by refraining from using ajax

I've implemented the following jquery code: $("#tasksViewType").selectBox().change( function (){ var userId = $('#hiddenUserId').val(); var viewTypeId = $("#tasksViewType").val(); $.post('updateViewType& ...

Display the specified div element automatically

Despite my best efforts, I can't seem to figure this out. I've searched everywhere for a solution but no luck so far. Is there a way to automatically display the content from http://tympanus.net/Tutorials/CSS3ContentNavigator/index.html#slide-mai ...

Looking up the image directory in a JSON file on the fly - (Module not found...) React, Next.js

I'm attempting to dynamically retrieve data from a JSON file and insert it into a component. { "team": [ { "id": "", "name": "", "role": "", "bio": "", "major": "", "i ...

React - input value for creating a numerical outcome is not functioning as expected

I'm currently in the process of learning React and have encountered an issue with my input field. I am attempting to have the input receive a number and then pass that number when the submit button is clicked. Most of the functionality is working as e ...

Creating Stylish Typography with CSS3

Seeking Inspiration Despite my efforts, I haven't been able to identify the name of a particular CSS effect that I'm trying to recreate. This has made it challenging to find examples or tutorials on how to achieve this look using CSS3. I have e ...

Unsuccessful PHP conversion of numbers to words

Kindly take note that the PHP code provided below is used to convert a numerical amount into words. It functions properly for integers like 5250, however, when it encounters a decimal number, it produces inaccurate results. Example: for 450 = four hundred ...

The art of positioning headers using CSS

Just a quick question: I'm working on formatting my HTML and CSS, and I have the following code: <div class="container"> <div id="header"> <div id="navbar"> </div> </div> </div> This is how my CSS looks ...

What is the proper way to integrate "require" into my Angular 2 project?

Currently, I am still in the process of trying to integrate the angular 2 code mirror module into my angular 2 application. Previously, I faced some challenges with imports which I discussed in detail here, and managed to overcome them. However, a new iss ...

Identifying overflow of text or elements in JavaScript during execution

The website I'm working on has a unique design that requires users to scroll horizontally using the Arrow Keys instead of swiping. To achieve this, I must constantly check for overflow in text or elements each time a new page is loaded, and if necessa ...

Potential 'undefined' object detected in Vuex mutation using TypeScript

Currently, I am diving into learning Vue.js alongside Vuex and TypeScript. While working on my application, I encountered an error stating "Object is possibly 'undefined'" within the Vuex Store. The error specifically arises in the "newCard" mut ...

Display a loading animation while the collapsible block is expanding in jQuery Mobile

I am experiencing an issue with displaying a loading icon when a collapsible block is opening. Within the collapsible block, there is a dynamically populated listview generated through ajax/php. This list could potentially contain up to 500 elements, so I ...

Perform a sequence of test functions to display as individual tests on BrowserStack

I am faced with a challenge in my web application where I have a series of functions that simulate login and run through various features. These functions are written in JS using nightwatch.js and selenium via browserstack. The issue is that all the func ...

Harvest Data from JSON Data Structure

My query pertains to handling data received from a URL in JSON format. Most examples I've come across focus on recursively printing symmetrical JSON objects, like this one. However, how can I effectively print the contents of the following JSON object ...

Tips on increasing the height of an element that is overflowing

When populating my timeline component with dynamically mapped data from an array, I encountered an issue where if I added more data causing the maximum height to be reached, the overflow-y element didn't display all content. Despite trying various sol ...