Managing both clicking and hovering events on a single element, ensuring that the popup modal remains open as long as it is being hovered over

After successfully implementing click and hover functionality on an element, I was able to position the popup relative to the mouse pointer based on a previous solution. However, I am now facing an issue where I want the popup modal to be fixed in a specific position when hovered over the middle of the element vertically.

The problem arises when I hover over the element causing the popup modal to flicker, and only opens up when I click somewhere else within the element.

Onclick

I aim to display the popup modal in full screen when clicked (this works but with flickering issues) + keep it open while being hovered. The modal should close only when clicked outside of it.

OnHover

My goal is to have the popup modal fixed at the vertical center by the side of the hovered element(this works too but the positioning is off) + remain opened while being hovered. As soon as the mouse leaves the element or the modal, it should automatically close.

Minimum Reproducible Example

View example here

app.component.html

<div class="box"
  (mouseenter)="addClickEvent($event)"
  (mouseleave)="addClickEvent($event)"
  (mousemove)="addClickEvent($event)"
  (click)="addClickEvent($event)">
</div>

<fs-modal *ngIf="modalShow"
           [ngStyle]="{'top.px': (zoneIsClicked ? 0 : modaltop) ,
                   'left.px':(zoneIsClicked ? 0 : modalleft)}"
           [ngClass]="zoneIsClicked ? 'zoneClicked' : 'zoneNotClicked'">
</fs-modal>

app.component

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
name = 'Angular';

modalShow = false;
modalleft;
modaltop;
zoneIsClicked;

addClickEvent(e) {
 if(e.type === 'click'){
  this.modalShow = true;
  this.zoneIsClicked = true;
 }
/*else if (e.type === 'mousemove') {
  this.modalleft = e.clientX
  this.modaltop = e.clientY
}*/
else if (e.type === 'mouseenter') {
  this.modalShow = true;
  this.zoneIsClicked = false;
  this.modalleft = e.clientX
  this.modaltop = e.clientY
}
else if (e.type === 'mouseleave') {
  this.modalShow = false;
}
}

}

app.component.css

.box{
width: 100px;       
height: 100px;      
background: rgba(254, 249, 247, 1);     
border: 1.5px solid #e24301;        
margin: 50px;       
font-size: 0.8rem;
position: absolute; 
}

.zoneClicked{
 display: block;
 position: absolute;
 width: 900px;
}

.zoneNotClicked{
position: absolute;
width: 50%;
}

fsmodal.component

import { Component, Input } from '@angular/core';

