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

Update links within a designated div section

How can I change the color and alignment of anchor tags within the third child div that is part of a set of three nested divs? These child divs are arranged side by side within a parent div with the class .logo-bckgrnd-rpt. Although I have successfully tar ...

Incorporate text into the URL of the image

Got a URL of an image like this: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg', and looking to add 240x240 within the URL. Current Url: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg Desire ...

Approach to clearing form data and displaying a message post-submission in AngularJS

Currently, I am in the process of developing an angularjs login and forgot password module. The snippet of my code looks like this: <body data-ng-cloak data-ng-app="LoginModule" data-ng-controller="LoginController"> <div id="login-page"> ...

Angular neglects the specified animation timing

I created a sidebar component that relies on the sidebarExpanded state passed through Input(). Despite the correct animation trigger upon input change, the width adjustment happens abruptly. Interestingly, the same animation code works flawlessly in anothe ...

Hover shows no response

I'm having trouble with my hover effect. I want an element to only be visible when hovered over, but it's not working as expected. I've considered replacing the i tag with an a, and have also tried using both display: none and display: bloc ...

I am experiencing issues with the functionality of the jQuery function

I am currently diving into the world of jquery. I put together a script, but unfortunately it's not functioning as expected. 1 Here is a snippet of my HTML code <ul id="recur" class="elasticstack"> <li id=<?=$item['id']?> ...

Eliminating the bottom border of all buttons, except for the last three buttons in the list, solely using pure JavaScript, unless alternative methods are available

I have 3 sets of buttons, with each set containing 9 buttons stacked in 3 columns inside an ordered list (ol) within list items (li). I have removed the bottom border of the buttons to avoid double borders since they are stacked on top of each other withou ...

Attempting to alter an image with a click and then revert it back to its original state

I'm currently working on a feature that toggles an image when a specific class is clicked. The code I have so far successfully switches from 'plus.png' to 'minus.png' upon clicking, but I need it to switch back to 'plus.png&ap ...

Having trouble changing the Font Awesome icon on click?

I'm struggling to figure out why I can't change a font awesome icon using a toggle. I've tried various solutions but nothing seems to be working. Any insights on what might be causing this issue? (HTML) <span class="toggle-icon" ...

Embed the YouTube video using the URL in the video tag

I tried using the <video> tag, but I'm having trouble getting it to play videos from YouTube. Even though I found this http://jsfiddle.net/wCrNw/, it doesn't seem to work as expected. I also consulted Show Youtube video source into HTML5 ...

Testing the capabilities of AngularJS with e2e testing using the angular-recaptcha library

I have been attempting to e2e test my basic application, but I am facing challenges with the angular-recaptcha plugin from VividCortex (https://github.com/VividCortex/angular-recaptcha). Here is the test case I am working on: it('should redirect t ...

The issue of checkboxes not being sorted properly in Slick Grid

Currently, I am working on resolving a sorting issue within one of our applications using Slick Grid for grid implementation. The existing sort function, although functional, presents a challenge. While it successfully sorts columns, the checkbox associate ...

extracting an empty value from this variable

When I click on an anchor tag using this operator, the value appears blank. I have multiple divs with the same class, so I used the .each() function, but I can't figure out where I'm going wrong. The desired output is that when I click on one of ...

Leverage JQuery Mobile for smooth sliding and effortless deletion of list elements

Currently, I am testing a JQuery Mobile webpage that features a simple list setup: Upon clicking the list items, they get highlighted and their ids are saved in a local array. Is there an easy (or not so easy) way to transition the selected elements slidi ...

Remove the <select> highlight specifically for Internet Explorer

Looking for a way to remove the blue highlight in Internet Explorer. I am working with a select tag and whenever an option is chosen from the dropbox, the blue highlight shows up. Unfortunately, I don't have access to IE on my Mac so I can't test ...

Ways to make an AngularJS Expression show an empty value

Consider the following HTML snippet: <p>{{ booleanVariable ? "The variable is true!" : "" }}</p> In case 'booleanVariable' evaluates to false, the expression should display nothing and the <p></p> tags should not be show ...

Encountered an error when attempting to submit with Node.js and Express.js connected to MySql - "Cannot POST /login

I am currently working on creating a basic login page using node.js with the express.js and mysql packages. The goal is to redirect users to the layout.html page if their username and password exist in the mysql database. For this project, I have set up a ...

The message "Temporary headers are displayed" appears in Chrome

After creating an API to remove images from a MongoDB database using GridFS, I encountered an issue when calling the API. The image is successfully removed, but it causes the server to stop with an error that only occurs in Chrome, displaying "Provisional ...

Creating a single factory that consolidates multiple return statements

Exploring the ins and outs of AngularJS, I find myself eager to learn how to effectively combine two factory modules. Specifically, I am interested in merging the following two: // Factory Module One services.factory('UserService', function ($re ...

Create an interactive mechanism where one scrollbar dynamically adjusts another scrollbar and vice versa

Using the provided js-bin, how can I synchronize scrolling between the "left" div and the "right" div when using the mouse wheel? Currently, if you uncomment the code block, the scrolling behavior changes from scrolling by 100px per scroll to scrolling pi ...