Displaying a div when hovering over it, and keeping it visible until clicked

I want to display a div when hovering over a button. The shown div should be clickable and persistent even if I move the cursor from the button into the shown div. However, it should be hidden when moving out of the entire area.

I'm unsure about how to achieve this effect.

var mouseX;    
var mouseY;
$(document).mousemove(function (e) {
    mouseX = e.pageX;
    mouseY = e.pageY;
});

$(".circle-button-small").hover(function () {
    mouseX = mouseX;
    mouseY = mouseY - 100;
    $('.theme-hover').css({
        'top': mouseY,
        'left': mouseX
    }).show('slow');
});

$(".circle-button-small").mouseout(function () {
    if ($(".theme-hover").mouseenter()) {
        $('.theme-hover').css({
            'top': mouseY,
            'left': mouseX
        }).show('slow');
    } else {
        $('.theme-hover').hide('slow');
    }
}); 

Update

$(".circle-button-small").hover(
    function () {
        mouseX = mouseX;
        mouseY = mouseY - 100;
        $('.theme-hover').css({
            'top': mouseY,
            'left': mouseX
        }).show('slow');
    }, 
    function () {
        $('.theme-hover').hide('slow');
    }
);

http://jsfiddle.net/u2oj799u/6/

Update: The solution provided by jbyrne2007 was helpful, but it feels like a workaround. I'm looking for a solution mentioned in the comments below,

When hovering, I'd like the "theme-hover" to appear near the mouse pointer. Additionally, when exiting the circle-button, if not entering the "theme-hover", the "theme-hover" should be hidden. However, if I enter the "theme-hover," it should remain visible. The above solution feels like a workaround.

Answer №1

It seems there may be some confusion about the desired outcome of your project, but I suggest checking out this jsfiddle

$(".main-area").mouseleave(function () {
$('.theme-hover').hide('slow');
});

I have repositioned the div .theme-hover inside the .main-area to prevent it from disappearing when clicked.

In addition, I included this style for .theme-hover

cursor:pointer;

This makes the interaction more user-friendly.

Based on my understanding, I assumed you wanted the "Hey" div to appear upon clicking, so I added a click function as well.

 $(".theme-hover").click(function () {
        $('.overlay').toggle();
    });

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 way to align content in the center when its parent element has a position property

JSBIN Is it possible to center the number row without using JavaScript to calculate its position, considering its parent has a fixed attribute? I've tried using CSS properties like margin and text-align with no success. Any suggestions on achieving t ...

How come my gifs appear tiny even though I've adjusted their width to 32% each?

I am trying to display three gifs in a row, each taking up one-third of the width of the page. However, when I preview the page, the gifs appear tiny. I have set the divs to 32% each and the gifs should fill 100% of their respective div. Here is the code s ...

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

CSS3 :not does not exclude a selector by id

Hello wonderful community of Stackoverflow, Here is my CSS code: .text:not(#overview > *) { margin-top: 10px; margin-bottom: 10px; } I am trying to apply a 10px top and bottom margin to everything with the class "text", except for elements ma ...

Exploring AngularJS: The Innovative Navigation Bar

My goal is to incorporate the functionality demonstrated in this example: https://material.angularjs.org/1.1.1/demo/navBar To achieve this, I have created the following index.html: <!DOCTYPE html> <html lang="en"> <head> &l ...

Issue with displaying background image in h2 element

Consider me a novice who learns primarily through trial and error. Unfortunately, I'm stuck on this particular issue. My goal is to have an image appear behind the h2 text on my homepage, but despite my efforts, I can't seem to make it work. You ...

Is Node.js truly a single-threaded and non-blocking platform?

As I delve into the world of Node.js, one thing that has caught my attention is the fact that it operates on a single thread and is non-blocking in nature. While I have a solid understanding of JavaScript and how callbacks work, the concept of Node.js bei ...

Is it possible to utilize Bootstrap Icons as bullet points on a website?

Is it possible to use Bootstrap icons for bullet points with CSS? I am aware that FontAwesome can be used for this purpose, but I would like to confirm if the same can be achieved using Bootstrap. Though there is a method using: <ul> <li>< ...

Ways to dynamically assign a name to a div using AngularJS

Currently, I am in the process of developing a download function for an Android app using Angularjs based on this tutorial: Utilizing the Progress event in PhoneGap file transfers Everything seems to be working efficiently as planned; I can successfully d ...

Mocha: A Unique Perspective on Testing the express.Router Instance

As I was developing a JavaScript controller file, I came across the need to test if my controller instance contains an instance of the express method called Router(). import {assert} from 'chai'; import {UF_Controller} from '../../controlle ...

The jQuery hover function is not functioning properly on page load in Firefox

While this code is functioning smoothly in Chrome and IE, it seems to be encountering issues in Firefox! <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> JS: $(window).load(function(){ $("#bosti ...

Tips for implementing code to function across several images in HTML and CSS

Hey there, I'm currently working on a website project for my friend and I sometimes refer to www.w3schools.com for help with coding. I'm having trouble implementing a gallery feature where users can click on images to view them fullscreen. I foun ...

What's the best way to transform createHmac function from Node.js to C#?

Here's a snippet of code in Node.js: const crypto = require('crypto') const token = crypto.createHmac('sha1', 'value'+'secretValue').update('value').digest('hex'); I am trying to convert t ...

Vertical alignment of text within a center位置

My current project involves creating a website layout similar to this: I am facing difficulty in centering the text within the box like this: This is my progress so far: @font-face { font-family: 'Trend Sans 004'; /*a name to be used lat ...

Troubleshooting problem with transmitting data using the b4j jQuery WebSocket library

I'm seeking guidance on the process of sending data through a custom library that connects to a Java server operating as a WebSocket server. The code I'm using for successful connection is: <script src="https://ajax.googleapis.com/ajax/libs/ ...

Would you like to learn how to display the value of a different component in this specific Angular 2 code and beyond

Hey there, I need your expertise to review this code and help me locate the issue causing variable "itemCount" to not display any value in about.component.html while everything works fine in home.component.html. I am attempting to only show "itemCount" in ...

PHP jQuery buttons for popovers

With a simple click of a button, I can generate 8 presentations and then edit each one individually by clicking on its respective name. Within this editing process, there is another form where I would like to include additional buttons that allow me to cus ...

Creating a Custom Rule for Checkbox Validation using jQuery

Can the jQuery Validation plugin be used to validate custom values on checkboxes, rather than just True or False? For instance: <input id="test" type="checkbox" name="test" value="something"> I am struggling to find a method that can check if &apo ...

Trouble with AJAX communicating with PHP and not rendering fresh data

I have created a row of images that are clickable, and once clicked, a variable is sent via AJAX to a PHP file for a database query. The PHP file receives the variable and executes the correct query. However, instead of updating the HTML on my page, it sim ...

Utilize Laravel 8 and Vue Js to dynamically showcase retrieved data in input fields

I am facing a challenge with my Laravel 8 registration form using Vue js. Before submitting the form, I need to verify if the referring user exists in the database. If the user is found, I want to dynamically display their full name in an input field upon ...