When it comes to responsive design, the CSS left and top properties may not always calculate as anticipated for an absolutely positioned element

Dealing with a responsive image inside a parent container, alongside an absolutely positioned element with higher z-index in the same container has proven to be problematic. While it works fine on a static design, making it responsive causes the pin to appear incorrectly due to browser miscalculations.

To address this issue, I resorted to using jQuery to calculate the responsive image size in pixels and determine the pixel-based left and top positions for the pin. However, even with this workaround, the problem persists.

HTML

<div class="col-xs-12" id="map">
            <img src="../img/world-map.jpg" alt="world-map" id="world-map">
            <a href="#" class="sydney" data-toggle="modal" data-target="#sydney-modal"></a>
            <a href="#" class="melbourne" data-toggle="modal" data-target="#sydney-modal"></a>
            <a href="#" class="perth" data-toggle="modal" data-target="#sydney-modal"></a>
            <a href="#" class="singapore" data-toggle="modal" data-target="#sydney-modal"></a>
            <a href="#" class="hongkong" data-toggle="modal" data-target="#sydney-modal"></a>
            <a href="#" class="india" data-toggle="modal" data-target="#sydney-modal"></a>
            <a href="#" class="china" data-toggle="modal" data-target="#sydney-modal"></a>
</div> <!-- end col map-->

Test Page Link:

LESS

#map
    {
        position:relative;
        #world-map
        {
            width:100%;
            z-index:-100;               
        } //end word-map
        .sydney{
            position:absolute;
            width:12px;
            height:12px;
            background-color:red;
            /*left: 81.5%;
            top: 77.5%;*/
            }
    }

Javascript solution

$( document ).ready(function() {
        $('nav li:contains("Contact us")').addClass("active");

        //get width and height of image in px
        var imgWidth=$("#world-map").width();
        var imgHeight=$("#world-map").height();

        var sydneyLeft=0.815;
        var sydneyTop=0.775;

        var sydneyLeftpx=0.815*imgWidth;
        var sydneyToppx=0.775*imgHeight;

        sydneyLeftpx=toInt(sydneyLeftpx);
        sydneyLeftpx=sydneyLeftpx-6; 
        sydneyToppx=toInt(sydneyToppx);
        sydneyToppx=sydneyToppx-6; 

        $(".sydney").css({left: sydneyLeftpx+"px",
                        top: sydneyToppx+"px"               
            });

});

Note: A screen resize function has not been implemented for the JavaScript code yet, testing has only been done by refreshing the browser window.

If anyone can provide insight into why these values are being improperly calculated and offer a solution, it would be greatly appreciated.

Answer №1

Issue

I set out to create a visually appealing effect on an HTML map using the img tag with a lower z-index, and added a pin or location marker over the image.

Initially successful for fixed designs, I encountered difficulties when working on responsive design images. Despite attempting to adjust positions using CSS percentages, the pin's location varied across different screen sizes. Even trying to calculate the image aspect ratio and modify the pin position pixel-wise through JavaScript did not yield the desired outcome.

Resolution

To address this problem, I opted to utilize the HTML map tag and area tag. Employing a plugin developed by Matt Stow helped make the map responsive.

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

Locate the parent element using a unique child element, then interact with a different child element

Can you help me come up with the correct xpath selector for this specific element? The element only has a unique href. Is it possible to locate the element by searching for the text iphone X within the href="#">iphone X and also checking for its paren ...

Internet Explorer 8 does not show the "Save Target as..." option for block elements

When using Internet Explorer and right-clicking on an anchor tag, the usual options include: "Open in New Tab" "Open in New Window" "Save Target as.." However, in IE8, if the anchor tag's content is a div or span with the "display:block" CSS pro ...

The overlay of the background image is excessively oversized

