Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [DHTML] Posizione anomala del DIV

    Salve a tutti.
    Questa è una prova che ho messo on line... tanto per vedere se funzionava:
    http://xoomer.alice.it/lyrix/testJS/prova.htm

    C'è molto da mettere a posto, lo so, ma per il momento vi chiedo una cosa semplice. Se cliccate sopra un'immagine dovrebbe apparire un DIV centrato con il suo ingrandimento (e in alcuni casi anche una descrizione).

    Se però fate un paio di prove noterete che il DIV appare una prima volta in posizione errata, e solo dopo aver cliccato nuovamente sulla miniatura appare centrato.
    La domanda è ovviamente: "COME MAI???"

    Vi posto un po' di codice (anche lo potete tranquillamente vedere dal sorgente della pagina che vi ho linkato). Il codice proviene da uno script che ho trovato su dynamicdrive.com, ma a me piaceva rifarlo a modo mio...

    - JAVASCRIPT -
    codice:
    /* ************************************************************** */
    /* getPageSize :: funzione che ricava le dimensioni della pagina  */
    /* ************************************************************** */
    function getPageSize(){
    	
    	var xScroll, yScroll;
    	
    	if (window.innerHeight && window.scrollMaxY) {	
    		xScroll = document.body.scrollWidth;
    		yScroll = window.innerHeight + window.scrollMaxY;
    	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    		xScroll = document.body.scrollWidth;
    		yScroll = document.body.scrollHeight;
    	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    		xScroll = document.body.offsetWidth;
    		yScroll = document.body.offsetHeight;
    	}
    	
    	var windowWidth, windowHeight;
    	if (self.innerHeight) {	// all except Explorer
    		windowWidth = self.innerWidth;
    		windowHeight = self.innerHeight;
    	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    		windowWidth = document.documentElement.clientWidth;
    		windowHeight = document.documentElement.clientHeight;
    	} else if (document.body) { // other Explorers
    		windowWidth = document.body.clientWidth;
    		windowHeight = document.body.clientHeight;
    	}	
    	
    	// for small pages with total height less then height of the viewport
    	if(yScroll < windowHeight){
    		pageHeight = windowHeight;
    	} else { 
    		pageHeight = yScroll;
    	}
    
    	// for small pages with total width less then width of the viewport
    	if(xScroll < windowWidth){	
    		pageWidth = windowWidth;
    	} else {
    		pageWidth = xScroll;
    	}
    
    
    	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    	return arrayPageSize;
    }
    
    /* ********************************************************************** */
    /* getPageScroll :: funzione che ricava lo scrolling attuale della pagina */
    /* ********************************************************************** */
    function getPageScroll(){
    
    	var yScroll;
    
    	if (self.pageYOffset) {
    		yScroll = self.pageYOffset;
    	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
    		yScroll = document.documentElement.scrollTop;
    	} else if (document.body) {// all other Explorers
    		yScroll = document.body.scrollTop;
    	}
    
    	arrayPageScroll = new Array('',yScroll) 
    	return arrayPageScroll;
    }
    
    
    /* ********************************************************************** */
    /* pause :: attende un certo numero di millisecondi prima di proseguire   */
    /* ********************************************************************** */
    function pause(numberMillis) {
    	var now = new Date();
    	var exitTime = now.getTime() + numberMillis;
    	while (true) {
    		now = new Date();
    		if (now.getTime() > exitTime)
    			return;
    	}
    }
    
    
    
    
    /* ****************************************************************************** */
    /* showBox :: funzione che mostra il box contenente l'ingrandimento dell'immagine */
    /* ****************************************************************************** */
    function showBox(objLink)
    {
    	var arraySize = getPageSize();		// contiene le dimensioni della pagina e della finestra
    	var arrayScroll = getPageScroll();	// contiene il valore di scoll verticale
    
    	var imgPreload = new Image();		// variabile immagine per ricavare i dati
    	imgPreload.src = objLink.href;
    
    
    	var objContainer = document.getElementById("vista");	// div esterno
    	var objToDisplay = document.getElementById("grande");	// immagine da caricare
    	var objTitle = document.getElementById("commenti");		// eventuale commento o titolo dell'immagine
    
    
    	objToDisplay.setAttribute('src',objLink.href);
    	
    	objContainer.style.left = (((arraySize[0] - imgPreload.width) / 2) + 'px');
    	objContainer.style.top =(arrayScroll[1] + ((arraySize[3] - imgPreload.height) / 2) + 'px');
    
    	if (objLink.getAttribute('title'))
    	{
    		objTitle.innerHTML = objLink.getAttribute('title');
    		objTitle.style.display = 'block';
    	}
    	else
    		objTitle.style.display = 'none';
    
    	objContainer.style.display = 'block';	// visualizzo i sia il div
    	objToDisplay.style.display = 'block';   // che l'immagine
    
    }
    
    
    /* ******************************************************************************** */
    /* hideBox :: funzione che nasconde il box contenente l'ingrandimento dell'immagine */
    /* ******************************************************************************** */
    function hideBox(objLink)
    {
    	var objContainer = document.getElementById("vista");
    	var objToDisplay = document.getElementById("grande");
    	objContainer.style.display = 'none';
    	objToDisplay.style.display = 'none';
    
    }
    
    
    
    /* *********************************************************************** */
    /* Inizializzazione del BOX in cui comparirà l'ingrandimento dell'immagine */
    /* *********************************************************************** */
    function initBox()
    {
    	if (!document.getElementsByTagName){ return; }
    	var anchors = document.getElementsByTagName("a");
    
    	// loop through all anchor tags
    	for (var i=0; i<anchors.length; i++){
    		var anchor = anchors[i];
    
    		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "imgGrande")){
    			anchor.onclick = function () {showBox(this); return false;}
    		}
    	}
    
    		
    	var objBody = document.getElementsByTagName("body").item(0);
    	
    	// struttura prodotta dal codice
    	//
    	// <div id='vista'>
    	//		<img id='grande' />
    	//		<div id='comment'>...</div>
    	// </div>
    
    
    	var objVista = document.createElement("div");
    	objVista.setAttribute('id','vista');
    	objVista.onclick = function () {hideBox(); return false;}
    	objVista.style.display = 'none';
    	objVista.style.position = 'absolute';
    	objVista.style.zIndex = '90';
    	objBody.insertBefore(objVista, objBody.firstChild);
    	
    	var objImage = document.createElement("img");
    	objImage.setAttribute('id', 'grande');
    	objImage.style.margin = '10px';
    	objImage.style.display = 'none';
    	objVista.style.zIndex = '120';
    	objVista.appendChild(objImage);
    
    	var objComm = document.createElement("div");
    	objComm.setAttribute('id', 'commenti');
    	objComm.style.margin = '2px 10 5 10';
    	objComm.style.display = 'none';
    	objVista.appendChild(objComm);
    	
    
    }
    
    window.onload = initBox;
    HTML
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="it" xml:lang="it">
    
    <head>
    <style type="text/css" media="screen">
    	#vista{
    		background:#f5f5f5;
    		border:6px solid #e8e8e8;
    	}
    	#commenti{
    		font-size: 0.8em;
    		padding: 0.4em;
    		text-align:center;
    		color:#000;
    		background:#FCFCFC;
    		border: 1px solid #f0f0f0;
    	}
    
    </style>
    
    	
    <script type="text/javascript" src="test.js"></script>
    
    </head>
    	
    <body>
    	[img]pics/image01_t.jpg[/img]
    	[img]pics/image02_t.jpg[/img]
    	[img]pics/image03_t.jpg[/img]
    	[img]pics/image04_t.jpg[/img]
    	[img]pics/image05_t.jpg[/img]
    	[img]pics/image06_t.jpg[/img]
    	[img]pics/image07_t.jpg[/img]
    </body>
    
    </html>
    Se avete dei dubbi su quello che ho fatto... chiedete.
    Grazie

    PS: so che se avessi preso il codice originale avrei fatto prima... ma che gusto c'era?
    Dice il saggio:
    Non ci sono domande difficili, esistono solo risposte che non conosciamo!
    "Se qualcosa può andare male, lo farà" - Murphy

  2. #2
    Ho risolto! grazie lo stesso.

    Si trattava di fare un controllo if/else sulla posizione, poiché poteva risultare negativa.
    Dice il saggio:
    Non ci sono domande difficili, esistono solo risposte che non conosciamo!
    "Se qualcosa può andare male, lo farà" - Murphy

  3. #3
    scusate, ho errato nel postare il messaggio (modificato in quanto non permessa la cancellazione)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.