Place a div according to the location of the mouse click

Is there a way to create a popup in Angular 4 that appears next to the mouse click coordinates? I want it to work similar to how events are created in Google Calendar.

Answer №1

Here is an example of how to set up a popup in your HTML and TS files:

<div class="click-container" (click)="onMouseClick($event)"></div>

In your TypeScript file:

onMouseClick(e: MouseEvent) {
  console.log(e);
  
  const popupHeight = 400,
    popupWidth = 300;

  let popupXPosition,
      popupYPosition

  if(e.clientX + popupWidth > window.innerWidth){
      popupXPosition = e.pageX - popupWidth;
  } else {
      popupXPosition = e.pageX;
  }

  if(e.clientY + popupHeight > window.innerHeight){
      popupYPosition = e.pageY - popupHeight;
  } else {
      popupYPosition = e.pageY;
  }
}

Don't forget to initialize the popup component with this code.

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

Creating a persistent div with fixed positioning, vertical overflow, and spanning the entire width using Bootstrap 4

I'm looking for a way to create two columns with two tables using Bootstrap 4.1. I want the right table to be fixed with auto overflow, but when I try to do this, the table does not span the full width of the column. Are there any bootstrap solutions ...

What is the best way to create a triangle shape that is responsive to elements?

I'm trying to make a background shape with a triangle inclusion using CSS. I want it to be responsive to all screen sizes, but I'm having some trouble. Any tips on how to achieve this? header { width: 100%; height:150px; background: #eee; } m ...

Angular (Ionic): Updating component view with loop does not refresh

When utilizing *ngFor to repeat a component multiple times, the components displayed are not updating in the DOM as expected. Displaying the component within an ionic page: <ion-row *ngFor="let file of files; let i = index"> <app-add ...

Ensure the continuous flow of text using the Div element

I've successfully implemented a fading image on scroll as desired. However, upon closer inspection here, I've noticed that the text and menu below the image are positioned too far away, especially when resizing the browser window. My goal is to ...

What is the best way to make the ngIf directive available for use throughout the entire project

I encountered an issue with the code snippet <div class="flex-container" *ngIf="mail"> Some data </div> The error message states: Can't bind to 'ngIf' since it isn't a known property of 'div'. What steps can I ta ...

How to Enhance GraphQL Mutation OutputFields with a Function Generator

I'm encountering issues while trying to incorporate a function generator within a GraphQL mutation. The function generator is utilized to streamline the promise chain inside the mutation. Prior to refactoring the code, everything was functioning corr ...

Finding the nth-level children using JQuery

Is there a way to select nth-level children in CSS/jQuery without using any ID or class references, only tag names? I know how to get first level children with the selector div#block > div. But I am looking for a solution to target deeper levels. & ...

Tips for adjusting the button color in Material UI by utilizing the ":active" pseudo-class

In the project I am currently working on, I am incorporating Material UI. One of the tasks I need to complete is changing the active state of a button. I have implemented both hover and active states from Material UI in my code: const useStyles = makeStyle ...

What role does the cleanData function play in jQuery?

While delving into the inner workings of jQuery, I came across the cleanData function multiple times. It is called by jQuery.remove() and other functions alike. What is the significance of invoking cleanData before removing a DOM element? Is jQuery's ...

"Which is better for maximizing the efficiency of an image grid: CSS or Jquery? What are the key

As a UX Designer looking to enhance my coding skills, I must admit my code may not be perfect. Please bear with me as I navigate through this process. I am in the process of revamping my portfolio website. The original seamless grid was created using a Ma ...

"Enhance your website with the magic of jQuery magnific-popup

Is there a way to add an additional button to the jquery magnific-popup component that can close the dialog box? I am currently utilizing this component to insert descriptions for photos, so I need a submit button that will successfully add the descriptio ...

Customize the label and value in Material UI React Autocomplete

If you visit this link, you can see an example of what I'm trying to achieve. My goal is to have the option label and value be different from each other. In the provided example, the following code snippet is used: const defaultProps = { ...

Change the objectID or _id obtained from a MongoDB Stitch query into a string format and then perform mapping on

I'm running into an issue with querying a MongoDB Stitch database where I am getting different results. When I console log all my data, it shows up like this (notice the 'id: Uint8Array(12) [ 94, 67, 83, … ]​​​' item.find().asArra ...

The comparison between ng-content and router-outlet in Angular 2

Can someone help me decide on the best approach for structuring my components? i. The first approach involves using ng-content in the parent component and then creating child components enclosed within the parent's selector. For example, creating a ...

Change the background color based on a dynamic variable in Angular 2

I have a timer that counts down to zero, and I wanted to change the background color of an element based on the timer's value: Green: 20 or higher Orange: 10 to 20 Red: 9 or lower My approach was to assign three different classes to the element and ...

Upon exiting the page, save the initial array in storage. However, the back button will load the last committed array

Currently, I have a feature on my website where users can filter articles based on their category. When the user selects the "ideas" filter, it creates a new array called "ideasFiltered" and stores it in the "filteredArticles" state. This works perfectly w ...

Possible solutions for AngularJS using ng- tags

I absolutely love incorporating AngularJs into my Multiple Pages Laravel Application. However, using the Laravel Blade Template Engine has made me reconsider adding Angular Tags to clutter my blade templates. For instance <div ng-controller="TodoCont ...

Dealing with browser timeouts for HTTP requests using JavaScript

Managing time out situations in a web application when calling a REST API is crucial. Below is the code snippet I am using to make the API call with jQuery ajax. $.ajax({ type: "POST", url: endpoint, data: payload, ...

Is it possible to change the style of an element when I hover over one of its children?

Encountered an issue while working with HTML and CSS: //HTML <div> <div class="sibling-hover">hover over me</div> </div> <div class="parent"> <div>should disappear</div> </div> ...

The Javascript function will keep on executing long after it has been invoked

I am currently facing an issue with calling a JavaScript function within an AJAX call. The progress function below is supposed to be executed every time the for loop runs. However, the problem is that although progress is being run as many times as the for ...