Solved

jQuery Mapster Resize: How to always fill document body + vertical center

Posted on 2014-01-30
7
1,554 Views
Last Modified: 2014-02-04
Hi experts!

Quiet challenging problem I have.

I'm using this jQuery mapster for custom styled HTML image maps:
http://www.outsharked.com/imagemapster/default.aspx?demos.html

Along with this nice tweak that makes it responsive:
http://www.andensolutions.com/blog/responsiveImagePaths

The problem:
It works fine, adapts to the window width and height, but I'd like to get it:

1. To always fill the browser window, no matter what width/height ratio is it (currently it leaves grey border if width lower than height value and vice versa)
2. At the same time to be always vertically centered

Here's my JSfiddle: http://jsfiddle.net/Aidam/DHSL8/7/

Any working suggestion would be highly appreciated!

Thank you
Adam
0
Comment
Question by:Aidam-11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39820458
>1. To always fill the browser window, no matter what width/height ratio is it (currently it leaves grey border if width lower than height value and vice versa)

The image is staying in proportion.  If the browser is narrowed as far as it can go and the height is the full width of the screen are you saying you want an image that is going to look out of proportion just to fill up the screen?

>2. At the same time to be always vertically centered
It did not seem vertically centered in jsfiddle but it does look fine in jsbin http://jsbin.com/ogEdOmek/1/

Add below code to resize vertically  http://jsbin.com/ogEdOmek/3/
var h = $(this).height();
$('img').height(h);
$( window ).resize(function() {
   h = $(this).height();
   $('img').height(h);
  
});

Open in new window

0
 

Author Comment

by:Aidam-11
ID: 39821753
Thank but this is not working for me.

Basically what I'm trying to achieve is exactly this behaviour:

http://jsfiddle.net/panchroma/gXGcD/

Any ideas?
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39822500
I found a fiddle that seems to do it.  The example I gave was very basic.  

http://jsfiddle.net/jamietre/jQG48/
var resizeTime = 100;     // total duration of the resize effect, 0 is instant
var resizeDelay = 100;    // time to wait before checking the window size again
                          // the shorter the time, the more reactive it will be.
                          // short or 0 times could cause problems with old browsers.
                          
$('img').mapster({
    mapKey: 'state'
});

// Resize the map to fit within the boundaries provided

function resize(maxWidth,maxHeight) {
     var image =  $('img'),
        imgWidth = image.width(),
        imgHeight = image.height(),
        newWidth=0,
        newHeight=0;

    if (imgWidth/maxWidth>imgHeight/maxHeight) {
        newWidth = maxWidth;
    } else {
        newHeight = maxHeight;
    }
    image.mapster('resize',newWidth,newHeight,resizeTime);   
}

// Track window resizing events, but only actually call the map resize when the
// window isn't being resized any more

function onWindowResize() {
    
    var curWidth = $(window).width(),
        curHeight = $(window).height(),
        checking=false;
    if (checking) {
        return;
            }
    checking = true;
    window.setTimeout(function() {
        var newWidth = $(window).width(),
           newHeight = $(window).height();
        if (newWidth === curWidth &&
            newHeight === curHeight) {
            resize(newWidth,newHeight); 
        }
        checking=false;
    },resizeDelay );
}

$(window).bind('resize',onWindowResize);

Open in new window

0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:Aidam-11
ID: 39822693
This fiddle you gave me it's the code I'm already building on.

Again, I need the image map to behave like this - to always fill the entire body: http://jsfiddle.net/panchroma/gXGcD/
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39822718
Let's see if somebody else as a better idea.  I have only seen that done as a background image.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39829951
The trouble with that background image is that instead of the grey bits on the side, you will now be missing parts of the image, but if you want to keep it proportional, that can't be helped. I updated your fiddle: http://jsfiddle.net/DHSL8/8/

The changes are to check for width/height ratio the other way around and set a negative offset for the top/left whichever is not the max.

I'm afraid there is a rather nasty timing problem but I haven't been able to put my finger on it yet. Sometimes the image is shown on a wrong offset after a sudden move it seems.

The changed code:
    // Resize the map to fit within the boundaries provided

// UPDATE: fit map "outside" window and center it
// TODO: resolve timing issues which cause the map to snap out of place after sudden movement

    function resize(maxWidth, maxHeight) {
        var image = $('img'),
            imgWidth = image.width(),
            imgHeight = image.height(),
            newWidth = 0,
            newHeight = 0;

        if (imgWidth / maxWidth < imgHeight / maxHeight) {
            newWidth = maxWidth;
            image.closest('div').delay(resizeTime).animate({ left: 0, top: Math.floor((maxHeight-imgHeight)/2) },resizeTime);
        } else {
            newHeight = maxHeight;
            image.closest('div').delay(resizeTime).animate({ left: Math.floor((maxWidth-imgWidth)/2), top: 0 },resizeTime);
        }
        image.mapster('resize', newWidth, newHeight, resizeTime);
    }

Open in new window

0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39830628
I just had to do something very similar on one  of my own projects.  This seemed to work at least for a background image.
<html>
<head>
<style>
body {
background:url('http://kb.adamhusar.com/wp-content/themes/kp/img/map.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
}
</style>

<meta charset=utf-8 />
<title>padas</title>
</head>
<body>

</body>
</html>

Open in new window

I don't know how that translates to an image map though.

I wonder if it would be best to just use media queries to size the image and not use js/jquery.  It may still be a bit jerky going from one size to the next, but I think most users open up the browser and view it in one size (especially if phone/tablet) and if on desktop, maybe slide to smaller or larger once.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question