After implementing the following CSS to showcase a background image: height: 90%; /* or any other value, corresponding with the image you want 'watermarked' */ width: 90%; /* as above */ background-image: url('<?php echo "study/".$_SESS ...

Adjust the wait time for sliding on the jQuery rcarousel

I am currently utilizing rcarousel for my website and I would like to adjust the duration of time that each slide is displayed. At the moment, each slide stays up for just a few seconds, but I want to extend this so that each slide remains on screen for at ...

`Problem with event propagation`

I am dealing with propagation issues caused by a series of click events. The container's data is loaded via ajax, hence the need for the body on-click method. Below is the code: $(function () { $("body").on("click","#top-div",function(){ ...

jQuery is implemented prior to the completion of HTML loading

Is there a way to ensure that HTML content loads before executing a JavaScript function? $(document).ready(function() { var x = 10; if(x == 10) { $('h1').text("Its changed !"); alert("Test"); } }) <h1>Hello< ...

Can you explain the significance of "Support: Firefox 18+" in the jQuery1.9.1 source code?

Currently, I am delving into the source code of jquery1.9.1. In the initial sections, there is a note that reads: // This action cannot be performed due to certain applications such as ASP.NET tracing // the stack using arguments.caller.callee and Firefox ...

Is it acceptable to include text after the <li></li> tag?

I have a question: is it possible to add text after the Li tag and still maintain correct formatting? Here's an example: <ul> <li>Text</li> Text placed here after the tag <li>Text</li> Text placed here af ...

Display a list of dates within a specified range using the ng

Utilizing the ng-repeat directive to connect data with a filter for name search: <div ng-repeat='myoldrecs in myoldrec | filter:q as results '>......</div> $scope.myoldrec = [{name:ccc,date:13-02-2016},{name:ddd,date:14-02-2016}]; &l ...

Issue with displaying website monitors

I am facing an issue with my website where it displays differently on various monitors. Below is the HTML and CSS code snippets: HTML <body> <div id="Links"> <a href="About.html"><img src="Slideshow/About.png" width="140em" />< ...

Transmitting form data inputted by the user to a modal that resides in the same component, all without the need for child or parent components or

In need of a solution where users can input answers to questions and have all the entered data displayed in a popup alongside the respective question. If a user chooses not to answer a question, I do not want that question or any related information to be ...

The Bootstrap star-rating display duplicated on the page

I have integrated a plugin to display star ratings on my Bootstrap website. Here is a snippet of the modal code where I have included the star rating input element: <div class="modal-header"> <button type="button" class="close" dat ...

Alert: Font preload was not utilized within a short timeframe after the window's load event

I've been working on optimizing the loading speed of fonts on my website, so I added the following: <link rel="preload" href="{{ '/css/fonts/bebasneue-webfont.woff' | prepend: site.baseurl | prepend: site.url }}" as="font" type="fo ...

Can jQuery be set up to initiate AJAX requests for URLs that have gzip/deflate compression enabled?

I need to utilize a web service that can output data in either gzip or deflated format. My testing has confirmed that this service is capable of responding with raw JSON or gzipped JSON when accessed using wget and curl. My goal is to access this web serv ...

List items not appearing inline

I'm having an issue with displaying a list of movies from my database. Instead of showing inline as intended, they are appearing vertically. I've tried using both inline-block and inline for the li elements, but so far nothing has worked. Any ass ...

Tips for effectively using the parseInt function to access and verify a span element from inside a chrome extension

Trying to utilize parseInt to ascertain if a span element is greater than or equal to 1 within my Google Chrome Extension. Attempting to monitor this "0" for changes and trigger a specific URL upon change - Refer to the Image View of the "inspect" option ...

Ensure that an HTML table row remains fixed on the screen at all times

I'm looking to make a specific row in my HTML table always visible on the screen, either at the bottom if scrolled off or top. The row should scroll normally as part of the table when it's visible on the screen. How can I accomplish this using bo ...

Tips for sending additional parameters to an MVC controller without including them in the URL when using the JavaScript window.open method

<a href="#" onclick="openAnchor('<%# DataBinder.Eval(Container.DataItem,"Url") %>', '<%# DataBinder.Eval(Container.DataItem,"infoToSend") %>')"> </a> openAnchor= function (Url, infoToSend) { window.open(Url, ...

Error: JSON input abruptly ended - SyntaxError was not caught

Hey there! I've been struggling with an AJAX call that keeps throwing an error mentioned in the title. It seems like the culprit might be this line of code: var obj = jQuery.parseJSON(data);. Maybe my userData is not formatted correctly. Here's ...

Issue with executing jQuery code within a Ruby on Rails environment

As a beginner in both ruby on rails and jquery, I am currently working on an app written in ruby that involves executing a jquery version. I have the jquery-rails (2.2.1) gem installed, along with rails (3.2.12). I've implemented a jquery function to ...