What is the best way to resize a mouse-over div based on the size of the screen

When using Quora, there is a feature that displays a pop-up card when you hover over a username or picture. This card will always stay within the visible window area and adjust its position based on the screen size.

For example, take a look at these Quora screenshots. The first image shows the hover card on the right side with higher resolution, while the second image shows it on the left after zooming in.

I'm curious about how to implement this functionality myself. Any tips?

Answer №1

To achieve this, calculate the position and width of the popup along with the screen's width. Compare these values to determine if the popup exceeds the screen width. If it does, adjust the position accordingly by setting it to be within the screen boundaries. Here is a sample implementation using jQuery as an example:

    function adjustPosition(menu) {
        var menuHeight = $('#' + menu).outerHeight();
        var bodyHeight = $(window).height();
        if ((menuY + menuHeight) > bodyHeight) {
            menuY = bodyHeight - menuHeight;
        }
        var menuWidth = $('#' + menu).outerWidth();
        var bodyWidth = $(window).width();
        if ((menuX + menuWidth) > bodyWidth) {
            menuX = bodyWidth - menuWidth;
        }
    }

Keep in mind that you should also consider adjusting for height if needed.

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

What steps should I follow to create an automatic image slider using Fancybox that transitions to the next slide seamlessly?

*I've recently ventured into web design and am currently experimenting with Fancybox. I'm interested in creating a slider with 3 images that automatically transitions to the next image, similar to what is seen on this website: Is it feasible to ...

Issues encountered while implementing Ajax functionality with PHP and JQuery

I encountered an issue while attempting to utilize AJAX in a PHP file, which calls upon another PHP file. Here is the code snippet from the PHP file: <script> function obtainProducts(cat) { parameters = {"idCat": cat}; ...

Using jQuery, you can easily insert a <span> tag around selected text and then save this modification permanently in a local HTML file

I have compiled notes in an HTML file stored on my local computer, with the intention of keeping it solely for personal use. For instance, I have a snippet like this: <p> this is an example text</p> My goal is to highlight the word "example" ...

Retrieving HTML content using CURL

Could really use some help with my curl issue: I'm using CURL to connect to a page, then redirecting myself to another page that's only accessible when logged in. How can I retrieve the HTML code from this page? I can see the page displayed, but ...

Is there a way to prevent hiding when clicking the mouse on an Ajax Magnific Popup?

Just starting out as a beginner with ajax magnific popup. I downloaded the magnific pop library and tried creating an example to use the popup, but I'm facing an issue. When I click on the link to open the popup, it displays fine. However, when I clic ...

I require integrating another element alongside this button

I am trying to create a button component that, when clicked, will display another component along with it. However, I am unsure of how to achieve this. Take a look at the plantioComp return - this is the component that I want the button to trigger. <t ...

Creating a dynamic slider using jQuery

Here is the code snippet I have been working on: var current_slide = 1, animation_time = 1000, pause = 3000, slide_container = $('.slide_container'), interval, height = slide_container.height(), slides ...

Is there a simple angular directive available for displaying expandable/collapsible text?

I have been exploring the idea of creating an Angular directive that would truncate a paragraph or a div if it exceeds a certain number of characters (for example, 115). I have not been successful in finding a solution that fits my needs. I have come acro ...

Tips for using jQuery to slowly change a CSS property to "auto"

I've encountered a situation where I have an element styled with position:fixed, and bottom: auto;. When I apply the command .animate({bottom : '10%'});, it smoothly slides to the specified position. However, when I try to set it back to its ...

Retrieving information from CRM online utilizing the Web API

Recently, I developed a webpage to serve as a survey for end users to provide feedback on the helpdesk technician's performance in resolving tickets. The webpage was constructed using HTML, CSS, JS, and PHP. Currently, the page requires access to two ...

Is there a way to prevent the virtual keyboard from covering up input fields on a webview?

I am currently dealing with an issue on my Genero web-app where the webview of an Angular app is not scrolling under an input when it gets the focus. This makes it difficult for users to see what they are typing. Interestingly, loading the page in a regula ...

Customize your header and footer for printing to display on each page

Does anyone know how to use the window.print function to print a div content with custom header and footer that will show on every page? I have been able to add a header and footer, but they only appear at the top and bottom of the print document. How can ...

Adjust the text color when hovering with the style tag

When it comes to customizing the color of a link on hover, CSS makes it simple with code like this: .myId:hover{ color:green; } But what if you're using inline styles? Is there a way to achieve the same effect without just plain HTML and CSS? & ...

Change the position of the background image when hovering over the div element

Looking for advice on how to create a div with a background image that only appears when hovered over. I also want the image to either zoom in or move to the sides while hovering. Here is my current progress. .seg1 p:first-child { font-size: 20px; p ...

PHP, HTML, Email - Special characters in emails are being altered into other symbols

Here's the Situation... I am currently utilizing PHP to send out emails in a conventional manner... Snippet 0 mail('', $subject, $message, $headers); ... with the following content setup: Snippet 1 $boundary = uniqid('np&a ...

Interactive pop-up window featuring conversational chat boxes

Trying to create a comment box within a Modal dialog box that is half of the width. The comments in this box should be read-only and created using div tags. Attempted reducing the width and using col-xs-6, but ending up with columns spanning the entire w ...

Transforming an interactive HTML webpage into React/JSX

Imagine a scenario where I have two files: example.html <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="u ...

"Utilize jQuery to superimpose an image on top of

I'm attempting to create a feature where clicking on an image will reveal text underneath, similar to how retailmenot.com displays coupon codes. In addition, when the image is clicked, users should be directed to an external URL. Here is an example o ...

Shorten/Alternate coding:

Sometimes, creating the content section of a website can be time-consuming. I recently spent half a day crafting the index page of my website at . However, when attempting to add an additional image to the list of six already present, it turned into a leng ...

Using javascript to determine the vertical position of an element within a fixed container

I'm facing a challenge in determining the vertical position of an element using scrollTop when the parent container has a position: fixed. When I use scrollTop, it consistently returns a value of 0. This probably occurs because the element is no long ...