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

Tips for retrieving the ID value of the <li> element using JavaScript and AJAX

Here is a snippet of code I've been using to send the value of an <option>: function getXhr() { var xhr = null; if(window.XMLHttpRequest) // Firefox et autres xhr = new XMLHttpRequest(); else if(window.ActiveXObject){ // I ...

An error was encountered while trying to use the 'export' token in lodash-es that was not

Transitioning from lodash to lodash-es in my TypeScript project has been a challenge. After installing lodash-es and @types/lodash-es, I encountered an error when compiling my project using webpack: C:\..\node_modules\lodash-es\lodash. ...

Difficulty with alignment in the process of generating a vertical stepper using HTML and CSS

I'm currently working on developing a vertical stepper component using html & css with inspiration from this design. However, I've encountered some alignment issues in the css that I'm struggling to resolve. CSS .steps-container .step::b ...

Having difficulty finding the close button in a pop-up window while using the Selenium library with Python

While using chromedriver to open the website mentioned below in my extract_data() function, I encountered a problem with dismissing a pop-up message by clicking the 'x' button. Surprisingly, it seems to click the wrong button instead. What' ...

What is the best way to adjust the height of a row in a Material UI table using CSS

I've been attempting to establish a row height of 30px for a table (https://material-ui.com/components/tables/), but it seems that the minimum I can set is 53. Why is this restriction in place? How can I adjust the row height to 30px using CSS based ...

Obtaining information from the HttpServletResponse through an AJAX form

In my "first.jsp", there is a form containing hidden input values and pointing to another jsp file. <form id="popupForm" name="popupForm" action="<%= cqRequest.externalizeHref(handle) %>" method="post"> <input type= ...

Update the database with the information provided by this model using Ajax technology

There is an Ajax function in my View that I am having trouble with. function Save() { var Url = '@Url.Action("UpdateCaseDetails", "CaseDetailView")'; var frm = $("form"); var data = JSON.stringify(frm.serializeArray()); $.ajax({ ...

Is it possible to nest a <dl> list within a <li> item?

Would it be considered semantically accurate to include a dl (along with some dt / dd elements) inside a li tag? While it's common to add block or inline elements within an li element, the feasibility of including dl, dt, and dd elements is uncertain ...

Struggling to separate a section of the array

Check out this array: [ '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a171319121b1f163a1f1915080a54191517">[email protected]</a>:qnyynf', '<a href="/cdn-cgi/l/email-protection" class="__cf_e ...

Tips for aligning the text of a button and placeholder vertically

I am facing an issue with centering placeholder/input text and button text vertically on my website. I used Bootstrap to create the site and utilized its input and button classes, but I couldn't figure out how to achieve vertical alignment. I tried ad ...

Making changes to an object using a different object

Is it possible to merge one object with another object? For instance master = { name: "John", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e389a389cd808c8e">[email protected]</a>" } child = { phone: ...

The JSON property is not defined

After making an AJAX call to retrieve data, I receive a JSON Object instead of a string. When I log the object, all its properties appear correctly. However, when attempting to log one specific property, it shows up as undefined. Here is a snapshot of my ...

Placing a cookie when clicking the close button in a fancybox popup

I'm familiar with the jquery cookie plugin, but I'm having trouble setting a cookie when clicking the close button. Here's how I've approached it: <script type='text/javascript' src='jquery-latest.js'></sc ...

Passing a list of objects containing lists in MVC3

Is it possible for me to send an array of objects, each containing arrays, from JavaScript to a MVC action result method? Essentially, I have a KeyValuePair with keys as arrays of strings and I need to return a list of these KeyValuePairs. In my code, I ha ...

Canvg | Is there a way to customize canvas elements while converting from SVG?

Recently, I encountered an issue with styling SVG graphics dynamically generated from data. The SVG graphic appears like this: To address the problem, I turned to Canvg in hopes of converting the SVG into an image or PDF using a hidden canvas element. How ...

Hidden IFrame for Jquery File Upload

I was looking for a quick guide on setting up an AJAX-style file upload using a hidden iframe. Below is the section of HTML code related to the form: <div id = "file" class = "info"> <form id="file_upload_form" method="post" enctype=" ...

Prevent the selection of Single Origin and House Blend options once the user opts for Espresso

<td> <select class="type"> <option value="Espresso">Espresso</option> <option value="" class="">Cappuccino</option> <opti ...

Using Python with Selenium and JavaScript for Logging in

When using Selenium and Python, I am attempting to log in to a website but encountering difficulties. The issue is that the source code does not display the necessary Id=apple_Id required for sending login information, although it can be seen when inspecti ...

Switching between play and pause for the video element using a component for the child

Trying to toggle the play/pause of a child video by clicking on a parent div can be challenging, especially when dealing with multiple instances of the same div and video. A normal function may only work for one specific video, as mentioned by @ken. I hav ...

Enhancing Symfony's performance through optimized Ajax response time

When using Symfony2, I am experiencing differences in loading times for AJAX requests between development and production environments. In development, it takes 1 second to load, while in production it only takes 500 milliseconds for a simple call: Here is ...