帮助将JavaScript单击功能转换为onLoad

时间:2011-06-26 07:05:12

标签: javascript jquery resize window onload

我正在尝试将运行点击事件的JavaScript函数转换为在页面加载和窗口大小调整时启动。如下所示,我注释掉了控制click事件的部分并添加了最后一行“window.onload”,并手动将class =“resizerd”添加到它正在使用的元素中。

该功能根本没有运行。 Chrome的开发工具显示“未捕获的TypeError:无法设置未定义的属性'prevWidth'”我是否在某处弄乱了语法?关于如何在负载下启动它的任何建议?

谢谢!

//var clicked =  document.getElementById("buttonImportant")

  var resizeeContainer = document.getElementById('video_container');
  var resizee = resizeeContainer.getElementsByTagName('video')[0];

  /*clicked.addEventListener('click',function(){
  if( resizeeContainer.className.lastIndexOf("resizerd")>=0 ){
  }
  else
  {
      resizeeContainer.className="resizerd";
  }*/
  myResizerObject.prevWidth = resizee.offsetWidth;
  myResizerObject.prevHeight = resizee.offsetHeight;
  myResizerObject.Init();
  //},false);


    myResizerObject.prevWidth = resizee.offsetWidth;
    myResizerObject.prevHeight = resizee.offsetHeight;
    myResizerObject.Init();

    var RESIZER = function(){ 

        this.prevWidth = resizee.offsetWidth;
        this.prevHeight = resizee.offsetHeight;

        this.resizee = resizeeContainer.getElementsByTagName('video')[0];
        this.resizeeContainer = resizee.parentNode;
        this.resizeeStyle = this.resizee.style;

        var ratio = this.resizee.offsetHeight/this.resizee.offsetWidth;
        var that = this;

        this.Init = function(){
            if( that.resizeeContainer.className.lastIndexOf("resizerd")>=0 )
            {
                var resizeeContOffsetWidth = that.resizeeContainer.offsetWidth;
                var resizeeOffsetWidth = that.resizee.offsetWidth;
                var resizeeContOffsetHeight = that.resizeeContainer.offsetHeight;
                var resizeeOffsetHeight = that.resizee.offsetHeight;

                if(that.prevWidth!= resizeeContOffsetWidth)
                {
                    that.prevWidth = resizeeContOffsetWidth;
                    var desired = resizeeContainer.offsetHeight/resizeeContainer.offsetWidth;
                    if(desired>ratio){
                        that.resizeeStyle.width=resizeeContOffsetWidth*desired+resizeeContOffsetWidth*desired+"px";
                        that.resizeeStyle.left = -1*(resizeeOffsetWidth-resizeeContOffsetWidth)/2+'px';
                    }
                    else{ 
                     that.resizeeStyle.cssText="width:100%;height:auto;position:fixed;";
                    }
                }

                if(that.prevHeight!=resizeeContOffsetHeight)
                { 
                    that.prevHeight = resizeeContOffsetHeight;
                    var desired = resizeeContOffsetHeight/resizeeContOffsetWidth;  
                    if(desired>ratio){  console.log(ratio);
                        //that.resizeeStyle.top = '0px';
                        that.resizeeStyle.left = -1*(resizeeOffsetWidth-resizeeContOffsetWidth)/2+'px';
                        that.resizeeStyle.width = resizeeContOffsetHeight*desired+resizeeContOffsetHeight/desired+'px';
                    }
                    else
                    {
                        that.resizeeStyle.top = -1*(resizeeOffsetHeight-resizeeContOffsetHeight)/2+'px';

                    }
                }

            }
        };
    };

    var myResizerObject = new RESIZER();
    window.onresize = myResizerObject.Init;
    window.onload = myResizerObject.Init;

4 个答案:

答案 0 :(得分:1)

您是否尝试通过<body>代码执行此功能?

像:

<body onload="myfunction();">

答案 1 :(得分:0)

尝试在OnLoad =“myfunction();”中调用整个调整大小的javascript函数页面正文的事件。我这样做是为了在每次加载页面时调整页面大小并且工作正常。

答案 2 :(得分:0)

你有这一行:

 myResizerObject.prevWidth = resizee.offsetWidth;

这可能是错误的原因。您没有做任何事情来声明myResizerObject,因此它无法拥有属性prevWidth

你去的某个地方

var myResizerObject = new RESIZER();

我怀疑你想以更合理的顺序排列这些行:)

答案 3 :(得分:0)

这样的代码应该可以正常工作:

var myResizerObject = new RESIZER();

function UpdateResizerObject() {
    var resizeeContainer = document.getElementById('video_container');
    var resizee = resizeeContainer.getElementsByTagName('video')[0];
    myResizerObject.prevWidth = resizee.offsetWidth;
    myResizerObject.prevHeight = resizee.offsetHeight;
    myResizerObject.Init();
}

window.onload = function() {
    UpdateResizerObject();
};

window.onresize = function() {
    UpdateResizerObject();
};

在您定义RESIZER课程后,请使用它。

您的错误是在创建对象实例变量之前调用它。

编辑:一些基本调试..向这个函数添加警报:

this.Init = function(){
   alert("Init called.. container: " + that.resizeeContainer);
   if (that.resizeeContainer)
      alert("class: " + hat.resizeeContainer.className);
   if( that.resizeeContainer.className.lastIndexOf("resizerd")>=0 )
   {
      ...
   }
}

看看你得到了什么。