Monday, 15 March 2010

javascript - HTML element not resizing when I set window.onresize -


I have recently started learning about web development, especially javascript, and I'm trying to write a few simple tasks to increase the language I wrote a function to change the size of a box in the ratio of window, but for some reason this is not working - when I use the size of the browser window Change, every time the size changes Receive function does not update. I think I'm making some really silly errors, but because I'm not familiar with Javascript, I can not understand what this is.

My HTML is very simple:

  & lt; Body & gt; & Lt; Div id = "box" & gt; Oh. & Lt; / Div & gt; & Lt; / Body & gt;   

and the JavaScript code is:

  function resizedToptorescreen (element, percent) {var wHight = window.innerHeight; Var objHeight = wHeight * (percent / 100); Element.style.height = objHeight + "px"; } Window.onresize = resizeToScreen (document.getElementById ('box'), 50);   

Here is a link to JSField, which shows the box in yellow color:

You are immediately executing the resizeToScreen function, you need to provide a function reference for onresize , so that it can be said later Window would like to be resized: windows.resources = function (resizeToScreen (document.getElementById ("box"), 50); };

After you prepare the Dome, you want to attach this event, like inserting it inside:

  window.onload = function () {// Here };   

Should be OK for now, so that the #box element should not be resized before it is ready.

Note that instead of the .onEVENT property is fine, but can be overwritten, so it is addEventListener (and attachEvent < / Code> for older IE) have some suggestions for using.

  function addEvent (element, eventName, callback) {if (element.addEventListener) {if>} Element.addEventListener (eventName, callback, wrong); } And if (element.attachEvent) {element.attachEvent ("on" + eventName, callback); }}   

and like it:

  addEvent (window, "load", function () {addEvent (window, "size", function) {ResizeToScreen (document.getElementById ("box"), 50);});});   

Reference:

  • addEventListener :
  • addEventListener vs Onewind :

No comments:

Post a Comment