I must align a bootstrap modal(dialog) based on the opener's position

Using a bootstrap modal for the settings dialog in an angularJS app, but facing an issue where it opens in the center of the page by default instead of relative to the opener. Looking for a solution to keep it positioned correctly when scrolling.

Code:

Modal template:

<script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3 class="modal-title">I&#39m a modal!</h3>
    </div>
    <div class="modal-body">
        <ul>
            <a href="#">hello</a>
        </ul>
        Selected: <b>123</b>
    </div>
    <div class="modal-footer">
        <p>Done</p>
    </div>
</script>

HTML:

   <span class="settings" ng-click="open(lg)"></span>

JavaScript (JS):

$scope.open = function (size) {
    console.log($document.find('my_items_wdgt').find('settings'))
    var modalInstance = $uibModal.open({
        animation: true,
        appendTo: $document.find('hello123'),
        templateUrl: 'myModalContent.html',
        size: size,
        backdrop: true,
        windowClass: "myItemsModal",
        resolve: {
            brands: function () {
                return $rootScope.usersBrands;
            }
        }
    });
};

https://i.sstatic.net/TnKmk.jpg

Answer №1

I encountered a similar issue today but managed to find a solution that worked perfectly for my project.

        var modalInstance = $uibModal.open({
            animation: true,
            backdrop: true,
            backdropClass: "modal-backdrop-custom",
            template:"<h1>Hello World!</h1>" 
        }).rendered.then(function (modal) {
            var element = document.getElementById("elementIdHere"),
                rect = element.getBoundingClientRect(),
                modal = document.querySelector('.modal-dialog');

            modal.style.margin = 0;
            modal.style.left = rect.left + 'px';
            modal.style.top = rect.top + 'px';



        });

The solution involves obtaining the position of the relative component and then applying the same coordinates to the top and left properties of the modal.

I hope this solution proves useful to you as well!

Answer №2

It grabs the target attribute from either the opener or a related element given in a bootstrap button.

$(".position-modal").click(function(e) {
        var getid = $(this).attr("data-bs-target");
        console.log(getid)
        var thisleft = $(this).offset().left;
        var thistop = $(this).offset().top;
        var thiswidth = $(this).width();
        var thisheight = $(this).height();
        var idwidth = $(getid).width();
        var idheight = $(getid).height();
        console.log(thisleft)
        $(getid + " .modal-content").css({left: thisleft - Number(idwidth/2) + Number(thiswidth/2), top: thistop - Number(idheight/2) + Number(thisheight), opacity:0.2});
    });

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

onsen-ui: navigating to a page that is already in the stack

I am working on a project where I want to showcase users along with the posts they have created. The users should be able to search for specific posts and then click on them to view more details. Each post page will contain a link to the profile of the use ...

Avoid cascading of the 'display' property in JavaScript styling to prevent inheritance

Is there a way in Javascript to toggle the visibility of a larger portion of HTML without affecting inner display properties with display: <value>? Despite setting an outer display property using Javascript, the inner display properties are also alt ...

Sliding and repositioning elements using jQuery

My goal is to build a simple slideshow for learning purposes. I want the list items to slide automatically to the left in a continuous loop. I've written some code that makes the slides move, but I'm struggling to set the correct position for ea ...

Formatting decimals with dots in Angular using the decimal pipe

When using the Angular(4) decimal pipe, I noticed that dots are shown with numbers that have more than 4 digits. However, when the number has exactly 4 digits, the dot is not displayed. For example: <td>USD {{amount| number: '1.2-2'}} < ...

Internal server error encountered while making an AJAX call using AngularJS routing

I'm currently diving into AngularJS with a basic application focused on customers and their orders. The issue I'm encountering involves a table that showcases the list of customers along with a link to access their respective orders. However, upo ...

Is there a way to circumvent the eg header along with its contents and HTML code using JavaScript?

