【问题标题】:Executing $(window).load() and $(window).resize() in Drupal 7在 Drupal 7 中执行 $(window).load() 和 $(window).resize()
【发布时间】:2013-12-11 19:02:46
【问题描述】:

我正在尝试在 drupal 7 中调整页面加载和窗口大小时调整几个 div 的大小。

主题信息

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 库,但无济于事。

谢谢

【问题讨论】:

  • 控制台有错误吗?是否加载了jquery?等等……
  • 我不确定如何检查。 Drupal 菜鸟,对不起。
  • 在浏览器中,按F12打开控制台,看看有没有错误提示
  • TypeError: $ 不是函数 $(window).load(function() {
  • 表示未加载 jquery。您必须包含它,但恐怕我不是 drupal 用户。我猜,你的 jquery 路径不准确

标签: javascript jquery drupal drupal-7


【解决方案1】:

此代码无需 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() });
}

【讨论】:

  • 是的,看起来很公平...... +1
  • 成功了,谢谢。虽然我仍然想让 jquery 在某个时候在项目中工作。
  • 您应该将您的 Drupal 代码发布到另一个问题,以便其他人可以帮助您...不幸的是(或幸运的是?)我从未使用过 Drupal。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多