简体   繁体   English

如何在加载时获取元素宽度?

[英]How to get element width on load?

I wrote a slider control in JavaScript and have some concerns about it.我用 JavaScript 写了一个滑块控件,对此有些担心。 Script is initialized on window.onload and transforms all elements having class slider to sliders.脚本在window.onload上初始化,并将所有具有类滑块的元素转换为滑块。
For example:例如:

<div id="hueSlider" class="slider" min="0" max="360" value="240"></div>  

Problem is when document.load event is fired elements are not rendered yet, so I can't compute slider handle position without having width of the slider.问题是当document.load事件被触发时,元素还没有被渲染,所以我不能在没有滑块宽度的情况下计算滑块句柄位置。 I made a workaround that creates an timer for each slider that checks if slider has non-zero width then sets handle position and removes itself.我做了一个解决方法,为每个滑块创建一个计时器,检查滑块是否具有非零宽度,然后设置句柄位置并自行移除。

But that's very ugly solution.但这是非常丑陋的解决方案。 Do you have an idea how to do it in the proper way?你知道如何以正确的方式做到这一点吗?

You can always use this:你总是可以使用这个:

window.onload = function(){
    // Access elements in here
};

Or this way:或者这样:

if(document.addEventListener) {
    document.addEventListener("DOMContentLoaded", function(){
        // Access elements in here for modern browsers
    }, false);
} else {
    document.attachEvent("onreadystatechange", function(){
        if(document.readyState === "complete") {
            // Access elements in here for older browsers
        }
    });
}
function checkLoad()
{
     if (window.onLoad)
     {
          // you can call your statements in here
     } else {
          setTimeout('checkLoad();', 1000)
     }
}

Another hack you can try is:您可以尝试的另一个技巧是:

    if (window.addEventListener){

       window.addEventListener('load', ready, false)

    } else if (window.attachEvent) {

      window.attachEvent('onload', ready)
    }

EDIT:编辑:

if both of the above methods are not working, try the one below it should be fool proof and if that does not work.如果上述两种方法都不起作用,请尝试下面的一种,它应该是万无一失的,如果这不起作用。 there might be something else wrong in your code:您的代码中可能还有其他问题:

function makeDoubleDelegate(function1, function2) {
    return function() {
        if (function1)
            function1();
        if (function2)
            function2(); //call your get slider width statements in here.
    }
}

window.onload = makeDoubleDelegate(window.onload, myNewFunction );

The logic behind this is, The makeDoubleDelegate() function would be put in the first JS file, ready for invokation any time.这背后的逻辑是,makeDoubleDelegate() 函数将被放在第一个 JS 文件中,随时可以调用。 What it does is accept two functions as arguments, checks to see if they exist, and if so, return an anonymous function containing valid, existing functions.它所做的是接受两个函数作为参数,检查它们是否存在,如果存在,则返回一个包含有效的现有函数的匿名函数。

The first time that window.onload is called, window.onload itself is undefined, so what gets returned is an anonymous function with only myNewFunction() inside.第一次调用 window.onload 时,window.onload 本身是未定义的,所以返回的是一个匿名函数,里面只有 myNewFunction()。 On subsequent window.onload assignments, window.onload does exist, so whatever it holds will be wrapped up with the new function.在随后的 window.onload 分配中,window.onload 确实存在,因此它所持有的任何内容都将被新函数包裹起来。 And so the list grows.所以名单越来越长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM