【问题标题】:How to get the height of an image and apply that height to a div?如何获取图像的高度并将该高度应用于 div?
【发布时间】:2012-11-14 06:03:26
【问题描述】:

我正在构建一个移动网络应用程序,并且我正在使用 jquerytools 滑块。

我希望滑块在所有移动设备上显示(以适当的比例),因此图像的宽度为 100%,高度在 css 中为自动。然而,由于所有元素都是浮动的,并且 jquerytools 滑块要求将位置设置为绝对位置,因此包含的 div (#header) 不会拉伸以适应内容。

我正在尝试使用 jquery 来获取 img 的高度并将该高度应用于标题....但是我没有运气。

CSS:

    #header{
    width:100%;
    position:relative;
    z-index: 20;
    /* box-shadow: 0 0 10px white; */
    overflow: auto;
    }

.scrollable {
position:relative;
overflow:hidden;
width: 100%;
height: 100%;
/* box-shadow: 0 0 20px purple; */
/*  height:198px; */
z-index: 20;
overflow: auto;
}


.scrollable .items {
/* this cannot be too large */
width:1000%;
position:absolute;
clear:both;
/* box-shadow: 0 0 30px green; */

}

.items div {
float:left;
width:10%;
height:100%;

}

/* single scrollable item */
.scrollable img {
    /* float:left; */
    width:100%;
   height: auto;
 /*    height:198px; */
}

/* active item */
.scrollable .active {
    border:2px solid #000;
    position:relative;
    cursor:default;
}

HTML

<div id=header><!-- root element for scrollable -->
<div class="scrollable" id="scrollable">

  <!-- root element for the items -->
  <div class="items">


    <div>
      <img src="img/img2.jpg" />
    </div>


    <div>
       <img src="img/img1.jpg" />
    </div>


    <div>
     <img src="img/img3.jpg" />
    </div>

    <div>
     <img src="img/img4.jpg" />
    </div>

    <div>
     <img src="img/img6.jpg" />
    </div>

</div><!-- items -->

</div><!-- scrollable -->

</div><!-- header -->

更新的 JQUERY

            <script>
        $(document).ready(function() {
        $("img.scrollable").each(function() {
            $('#header').css("height",$(this).innerHeight());
        });
        });
        </script>

【问题讨论】:

  • 是的,对不起,我刚刚注意到了。我刚刚回顾了我在这里提出的问题并勾选了相关答案,那里有一个。感谢您指出这一点!

标签: jquery css


【解决方案1】:

查看这个 jquery 函数:http://api.jquery.com/height/

【讨论】:

  • 谢谢,但我一直在玩这个。我将用我刚刚使用的 jquery 更新我原来的问题。
猜你喜欢
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
相关资源
最近更新 更多