5

I am using CSS to create a pop-up on my page. I have a within the tag that is set to display:none;. Then when you hover over the tag the definition of the word pops up. This is working wonderful for me, HOWEVER sometimes the pop-up is appearing outside the browser window. Is there a way to make sure that the entire pop-up stays within the browser window with CSS or so I need to add in some Javascript? I would really like the pop-up to appear at the location of the link but I don't care if it moves left or right in order to be within the window and visible.

This is my code:

a span {
display: none;
}

a:hover span {
display: block;
position: absolute;
width: 300px;
z-index: 1000;
}

Thank you in advance for your help! :-)

3 Answers3

4

So, if you put the class "hvrpop-link" on the element you hover over and under that you put the class "hvrpop-win" on the window/menu that popups up. Then the following will nudge the popup window/menu over to the left just enough to keep it on screen. Note - it you already have a margin-left on the popup window you might need to save it down in "data" or something so it goes back to the correct place if you resize the window.

It seems to work all the way back to IE7:

$(function() {
    $('.hvrpop-link').hover(function() {
        var win=$(this).find('.hvrpop-win:visible');
        if(win.length>0) {
            var screenwidth=$('body').width();
            var width=win.width();
            var pos=win.position();
            var rightpos=width+pos.left;
            if(rightpos>screenwidth) {
                var moveleft=rightpos-screenwidth;
                win.css('margin-left','-'+moveleft+'px');
            } else {
                win.css('margin-left','0px');
            }
        }
    });
});
Simon Sawyer
  • 317
  • 1
  • 2
  • 11
1

You can check whether the corners of the popup are inside the window with jQuery. Have a look at this: How to check if an element is off-screen

Community
  • 1
  • 1
Pieter
  • 1,823
  • 1
  • 12
  • 16
  • Thank you for the reply! Is there a way to do this without jQuery and just use Javascript? I REALLY don't want to use jquery because I would have to go through and attach jquery to over 400 pages. I already have a Javascript file attached to all of these pages so if there is a fix I can put in that one file that would be best. – Christie DeLuca Ellis Jul 16 '13 at 17:05
  • Hm, that sounds as a lot of work. jQuery is based on Javascript, so it should be possible. I don't know whether it works, but maybe you can have a look at this: http://useallfive.com/javascript-tool-detect-if-a-dom-element-is-truly-visible/ – Pieter Jul 16 '13 at 17:11
  • You can add jQuery with a existing Javascript by the way. See this: http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file – Pieter Jul 16 '13 at 17:13
0

Will be better use the css code:

a span {
    position: absolute;
    z-index: 100000;
    display: none;
    min-width: 100px;
    max-width: 50%;
    left: 50%;
    margin-left: -25%;
}
a:hover span {
    display: block;
}
SlawomirJ
  • 106
  • 4