javascript - 使用关键 CSS 时,Jquery 函数不会在加载时运行

标签 javascript jquery css optimization resize

我正在尝试优化我的网站 ( http://www.mazion.co.uk )。

因此,我尝试使用 penthouse 为网站创建关键 CSS。 (请参阅使用的关键 CSS here - 这是由 penthouse 的主要开发人员为我生成的)。

但是,当使用关键 CSS 时,我网站上的其中一个子页面无法正确加载。但是,当我完全内联 CSS(或不做任何优化 CSS 的事情)时,这个子页面会正确加载。

在此子页面上 - http://www.mazion.co.uk/courses ,有许多框使用 JS 函数(见下文)调整大小,该函数在 on.ready 和 on.resize(即调整屏幕大小时)运行,确保所有框的大小相同。

当使用 critical CSS 时,调整大小功能在 resize 上有效,但在 ready 上无效。另一方面,使用内联 CSS,调整大小功能按预期工作 on.resize 和 on.ready...

因此,我想知道是否有人可以帮助我确定问题所在。我试图将框的样式直接内联到 HTML 中,但我没有成功...

您可以转到http://www.mazion.co.uk/courses/ 查看此问题。看看盒子。如果您随后调整浏览器的大小,所有框将自行调整大小,使它们的高度都相同......这种使所有框具有相同高度的调整实际上应该在页面加载时自动发生......

Js Function(不是很重要的问题,但有助于设置场景)

jQuery(document).ready(function($){

  $(window).resize(function() {
    resizeCourseBoxes()
    resizeTopBespokeCoursesBoxes()
    resizeMidBespokeCoursesBoxes()
  }).resize(); // Trigger resize handlers.
});

// Ensure that all the courses boxes are the same height (this ensures that the rows are of the same size...)
function resizeCourseBoxes() {
  jQuery(function($) {
    courseHeader = $('.course_header')
    maxTextHeight = Math.max.apply(
    Math, courseHeader.map(function() {
      return $(this).height()
    }).get())

    for (var i = 0; i < courseHeader.length; i++) {
      currentHeight = courseHeader[i].offsetHeight
      new_padding = Number(maxTextHeight) - currentHeight + 10
      courseHeader[i].style.marginBottom = new_padding + 'px'
    };
  })
}

// Ensure that all mid section (prices section) of the bespoke section is the same
function resizeTopBespokeCoursesBoxes() {
  jQuery(function($) {

    CoursePriceSection = $('.green_bx_top')
    maxTextHeight = Math.max.apply(
    Math, CoursePriceSection.map(function() {
      return $(this).height()
    }).get())

    for (var i = 0; i < CoursePriceSection.length; i++) {
      currentHeight = CoursePriceSection[i].offsetHeight
      new_padding = Number(maxTextHeight) - currentHeight + 10
      CoursePriceSection[i].style.marginBottom = new_padding + 'px'
    };
  })
}

// Ensure that all mid section (prices section) of the bespoke section is the same
function resizeMidBespokeCoursesBoxes() {
  jQuery(function($) {

    CoursePriceSection = $('.green_bx_mid')
    maxTextHeight = Math.max.apply(
    Math, CoursePriceSection.map(function() {
      return $(this).height()
    }).get())

    for (var i = 0; i < CoursePriceSection.length; i++) {
      currentHeight = CoursePriceSection[i].offsetHeight
      new_padding = Number(maxTextHeight) - currentHeight
      CoursePriceSection[i].style.marginBottom = new_padding + 'px'
    };
  })
}

最佳答案

我的问题的答案很简单:

关键 CSS 特定于每个 HTML 页面。因此,每个单独页面的关键 CSS 应该单独计算...

(我对我所有的子页面都使用了相同的关键 CSS...)。

关于javascript - 使用关键 CSS 时,Jquery 函数不会在加载时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28289634/

相关文章:

html - 当元素值为空时 CSS3 中断

javascript - jQuery 附加不需要的数据

javascript - 如何在通常通过 Javascript 设置的 JMeter 中设置 cookie?

javascript - JavaScript 中的循环出错?

jQuery:将一个选择的集合添加到另一个相同类型的集合

javascript - 在 css3 中使用百分比创建预加载器

javascript - HTML语音聊天宽度Phonegap和node.js

javascript - 按属性值对数组中的对象进行计数并以 HTML 形式显示结果

javascript - 如何在 JQuery 中暂停图像在鼠标上滑动

html - 多个类的CSS