Autor Zpráva
Musilda
Profil
Zdravím

potřeboval bych poradit se scriptem na zobrazování okna uprostřed obrazovky.
Mám script
$(document).ready(function() {  
 
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
                       
        //Set the popup window to center
        $(id).css('top',  100);
        $(id).css('left', winW/2-$(id).width()/2);
        $(id).css('width', winW/2);
     
        //transition effect
        $(id).fadeIn(2000); 
     
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         
     
});


Funguje to docela dobře, ale problém vznikne, když zmenším okno prohlížeče. Šíře okna a jeho umístění je vypočítávána podle velikosti celého okna, tedy i toho co je mimo obrazovku prohlížeče. Tzn., že musím použít posuvníky, abych si okno posunul.
Snažil jsem se najít nějaké řešení, ale k ničemu jsem nedošel.

Díky za každou pomoc.
David1256
Profil
Pokud ti jde pouze o šířku postará se ti o to toto:
var sirka=document.body.clientWidth;

To zjistí šířku prohlížeče (respektive obsahu), ale s výškou to už bude složitější. Protože se domnívám, že javacript dokáže zjistit pouze výšku stránky, ale nahoře má každý prohlížeč ještě řádek na adresu, menu atd. Takže ta výška by se dala udělat jen dost nepřesně.
Witiko
Profil
David1256:
Jde zjistit přesně oboje, jelikož dochází ke získání výšky a šířky dokumentu a do něj menu nepatří. Tvé řešení nicméně nepočítá s quirk módy prohlížečů a spor mezi document.documentElement a document.body. Osobně jsem si už před časem napsal následující řešení: http://pastebin.com/ee0AD9aa Netvrdím, že neexistuje jednodušší řešení, mě však ve své celistvosti vyhovuje.

Jen počítá s metodami indexOf a each na prototypu objektu Array, tedy:
if(!Array.prototype.indexOf)
  Array.prototype.indexOf = function(search, beginning) {
    var length = this.length, index = typeof beginning !== "number" || beginning <= 0?0:beginning;
    do {
      if(this[index] === search) return index;
    } while(index++ < length)
    return -1;
  }
  
Array.prototype.each = function(callback, from, to) {
  if(callback instanceof Function === false ||
     this.length === 0) return this;

  if(from === undefined)
     from  = 0;
  else if(from < 0)
    from = -from >= this.length?0:this.length + from;

  if(to === undefined)
     to  = from > 0?0:this.length - 1;
  else if(to < 0)
     to = -to >= this.length?(from > 0?0:this.length - 1):this.length + to;

  for(var descend = from >= to; descend === true?from >= to:from <= to;
                                descend === true?from--:from++) {
    if(this[from] === undefined || callback.call(this, this[from], from) === false) break;
  }
  return this;
}

Vaše odpověď

Mohlo by se hodit

Neumíte-li správně určit příčinu chyby, vkládejte odkazy na živé ukázky.
Užíváte-li nějakou cizí knihovnu, ukažte odpovídajícím, kde jste ji vzali.

Užitečné odkazy:

Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0