@Component({
  selector: 'fs-modal',
  template: `<div [ngStyle]="{'border':'1px solid black'}">ok</div>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class fsModalComponent  {
}

Answer №2

To accurately determine the position of the element you are hovering over, utilize getBoundingClientRect(). Apply these values to your popup accordingly. A demonstration can be found below.

Experience the StackBlitz Demo

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 call a JavaScript function with multiple arguments from a Silverlight project?

I encountered an issue when trying to invoke a JavaScript function with multiple arguments from an HTML page. Here is what I am attempting to do: wbNavigator.Navigate(new Uri("http://localhost:56433/route.html", UriKind.Absolute)); object results = wbNavi ...

In a scenario where multiple fields need to be incremented, one can accomplish this by incrementing one field every time while also increasing the other field only if it exceeds a

When trying to decrement class_number, everything works fine. However, the issue lies with number_of_classes not being decremented due to the usage of the $gt operator. posts.update({ 'title':doc.title, 'author':doc.author, 'class ...

concealing components during screen adjustments

There are 3 identical <div>s available: <div class="box">Hello World!</div> <div class="box">Hello World!</div> <div class="box">Hello World!</div> I need these <div>s to ...

PHP code to display "ed elements that do not adjust height"

A php script is being utilized to scan a directory and populate a gallery with all the images. Within the <div id="content"> tag, the function <?php create_gallery("path"); ?> loads all the images. The issue arises when the height of the <d ...

Transmitting OfficeJS 2D Array via an Ajax Call

Struggling with sending a "large" table using OfficeJS: functionfile.html loaded from manifest route <script> (function (){ "use strict"; Office.initialize = function (reason) { $(document).ready(function() { $("#send-data-button").cli ...

Unique browsing key

Is there a specific identifier that can uniquely represent the browser you are currently using? I have two applications logged in through ApiGateWay, and I need to determine whether they are both running on the same browser. Therefore, I require a unique ...

Learning to retrieve specific properties from an event target in JavaScript

Here is the code snippet I am working with: <h1 msgId = "someId"> Some text </h1> And in my code.js file, I have the following function: document.addEventListener('click', function(e) { target = e.target; }, false); I am tryin ...

Ways to reduce the width of a flex box

I am facing an issue with my main container where I have two items placed on the same line. Item 1 is an image linked to a URL and it should be positioned at the far left of the container. On the other hand, item 2 contains a font awesome icon that needs t ...

What is the best way to switch the site header in JavaScript or jQuery?

I have created a Bootstrap menu design and I am looking to add a sliding functionality to it. My goal is to hide the menu when scrolling down and display it when scrolling up (slide-down / slide-up). For implementing this feature, I plan to utilize jQuery ...

PHP can display content directly in the browser, without the need for jQuery

As I develop a web interface for an application that has longer processing times, a loading page is displayed to the user when it starts. An AJAX call then loads the output onto the page. Strangely, while browsing the PHP function directly in the browser r ...

Client-side validation with Jquery is failing to function properly

Currently, I am experimenting with the jquery.validate.unobtrusive.js plugin to dynamically generate form fields. Here is an example of how I'm creating a textarea field: var message = $("<textarea id='test'></textarea>"); $(mes ...

Tips for ensuring the border matches the size of the image

I am in the process of creating a website that includes a filter option. My plan is to have the filters displayed on the left side and the items on the right side. To achieve this layout, I have implemented a scrollable div for the items. However, I notic ...

Importing a JavaScript file into another JavaScript file as text in React Native can be a convenient way

In my project, I have a file named MyFirstPage.js that contains the following code snippet: renderCards() { let icon = this.state.icon; .... .... .... } This code is responsible for rendering cards within the main render function. However, as the ...

The process of exporting and utilizing models in Sequelize

When working on my node.js project with sequelize, I encountered a challenge of exporting and using table models in another file. I typically save table models in a folder, for instance Profile.js. module.exports = (sequelize, DataTypes) => sequelize.d ...

Heroku: Unable to retrieve resource - server returned a 404 (Not Found) status code

I successfully developed my Angular 6 app on my local machine with everything working perfectly. However, after deploying the project to Heroku and running my app here Testing App, I encountered an error in the console browser. Failed to load resource: ...

Manipulating a cloned object using jQuery

var clonedForm = $('form').clone(); //clonedForm.find('input[type="hidden"]').remove(); clonedForm.find('select').each(function(){ $($(this).val()).insertAfter($(this)); $(this).remove(); }); Atte ...

Creating an automatic image carousel using a combination of Jquery, Javascript, and CSS

I have been working on creating a slider using jQuery and javascript, but I am facing some issues with the autoplay functionality and the next-previous buttons. This slider is intended for displaying images as a title indicates. Can anyone assist me with i ...

Using raphaeljs and freetransform in conjunction with clip-path

Currently, I am utilizing Raphael along with Elberts FreeTransform Plugin. You can view my progress so far by visiting MyWork The issue arises when I attempt to translate or rotate the set of rectangles within my clip path. Once rotated or translated, it ...

Sending an HTTP post request to a server that is powered off results in an error on Android devices but strangely not on iOS devices

I am currently utilizing Angular 2 and Ionic 2 for my project. To test how the offline mode is handled, I intentionally shut down the server that my app sends requests to. For this project, I have created a Custom HTTP Service using import { Http, Header ...

HTML Multi-Column List Box

Can a List Box be created with List Items displayed in Multiple Columns? I know there are other options available, but I'm curious if this is achievable using the <select> tag ...