Tips on positioning a div based on the screen dimensions

In my table, there is an icon that reveals a chart as a popup when hovered over.

This div is where the chart is displayed:

<div id="chart-outer" style="@style" class="popup-chart close">
    <h2 id="charttitle" style="width: 100%; text-align: center;"></h2>
    <div id="chartContainer" style="width: 96%; height: 300px; vertical-align: top; direction: ltr; display: inline-block; padding: 10px;"></div>
</div>

When hovering over the table icon, the chart appears like this:

https://i.stack.imgur.com/fCyKn.png

When hovering over the second last item in the table, the chart renders perfectly on the right side of the icon.

I am using JavaScript to apply CSS to the chart div when hovering over the icon:

var mouseX = 100, mouseY = 100;
$(document).mousemove(function (e) {

    mouseX = e.pageX;

    mouseY = e.pageY - 200;
});

//cache the selector
var follower = $("#chart-outer");
var xp = 0, yp = 0;
var loop = setInterval(function () {
    // change 12 to alter damping, higher is slower
    xp += (mouseX - xp) / 1;
    yp += (mouseY - yp) / 1;

     xp = '@SharedPageData.CurrentLanguage.LanguageID' == '1' ? 500 : 200;
    
    follower.css({ left: xp, top: yp });

}, 10);

I have two page versions, one for Arabic and one for English. For the English version, I set the left position to 200, and for Arabic, I set it to 500.

Although it works well, on some high-resolution screens, the chart may overlap with the table icon and flicker. I want to position it consistently on the right side of the icon regardless of screen size, and on the left side for Arabic. How can I achieve this?

Answer №1

Utilize the vw and vh CSS units, which are relative to the window size. For example:

  • 10vw represents 10% of the total window width
  • 30vh represents 30% of the total window height.

You can experiment with the following code:

.sample-box {
  width: 30vw;
  height: 10vh;
}

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

Tips for modifying the appearance of this input document

Hey there, I have this input element created in React: <span className="btn-file" type='button'> <div style={{display:'flex',justifyContent:'space-around'}}> <span style ...

Encountering the Extjs 3.4 error ERR_UNKNOWN_URL_SCHEME while trying to access a legitimate JSON

Using Extjs 3.4, I am working on a simple ajax request: Ext.Ajax.request({ url: "localhost:3000/offers.json", success: function(response, opts) { var obj = Ext.decode(response.responseText); console.dir(obj); }, failure: funct ...

Sending Javascript Variable through Form

I'm a newcomer to JavaScript and struggling to solve a particular issue in my script. I need help passing the variable "outVal" to a PHP script when the submit form is clicked. The value of "outVal" should come from the "output" value in the script. I ...

Executing a PHP function every second using JS/Ajax: A step-by-step guide

Currently, I am utilizing the Highcharts API from to create a dynamic graph that reflects server load on my web panel. I have developed a PHP function called get_server_cpu_usage() which retrieves the current load data each time it is executed. My JavaS ...

ES6 module import import does not work with Connect-flash

Seeking assistance with setting up connect-flash for my nodejs express app. My goal is to display a flashed message when users visit specific pages. Utilizing ES6 package module type in this project, my code snippet is as follows. No errors are logged in t ...

Tips for transferring an entire array to a servlet and retrieving it efficiently

I have been attempting to collect all jqgrid data into one array and send it to a servlet. So far, I have tried the following code snippet: var rows= jQuery("#list").jqGrid('getRowData'); var paras=new Arr ...

Utilize varying sizes within a single column using Bootstrap

I am struggling to create a heading that spans the entire width of my listview within Bootstrap. Despite being in the same column, the heading does not extend as far as the listview. I desire for the title to stretch all the way across the list, leading m ...

How can you optimize the storage of keys in JS objects?

Just pondering over this scenario: Consider a line definition like the one below, where start and end are both points. let ln = { s: {x:0, y:0}, e: {x:0, y:0}, o: 'vertical' } Now imagine having a vast array of lines, how can we sav ...

Is it possible for a function to alter the 'this' object in JavaScript?

Referencing the MDN article on this keyword in JavaScript When not in strict mode, 'this' in a function will point to the global object. However, attempting to modify a global variable within a function does not result as expected. Is there an ...

jQuery dynamically updating calculations on a single row consecutively

Currently, I am in the process of developing a small table to calculate potential winnings from betting on a rubber duck race with specific odds for each duck. I have managed to get most of it working but have encountered an issue... Upon loading the pag ...

Dynamic columns added to the DataTables plugin with the use of the brackets-negative feature

Before initializing my DataTable, I dynamically generate headers for it. As a result, I am unable to predict the column datatypes, relying on DataTables to identify them based on the data for sorting. Some columns contain currency data in formats like $23 ...

The frozen column feature of jQGrid does not function properly when it is implemented inside a jQuery Tab container

I have encountered an issue with my jQuery TAB setup. In the first tab, I have a jqGrid with frozen columns which is functioning correctly. However, in the second tab, another jQgrid with frozen columns is not working as expected. Upon removing the code t ...

CSS- The ultimate guide to perfectly centering a webpage without the need for extra margins!

Our SharePoint 2013 generated web page has a width of 1024. The main content div is styled as follows: #container_master { width:1024px !important; margin-left: auto !important; margin-right: auto !important; background-color:#FFF !impor ...

Ensuring Consistent Item Widths in a Loop using JavaScript or jQuery

In my code, I created a function that dynamically adjusts the width of elements in a loop to match the widest element among them. // Function: Match width var _so20170704_match_width = function( item ) { var max_item_width = 0; item.each(function ...

Is there a way to program custom key assignments for my calculator using JavaScript?

As a beginner in the world of coding and JavaScript, I decided to challenge myself by creating a calculator. It's been going smoothly up until this point: My inquiry is centered around incorporating keys into my calculator functionality. Currently, th ...

What is the best way to send a POST request to my Rails controller using Ajax?

I'm facing an issue trying to transmit data from my JavaScript game-state on the front end to my controller and ultimately store it in the database. Unfortunately, I haven't received any response so far, and I'm struggling to pinpoint the re ...

`Why setRequestHeader is essential for Ajax and XMLHttpRequest`

Should I ever manually specify the setRequestHeader as 'application/x-www-form-urlencoded' for an ajax POST request? Is it necessary to manually define the setRequestHeader as 'multipart/form-data' when uploading files via ajax? Do XMLH ...

Ensure the vertical dimension of the webpage is properly maintained

I am currently working with a client who has requested a website design with a specific height for the content section. The Inquiry: Is it possible to automatically create a new page for text that exceeds the maximum height of the content section on the ...

Discovering applied styles based on component props in Material-UI v5 using browser developer tools

When working with Material UI v4, I found it easy to identify which styles are applied by component props classname using browser dev tools. This allowed me to override specific styles of the component effortlessly. However, in Material UI v5, I am unsure ...

Identifying the moment when the body scroll reaches the top or bottom of an element

I have been experimenting with javascript and jquery to determine when the window scroll reaches the top of a specific element. Although I have been trying different methods, I have yet to see any successful outcomes: fiddle: https://jsfiddle.net/jzhang17 ...