Javascript 使用Jquery Mobile更正页面事件以修改CSS

Javascript 使用Jquery Mobile更正页面事件以修改CSS,javascript,jquery,css,jquery-mobile,Javascript,Jquery,Css,Jquery Mobile,我想在页面准备就绪时动态更改div高度。(文件。就绪)。那么,我应该使用Jquery Mobile中的正确页面事件是什么 已尝试使用pagebeforeshow和pageshow事件 Pagebeforeshow事件 $(document).on 'pagebeforeshow', -> page_id = $.mobile.activePage[0].id if page_id == "brands" maxHeight = Math.max.apply(null, $

我想在页面准备就绪时动态更改div高度。(文件。就绪)。那么,我应该使用Jquery Mobile中的正确页面事件是什么

已尝试使用pagebeforeshow和pageshow事件

Pagebeforeshow事件

$(document).on 'pagebeforeshow', ->
  page_id = $.mobile.activePage[0].id

  if page_id == "brands"
    maxHeight = Math.max.apply(null, $(".product_descriptions").map(->
      $(this).height()
    ).get())

    $(".product-image").css("height", maxHeight)
    $(".circle-container").css("height", maxHeight)
$(document).on 'pageshow', ->
  page_id = $.mobile.activePage[0].id

  if page_id == "brands"
    maxHeight = Math.max.apply(null, $(".product_descriptions").map(->
      $(this).height()
    ).get())

    $(".product-image").css("height", maxHeight)
    $(".circle-container").css("height", maxHeight)
最大高度返回0

页面显示事件

$(document).on 'pagebeforeshow', ->
  page_id = $.mobile.activePage[0].id

  if page_id == "brands"
    maxHeight = Math.max.apply(null, $(".product_descriptions").map(->
      $(this).height()
    ).get())

    $(".product-image").css("height", maxHeight)
    $(".circle-container").css("height", maxHeight)
$(document).on 'pageshow', ->
  page_id = $.mobile.activePage[0].id

  if page_id == "brands"
    maxHeight = Math.max.apply(null, $(".product_descriptions").map(->
      $(this).height()
    ).get())

    $(".product-image").css("height", maxHeight)
    $(".circle-container").css("height", maxHeight)
maxHeight将返回正确的值

尽管pageshow事件返回正确的值,但所有元素都将首先显示,然后仅激活.css函数。我可以看到.css事件被触发,在手机上看起来很奇怪(屏幕反弹)

有解决方法吗?

在“pageshow”和“pagehide”事件中更改css看起来很奇怪,是的,所以您只能使用“pagebeforeshow”事件。 首先,如果您使用早于1.7.1的JQuery并尝试获取非隐藏元素的维度,那么您应该尝试使用更新的JQuery。 如果没有帮助,或者您尝试获取隐藏元素的维度,您可以尝试一些奇怪的破解方法,如下所示:

 // object is JQuery object (e.g. $('#myId'))
 function getRealHeight(object)
 {
     var clone = object.clone().show().css({'visibility':'hidden'}).appendTo('body');
     var realHeight = clone.height();
     clone.remove();
     return realHeight;
 }
这对我来说是可行的,即使在某些元素上它返回的不是精确的尺寸(例如,216而不是最终的232)。
此黑客行为的更多版本:

pagebeforeshow
应为您提供正确的高度,因为页面已完全创建。也许你应该在你的选择器中更具体一些(“#品牌.产品描述”)