Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    5

    [Js OO] ambito delle variabili

    Ho fatto questo in script per creare un animazione ( espandi - richiudi ), ma quando lo eseguo ottengo "this.obj has not properties" riferito ad "this.obj.style.overflow = 'hidden'". Penso che sia perchè "window.setInterval" esegue la funzione "this.collapse" in un ambito dove "this.obj" non è definito. Vorrei un aiuto per far funzionare lo script così com'è, senza passare parametri in ogni funzione!

    codice:
    function Toggle ( trigger, obj, speed ) {
    
    	this.intervalId = null ;
    	this.trigger = trigger ;
    	this.obj = obj ;
    	this.speed = speed ;
    
    	this.create = function () {
    		window.clearInterval ( this.intervalId ) ;
    		if ( !this.speed )
    			this.speed = parseInt ( 100 / 30 )  ;
    		if ( !this.obj.style.display ) {
    			this.trigger.innerHTML = '+' ;
    			this.intervalId = window.setInterval ( this.collapse, 1 ) ;
    		}
    		else {
    			this.trigger.innerHTML = '-' ;
    			this.intervalId = window.setInterval ( this.expand, 1 ) ;
    		}
    	}
    
    	this.expand = function () {
    		this.obj.style.display = '' ;
    		if ( this.getHeight () < 100 ) {
    			this.obj.style.height = this.getHeight () + this.speed + 'px' ;
    		}
    		else {
    			this.obj.style.height = 100 + 'px' ;
    			window.clearInterval ( this.intervalId ) ;
    		}
    	}
    
    	this.collapse = function () {
    		this.obj.style.overflow = 'hidden' ;
    		if ( this.getHeight () > 0 ) {
    			if ( this.speed > this.getHeight () )
    				this.obj.style.height = 0 + 'px' ;
    			else
    				this.obj.style.height = this.getHeight () - this.speed + 'px' ;
    		}
    		else {
    			this.obj.style.display = 'none' ;
    			window.clearInterval ( this.intervalId ) ;
    		}
    	}
    	
    	this.getHeight = function () {
    		return this.obj.offsetHeight ;
    	}
    
    }

  2. #2
    beh, il this non va usato a caso, negli intervalli sarà sempre l'oggetto window, insensato se rivolto allo scope locale.

    this. dentro una funzione, è l'oggetto window, c'è una pillola che parla di "JavaScript obejct oriented"
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    5
    Grazie della risposta! Ho letto la tua pillola e ho modificato il codice. Ora funziona!
    Hai scritto una pillola veramente utile!

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.