在Drupal 7中执行$(window).load()和$(window).resize()

时间:2013-12-11 19:02:47

标签: javascript jquery drupal drupal-7

我正在尝试在页面加载时重新调整几个div,并在drupal 7中调整窗口大小。

theme.info

name = Theme
description = The Theme
core = 7.x

stylesheets[all][] = css/style.css
scripts[] = js/scripts.js

(the rest)

scripts.js中

$(window).load(function() {
    getViewport();

});

$(window).resize(function() {
    getViewport();
});

function getViewport() {    
    alert('function hit');
    var viewportwidth;
    var viewportheight;
    if (typeof window.innerWidth != 'undefined')
    {
         viewportwidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; 
         viewportheight = window.innerHeight || document.documentElement.clientHeight || document.body.clientWidth;
    }
    document.getElementById("content").style.height = (window.innerHeight - 150) + 'px';
    document.getElementById('sidebar-first').style.height = (window.innerHeight - 50) + 'px';
}

但是既不加载,调整大小,也不加功能或受到攻击。我确信这是我在drupal中使用它的无知,但我似乎无法找到答案。

我甚至尝试在信息文件中包含我自己的jquery库,但无济于事。

由于

1 个答案:

答案 0 :(得分:3)

此代码无需jQuery,只需创建一些事件侦听器并终止$(window)方法链。

window.addEventListener('load', function() { getViewport() });
window.addEventListener('resize', function() { getViewport() });

注意:

如果您的目标需要低于IE9,则需要规范化addEventListener()。类似的东西:

if ( window.addEventListener ) {
  // Modern browsers
  window.addEventListener('load', function() { getViewport() });
  window.addEventListener('resize', function() { getViewport() });
} else
  // Browsers that need to die
  window.attachEvent('onLoad', function() { getViewport() });
  window.attachEvent('onResize', function() { getViewport() });
}