将Javascript元素调整为浏览器大小

时间:2013-02-25 18:59:55

标签: javascript css

我有一个javascript adplayer脚本,我希望在浏览器窗口大小更改时动态调整大小。我该怎么做?

    <script type="text/javascript">
    var rnd = window.rnd || Math.floor(Math.random()*10e6);
    var pid155420 = window.pid155420 || rnd;
    var plc155420 = window.plc155420 || 0;
    var abkw = window.abkw || '';
    var absrc =         'http://servedby.myexampledomain.com/adserve/;ID=162421;size=468x60;setID=155420;type=js;kw='        +abkw+';pid='+pid155420+';place='+(plc155420++)+';rnd='+rnd+'';
    document.write('<scr'+'ipt src="'+absrc+'" type="text/javascript"></scr'+'ipt>');
    </script>
    </div>

2 个答案:

答案 0 :(得分:0)

将事件处理程序附加到window.resize事件。在jQuery中,它是以下内容。

$(window).resize(function() {

    var currentWindowWidth = $(window).width();
    var currentWindowHeight = $(window).height();
    // do stuff with these variables
});

答案 1 :(得分:0)

由于有人已经提供了jQuery答案而没有任何指示jQuery实际上正在使用....

<script type="text/javascript">
    function setShtuff(){
        var rnd = window.rnd || Math.floor(Math.random()*10e6);
        var pid155420 = window.pid155420 || rnd;
        var plc155420 = window.plc155420 || 0;
        var abkw = window.abkw || '';
        var absrc = 'http://servedby.myexampledomain.com/adserve/;ID=162421;size=468x60;setID=155420;type=js;kw='        +abkw+';pid='+pid155420+';place='+(plc155420++)+';rnd='+rnd+'';
        document.write('<scr'+'ipt src="'+absrc+'" type="text/javascript"></scr'+'ipt>');
    }

    window.onload = setShtuff;
    window.onresize = setShtuff;
</script>

You can see a more complete explanation here.