Positioning of the tooltip at the bottom left using Bootstrap 4

I am attempting to align my input field tooltip perfectly with the bottom left corner of the input field. The page contains multiple dynamically generated input fields, so achieving this alignment is crucial.

After trying to use the offset parameter, I found that it was not very effective when dealing with zoomed content. The correction of +11 proved to be cumbersome in creating a general solution.

$('[data-toggle=tooltip]').tooltip({
    placement: "bottom",
    title: "test"
    offset: -$('#inputFieldId').width()/2 + 11
});

I experimented with modifying the transform style of the .bs-tooltip-bottom class and found that it yielded good results! However, the tooltip flickers momentarily between its original bottom position and the new bottom left position. There must be an easier way to achieve this, but I have yet to discover it.

$('#inputFieldId').on('shown.bs.tooltip', function () {
    var x = $('#inputFieldId').offset().left;
    var y = $('#inputFieldId').offset().top + $('#inputFieldId').height();
    $('.bs-tooltip-bottom').css('transform', 'translate3d('+x+'px, '+y+'px, 0px)');
})

To address the flickering issue, I attempted to set the opacity of the tooltip to 0 before showing it and revealing it only after applying my custom transformation. Despite these efforts, the flickering persisted.

CSS:

.tooltip.show {
   opacity: 0;
}

JavaScript:

$('#inputFieldId').on('show.bs.tooltip', function () {
    $('.bs-tooltip-bottom').css('opacity', '0');
})

$('#inputFieldId').on('shown.bs.tooltip', function () {
    var x = $('#inputFieldId').offset().left;
    var y = $('#inputFieldId').offset().top + $('#inputFieldId').height();
    $('.bs-tooltip-bottom').css('transform', 'translate3d('+x+'px, '+y+'px, 0px)');
    $('.bs-tooltip-bottom').css('opacity', '0.9');
})

UPDATE:

It appears that the element .bs-tooltip-bottom does not exist during the show event, rendering the opacity workaround ineffective.

Answer №1

Hopefully, there might be a simpler solution, but the following method has been effective for me. Everything remains the same as mentioned in the original post except for the change of event listener from show to inserted. inserted is triggered after show and before shown.

CSS (optional):

.tooltip.bs-tooltip-bottom, .tooltip.bs-tooltip-auto[x-placement^="bottom"] {
    margin-top: 10px;
}

JavaScript:

$('#inputFieldId').on('inserted.bs.tooltip', function () {
    $('.bs-tooltip-bottom').css('opacity', '0');
});

$('#inputFieldId').on('shown.bs.tooltip', function () {
    var x = $(this).offset().left;
    var y = $(this).offset().top + $(this).height();

    $('.bs-tooltip-bottom').css('transform', 'translate3d('+x+'px, '+y+'px, 0px)');
    $('.bs-tooltip-bottom').css('opacity', '0.9');
});

Answer №2

To eliminate the flickering effect, incorporating a delay in showing the tooltip is essential.

Here's an example:

$('#inputFieldId').tooltip({delay: { "show": 140, "hide": 0 }});

To completely eradicate the flickering, it is recommended to set the show delay to at least 140. Additionally, the change in opacity is not mandatory for this fix.

   $('#inputFieldId').tooltip({delay: { "show": 140, "hide": 0 }});
   $('#inputFieldId').on('shown.bs.tooltip', function () {
        var x = $(this).offset().left;
        var y = $(this).offset().top + $(this).height();
   $('.bs-tooltip-bottom').css('transform', 'translate3d('+x+'px, '+y+'px, 0px)');
    });

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

Implementing HTML in MVC using MvcHtmlString.Create while incorporating JavaScript features

I am facing a major dilemma. In my MVC project, I am rendering an HTML template (loaded from a .html file) into a View using MvcHtmlString.Create(); While this method works fine, there is a problem that arises. The HTML template requires JavaScript functi ...

Unable to shift to the side while in full flexibility mode

I ran into an issue with my code, shown in the image below. I am trying to position 1 on the left and 2 on the right, but something seems off. Can you spot the problem? <div className="mt-5 mx-5 py-3 shadow bg-white"> &l ...

What is the best way to incrementally add elements and automatically update them in the browser using JavaScript?

