Is there a way to automatically adjust the position of a tooltip div based on its location relative to the screen in AngularJS?

I've implemented AngularJs to create a grid with repeated li elements, each displaying a description box when hovered over. However, I am facing an issue where the description box goes off-screen when hovering over items on the right side of the grid.

Check out this jsfiddle for clarification:

ul {
  width: 100%;
  margin: 0;
  padding: 0;
}
li {
  position: relative;
  display: inline-block;
  vertical-align: top;
  margin: 0;
  width: 20%;
  padding: 5px;
}
li:hover .info {
  opacity: 1;
  transition: 0.5s;
}
.image {
  width: 100%;
  background-color: red;
  height: 100px;
}
.info {
  position: absolute;
  background-color: yellow;
  padding: 0px 5px;
  width: 200%;
  top: 0;
  left: 100%;
  opacity: 0;
  transition: 0.5s;
  z-index: 100;
}
<ul>
  <li>
    <div class="image"></div>
    <p>Hover over me</p>
    <div class="info">
      <p>Description Here</p>
    </div>
  </li>

  // More li elements

</ul>

The layout of my grid is responsive, making it difficult to determine which side of the screen an item will be located on. Is there a way to dynamically change the grid item class in Angular based on its position within the grid?

Thank you!

Answer №1

To create customized tooltips, I recommend defining two separate classes - one for left and one for right positioning. Then, utilize the ng-class directive to call a function that will determine the appropriate class based on the element's position. Below is a detailed example that you can easily adapt to suit your specific scenario.

<html>
<head>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.3/angular-material.min.css" />
    <style>
        .left {
            background: #ffcc80;
        }

        .right {
            background: skyblue;
        }
    </style>
</head>
<body ng-app="myApp">
    <div layout="row" flex="100" ng-controller="customController as ctrl">
        <div flex="50" ng-class="ctrl.findClass('left')" id="leftTooltip">
            <h1>{{ctrl.greetings}} Left Tooltip</h1></div>
        <div flex="50" ng-class="ctrl.findClass('right')" id="rightTooltip">
            <h1>{{ctrl.greetings}} Right Tooltip</h1></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js "></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-animate.min.js "></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-aria.min.js "></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.3/angular-material.min.js "></script>
    <script>
        angular.module('myApp', []).controller("customController", customController);

        function  customController($scope){
            var ctrl = this;
            ctrl.greetings ="Greetings"
            ctrl.findClass = findClass; 

            function findClass(id) {
                var e=document.getElementById(id);
                var rect = e.getBoundingClientRect()

                if (rect.left < (document.body.clientWidth/2)-1) {
                    return "left";
                }
                
                return "right";
            }
        };
    </script>
</body>
</html>

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

Angular controller uses Javascript variables to store and manipulate data within the

