Is there a way to ensure a box remains fixed on the edge of a browser window?

Looking to create a tooltip div that pops up next to and aligns with the top of a specific input element when it is clicked:

                lorem ipsum dolor
 ____________    ___________________
| this       |  [_button____clicked_]
| appears    |
| on side    |  body content dafjsd fdjskf sdf
|____________|  content djsfs df skfjs daskf kdf s
                text texty djkf jsk fdjfs
                texty hehe blabla ajdfskfsd

I could achieve this by using absolute positioning. However, I prefer not to display the entire tooltip without the need for scrolling. So, if the input box is at the bottom of the page, the tooltip should adjust to remain visible:

                content djsfs df skfjs daskf kdf s
                text texty djkf jsk fdjfs
                texty hehe blabla ajdfskfsd
 ____________   lorem ipsum dolor
| this       |   ___________________
| appears    |  [_button____clicked_]
| on side    |
|____________|  edge of browser window  vvv
------------------------------------------------------------

This means the tooltip should stay as close to its original position as possible while ensuring it remains within view as the user scrolls up or down.

Answer №1

If you're looking for a solution to your problem, delving into the code of an uncomplicated javascript tooltip script might give you the answers you seek: .

This particular script adeptly identifies the boundaries of the browser window and adjusts its display position accordingly with smooth transitions. Experimenting with the tooltip demonstrations at the beginning of the page will illustrate this concept effectively.

To address your specific question - the most effective approach involves utilizing javascript to determine the user's browser type, followed by employing appropriate calculations based on screen width, height, and the current positioning of the browser window to showcase the tooltip accurately. Detailed examples of this conditional logic pertaining to browsers and screen dimensions can be explored in the provided code :)

I hope this explanation proves beneficial.

UPDATE:

Upon reflection, I've decided to include the JS segment from the aforementioned code for future reference in case the link becomes inaccessible. Though somewhat enigmatic, it remains a functional script aimed at managing tooltip placements relative to the browser window:

var offsetxpoint=-60 //Adjust x offset of tooltip
var offsetypoint=20 //Modify y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Calculate distance between mouse and window corner
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//Adjust menu position if not enough horizontal space
if (rightedge<tipobj.offsetWidth)
//Shift menu to left by its width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//Position menu horizontally where mouse is
tipobj.style.left=curX+offsetxpoint+"px"

//Similar vertical position adjustment
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

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

By utilizing geocoordinates, arrange items in order of proximity to the individual's current location

Looking to organize an array based on the user's location in an AngularJS/ionic app. Specifically, the goal is to rank restaurants that are closest to the current user location 1/ Within my controller.js, I have the following code to retrieve the use ...

Leveraging the 'this' keyword in TypeScript

In my Javascript class, I used the 'this' keyword as shown below: if (this[this.props.steps[i].stepId].sendState !== undefined) { this.setState({ allStates: { ...this.state.allStates, [thi ...

Utilize xdebug and NetBeans 8.2 to troubleshoot PHP code submitted through an HTML form

After successfully setting up the XDebug extension to debug PHP code within NetBeans IDE (ver. 8.2), I encountered an issue. While debugging works fine when setting a breakpoint and running the script in debug mode, I am having trouble debugging PHP code t ...

RobotFramework: Find elements using the same class identifier

I am currently tackling a challenge with test automation using robotframework and the seleniumlibrary. The issue I am facing is that I am unable to locate the correct element due to there being two elements with identical classes. These elements are part o ...

What is the best way to nest a table within another table in AngularJS, especially when dealing with tables of varying numbers of rows and columns

I am currently working on creating a dynamic table that maps various enemy courses of action in the columns to friendly courses of action in the rows. Each cell in the table will contain a 2x2 matrix displaying the number of friendly casualties, ammo loss, ...

The read more button is not functioning properly when used in conjunction with the <br>

Can someone help me debug an issue I'm facing with my code? I have created an HTML tab that contains multiple DOM elements, each with a "Read More" button. Everything works fine when it's just plain text, but as soon as I add tags within the p ...

What is the best way to extract a value from a string using JavaScript?

In my string labeled as 'content', I have the phrase "data-RowKey=xxx" embedded within it. I attempted to extract the 'xxx' portion using the code snippet below: var val = content.substring(12 + content.indexOf("data-RowKey="), 3); Un ...

Tips for navigating between slides on a CSS carousel?

I am currently in the process of creating a CSS-based carousel .carousel { -webkit-scroll-snap-type: x mandatory; -ms-scroll-snap-type: x mandatory; scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch; overflow-x: scro ...

Tips for showing a jQuery dialog when a link is clicked

I want to create a jQuery popup that shows confirmation information when a specific link in my navigation menu is clicked. The code I have does not display the popup, but instead shows the information on the page mixed with other content. HTML Link <l ...

Setting up an inline SVG using webpack: a comprehensive guide

I am inquiring about the process to incorporate an inline svg with webpack. I am currently following the react-webpack-cookbook. My configuration in the webpack.config file is correctly set up with the file loader. However, the example shows using a bac ...

Determining the window height using jQuery when overflow is set to hidden

I've built a full screen web application that I don't want to scroll, so I used this code: $("body").css("overflow", "hidden"); However, after adding a resize listener: $(window).resize(function(){ console.log("Inner height: " + $(window). ...

Encountering a Microsoft error while trying to install jsdom with node.js

I'm currently in the process of setting up jsdom on my system. I found a helpful guide at but encountered the following issue: C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.Cpp.InvalidPlatform .Targets(23,7): e ...

Tips for utilizing MUI Typography properties in version 5

I'm clear on what needs to be done: obtain the type definition for Typography.variant. However, I'm a bit uncertain on how to actually get these. interface TextProps { variant?: string component?: string onClick?: (event: React.MouseEvent&l ...

Why do my session details in the stripe webhook show as undefined?

During the development of my stripe checkout session, I encountered an issue where I can successfully send information to my webhook and print out all the session details, but I am unable to access individual pieces of information. Whenever I try to access ...

Approach to decoupling design elements from DOM interactions

Seeking recommendations or guidelines for effectively segregating classes utilized for browser styling and DOM manipulation. Specifically, we are developing a single-page app using backbone.js and heavily relying on jQuery for dynamically updating page el ...

PHP implementation for a static header layout

I am interested in learning how to update content without refreshing the header. I have created a simple example below. Header.php <html> <head> </head> <body> <ul> <li><a href="index.php" ...

The react-rails gem does not support rendering React components with nested Child components

Utilizing React.js for server-side rendering is my current goal with the help of the react-rails Ruby gem. However, I have encountered a problem where a Child Component is not being rendered as expected. Single Component //javascript class Hoge extends ...

Unexpected border-bottom styling issue observed on adjacent div elements

This is my unique code: <!DOCTYPE html> <html> <head> <style> </style> </head> <body> <div style="border-bottom:1px solid black"> <div style="width=50%;float:left">A new paragraph with u ...

What is the best method for creating an HTML table using a JSON object?

Currently, I am in the process of developing an express app that can render a csv file and convert it into a json format. Within my method, I have this json data stored as a variable and my goal is to display it as a table on an HTML page. How can I effect ...

Using the jQuery window visibility API to ensure continuous operation

I am attempting to create a countdown that only counts down when the window is active and pauses when it is not. To achieve this, I am using a plugin called https://github.com/mathiasbynens/jquery-visibility/blob/master/jquery-visibility.js $(function ...