Below is a function that triggers when a button is clicked, adding 30 div elements to the document with specific attributes. var bar_counter = 0; bar_lengths = new Array(); function createBar(btnObj) { while(bar_lengths.length < 30){ var siz ...

Styling AngularJS Pie Charts

Is it possible to customize the appearance of a pie chart created with angularjs-chart in order to achieve a design similar to this: https://i.sstatic.net/xYcu9.png While there are some examples available on the angularjs-chart website, one particular ex ...

JS (jQuery) not working as expected - Laravel 8 integrated with Bootstrap 5 and jQuery

Having difficulty integrating bootstrap and jQuery with Laravel 8. Despite following multiple tutorials and meticulously following the steps, the buttons remain unclickable. It seems like the click function is not running at all. The app was initialized u ...

Unable to choose the div element with id ":1"

Just starting out with web development, and I have a div element with the following attributes: <div class="" id=":1" role="treeitem" aria-selected="false" aria-expanded="false" aria-level="1" aria-labelledby=":1.label" aria-setsize="10" aria-posinset= ...

Utilizing a Jquery plugin for enhanced form validation with the inclusion of supplementary techniques

I am looking to integrate a jQuery plugin into my form validation process, specifically to ensure that only letters are allowed in the name section. If a user enters special characters or numbers, I want the form to display an error message. Additionally, ...

Tricking a jqXHR into resolving with local data: A sneaky workaround

I came across a similar question on the topic of making jqxhr act like an already sending request, but unfortunately, that solution did not work for me. The challenge I am facing involves working within a framework that heavily utilizes jQuery ajax methods ...

What is the best way to display a popup on the right side when hovering?

I currently have a situation where a popup appears at the top when a user hovers over a link or text. However, the issue is that the popup is not fully visible on the screen. The top part of the popup seems to be hidden in the browser. My application uses ...

Push down on the scroll wheel and then let go

I am looking to create a script that will automatically scroll the window to a specific point if a certain condition is met. Once the scrolling is complete, I want to update the condition so that the window can freely scroll again. Can someone offer guidan ...

Tips for adjusting the size of numbers (e.g. 0, 1,4..) using CSS

I have a paragraph on my website that contains a phone number and an email address within the header section. I have styled them to be the same font-size: small, but for some reason, the phone number appears slightly larger than the email address when view ...

What steps can be taken to ensure the random generator continues to function multiple times?

My little generator can choose a random item from an array and show it as text in a div. However, it seems to only work once. I'm looking for a way to make it refresh the text every time you click on it. var items = Array(523,3452,334,31,5346); var r ...

Looping through jQuery, I am adding divs, but frustratingly I can only insert text into the initial div created. Why

var numPills = 3 for(var i=0; i< numPills; i++){ //var currentPill = JUser.pill1.; $(".col-r-in").append( $('<div/>') .attr("id", "medsProgress") .addClass("roundedBar") ); $('#medsProgress').append( $('<div/>&apo ...

Complete Calendar featuring event limitations. Click the Reset or Refresh button to view more events

Recently, I've integrated FullCalendar with the LimitEvents feature. Everything works smoothly when I initially load the calendar. However, I encounter an issue when updating the resources and displaying new data on the calendar. The view-more button ...

Tips for directing attention to a div element

Need some help here! I'm just starting out with JS and jQuery, so please be gentle. Is it possible to set focus on a div element? I am looking to trigger events when the div is clicked or focused, as well as when clicking outside of the div. Here&apos ...

Obtaining the URL from the anchor tag

I am currently working on a project that involves scraping data from the cinch.co.uk website. My tools of choice are Python along with the BeautifulSoup4 and Request libraries. My goal is to extract car data from each advertisement, starting by navigating ...

The jQuery accordions feature enforces the rule of only having one open at a time

Could someone assist with my custom jQuery accordion implementation? I am facing issues ensuring that only one accordion can be open at a time. The desired functionality is to have all accordions closed initially, and when a user opens one, it should close ...

Applying CSS dynamically to a mat-cell in Angular 6

In my material table, I need to apply specific CSS classes based on the content of the cell. https://i.sstatic.net/YN6EO.png These are the CSS classes that I am using .status-code{ flex: 0 0 10% !important; width: 10% !important; } .status-code- ...

What is the best way to ensure that my <h1> header stays above my <h2> header at all times?

As a novice in web design, I recently took on the challenge of creating my own website. I am a graphic designer and came up with a mock-up of how I envision the site to look. While I managed to complete most parts of it, there's one specific area wher ...

Error occurred when trying to set the method in the Magento checkout: Uncaught TypeError - Unable to access the 'style' property of null

After deciding to move my Magento 1.3 template to Magento 1.9, I anticipate encountering a lot of minor issues. However, one specific problem has left me stumped. The issue arises when I am in the on-page checkout process and opt to make a purchase as a g ...