How can I retrieve my javascript variable in my angular controller? Here is the javascript code: function getRecaptchResponse(){ var challengeVal = Recaptcha.get_challenge(); var reponseVal = Recaptcha.get_response(); //alert("reponseV ...

Angular Pagination: A Detailed Guide

My goal is to paginate 7 results using a custom pagination tag I created: <pagination ng-model="currentPage" total-items="array.length" max-size="maxSize" boundary-links="true"> </paginatio ...

Disappearing a menu list while zooming in on a page: The art of vanishing navigation

[QUESTION] Is there a way to make the Menu List disappear when zooming in on a webpage? For example: <-- Normal Page [No Zoom] --> [Logo] Profile Gallery Guestbook <-- Zoom In 120% --> [Logo] Profile Guestbook <-- Zoom In 150% --> ...

What is the best way to calculate the number of div elements with a specific class that are contained within parent div elements with another specific class?

Is there a way to count the number of elements with the class '.child' in each container and then add a sentence containing that count inside each container? <div class='container'> <div class='child'></di ...

Having difficulty locating the identification number of an item within the HTML coding

I'm currently trying to locate the ID of the "proceed to checkout" button for a bot that I am developing for my mother on the Best Buy website. However, it seems like the button's ID is hidden and I'm unable to determine how to uncover it. A ...

Shifting and implementing various styles across numerous elements at the same time

There is an anchor tag containing two spans... <a class="banner-logo" href="/search"><span id="banner-logo-hello">Hello</span><span id="banner-logo-world">World</span></a> When hovering over the anchor tag, I want to c ...

The most effective method for incorporating a header and footer into an HTML page utilizing a variety of client-side frameworks

Looking for a solution for my HTML project where I want to incorporate a header and footer to minimize rework. Any suggestions on a good approach? I have experimented with AngularJS using ng-include, and here is the code snippet: var app = angular.module ...

AngularJS is not properly clearing the datepicker

Upon submitting the form, I have managed to reset all input fields to blank except for the datepicker which still retains the previously selected date. How can I clear that as well? Snippet of HTML: <div> <form name="profileform" role="fo ...

Assign the value from JavaScript to the element with the "ng required" attribute, without resetting frm.$error.required to false

My situation involves using a button with ng-style that relies on frm.mybtn.$error.required as follows: ng-style="frm.mybtn.$error.required?{'border-color':'#a94442'}:''" I am updating the value of the button from JavaScript ...

What steps should I take to create a slider using my image gallery?

I have a unique photo gallery setup that I'm struggling with: As I keep adding photos, the cards in my gallery get smaller and smaller! What I really want is to show only 4 photos at a time and hide the rest, creating a sliding effect. I attempted ad ...

Can a universal Save File As button be created for all web browsers?

Currently, I am in the process of developing a music player on the client side and I am seeking a method to save playlists that include mp3 data. Concerns with localStorage The restriction of a 5mb limit for localStorage makes it impractical for my needs. ...

What is the best way to serve a .ttf file in node.js with the http and fs libraries?

I am running a basic node server and I have a CSS file that is trying to load a font from the server: @font-face{ font-family: 'NiagaraSolid-Reg'; src: url('http://localhost:1111/NIAGSOL.TTF'); } This is my attempt at servin ...

What are the specifications for the opacity, width, and height of the background image in Bootstrap's

My current project has specific requirements that I need assistance with: I need the background image of the jumbotron to fit the width of the page and have its height scaled proportionally. The opacity of the jumbotron's background image should be ...

How To Access a View by Clicking in Ionic Framework

I have designed the main screen shown below. When each link is clicked, it should open the corresponding view. https://i.sstatic.net/H84jt.png Here is what I have created so far: Main Screen <body ng-app="starter"> <ion-pane> < ...

Retrieve Claims using Security Token Service and AngularJS

I am currently utilizing ADFS as a Security Token Service (STS) while my relying parties are built with ASP.NET MVC and configured using the Identity Access Tool. The relying party is set up with the .NET WSFederationAuthenticationModule to retrieve claims ...

How can I display an array with keys from php in AngularJS using ng-repeat?

I have a rootScope variable that will store product categories, each category may or may not have child categories. Here is how I assign the rootScope: $rootScope.categoryList = $http.get('category').then((result) -> result.data) This code s ...

Oops! You can only have one parent element in JSX expressions

I'm working on creating a password input box, but I keep encountering an error when integrating it into my JS code. Here's the snippet of my code that includes TailwindCSS: function HomePage() { return ( <div> <p className=&q ...

Find a solution to splitting the area in half, with one side designated for the icon and the other for text

I'm currently working on a code to assign tasks to employees, and I have created a login form with multiple text fields, each accompanied by an icon. However, there seems to be a layout issue as shown in the image. There is a distinct gap between the ...

Adding flair to a fresh element and then removing it upon its inception

I'm working with a JavaScript code that creates a new element when a button is clicked. I have a question about it. Here's the JavaScript code snippet: var comment = document.querySelector("#AddComment"); var req = new XMLHttpRequest(); if(comm ...

A perfect illustration showcasing the distinction in practical applications for the operators `=` and `&`

Although I grasp the technical distinction between using = and & in isolated scopes, such as understanding what is assigned to isolate scope property: // "=" isolateScopeProperty = getter(parentScope) // "&" isolateScopeProperty = function(locals ...