I have a script in JavaScript that is designed to replace the content of data-src attribute within each img tag with src. However, I am facing an issue where this script interferes with the functionality of a slider located in the header section. The sli ...

Could someone please assist me with an issue I am experiencing with an AJAX GET request?

My code is not fetching any data for me. Below is the code snippet: <script type="text/javascript"> function matriculaFn(mat) { $.ajax({ method: 'GET', url:'My url API, async: true, crossDomain: false, contentType: 'application/j ...

What is the best way to transfer Flow type properties from one React component to another?

I'm in the process of developing a component that will wrap another component known as Button. The tricky part is that the library where Button is defined does not expose the type of its properties. In order to properly assign types to my component, ...

Flask application experiencing JavaScript issues on local host, yet functioning properly when accessed via 127.0.0.1

My web application is built using Python with Flask for the server, and HTML with JavaScript for the user interface that utilizes callback functions and POST methods. While the application runs smoothly with redirections and REST API calls at: Upon click ...

A more efficient way to have Vue import files from the assets folder rather than manually inserting script tags into the index.html file

I have a simple structure and would like to utilize assets from cdnjs.com in my project. The issue arises when I try to import these assets from src/assets/lib instead of directly from the CDN. For example, importing jQuery like this: Main.js: import &a ...

React developer server is failing to automatically refresh the code

I've encountered an issue with react-dev-server where changes I make to the code do not reflect on the app itself even after refreshing (not hot-loading). I've tried setting up my own project as well as using Facebook's Create-react-app, whi ...

Having trouble logging JSON data from nodejs + express while serving static files through express. However, I am able to see the data when I only make a GET request for the JSON data without the static files

Currently, I am experimenting with sending data from a nodejs + express server to the front-end static files. The objective is for JavaScript (allScripts.js) on the front-end to process this data. At this stage, my aim is to log the data to the console to ...

Regular expressions won't produce a match if the string is empty

At present, I am employing the regular expression /^[a-zA-Z.+-. ']+$/ to ascertain the validity of incoming strings. If the incoming string is devoid of content or solely comprises whitespace characters, my objective is for the code to generate an er ...

My current scroll animations are problematic as they are causing horizontal scrolling beyond the edge of the screen

I've implemented scroll animations on various elements of a website I'm constructing. The CSS rules I'm utilizing are as follows: .hiddenLeft { opacity: 0; filter: blur(5px); transform: translateX(-090%); transition: all 1s; ...

Issue with combining jQuery-UI and Bootstrap offcanvas components

I've been struggling to understand why my Bootstrap navbar isn't working properly with jQuery-UI. It seems like they're not cooperating, and I can't seem to figure out the issue. If you have any insight into this problem, you'll be ...

JavaScript decimal precision function malfunctioning with currency format conversion

My current snippet works perfectly with the currency format 249.00, but I need to adapt it for a site that uses euros with pricing in the format 249,00. When I make this change, the total calculation goes haywire and shows as 29.900,00. Can someone advise ...

Retrieve the file that has been uploaded to AWS S3

Here is a snippet of code to consider : var express = require('express'), aws = require('aws-sdk'), bodyParser = require('body-parser'), multer = require('multer'), multerS3 = req ...

Utilizing CodeIgniter for Efficient AJAX Posting

I'm struggling to submit a post using the ajax post method. It appears that the request is being posted without any error messages, but the model doesn't seem to respond and insert the row into the database. While I'm not well versed in jQu ...

Guide on plotting latitude and longitude coordinates on Google Maps with Vue.js through JSON data fetched via AJAX

I have implemented a Vue.js script to fetch JSON data through an AJAX request. However, I am encountering issues with the code. <script> new Vue({ el: '#feed' , data: { details: [], }, mounted() { this.$nextTick(fu ...

Generate arrays with custom names by parsing JSON data

Currently, I am retrieving data from a MySQL database and converting it to JSON before passing it to a JavaScript class for chart display purposes. The challenge lies in creating arrays required by the chart from the JSON object without manually creating a ...