【发布时间】:2015-05-22 04:34:37
【问题描述】:
目前我有一个巨大的 div,它会从它自动生成的高度折叠到 div 标题的高度。 (即 32 像素)。我让它一开始是折叠的,然后当我单击 div 时,它会打开到完整大小,显示其所有内部信息,然后当我再次单击时,它会再次折叠。不幸的是,发生了两件事:
- div 没有完全展开到全高。
- 调整 div 中的第一个大 img 的大小。
现在我明白为什么会发生后者了。它与高度是百分比而不是离散数字有关,因为当我将数字更改为 500px 之类的东西时,它工作得很好。但我不想那样做。我需要在我需要使用大图片时保留一个百分比。
我也觉得这个问题可能和前一个问题重合,但我不确定。
请帮帮我。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>This is the title</title>
</head>
<body>
<div class="example"> <span class="h2">DIV Example</span>
<br />
<img class="big" src="http://www.greenbookblog.org/wp-content/uploads/2012/03/big-data.jpg" />
<p>This is a big picture. It's here to show what this thing is supposed to be doing.
However, this picture has been squished so that it can fit within the div nicely. I am
writing a bit so that I can take up space.</p>
<img class="big" src="http://www.greenbookblog.org/wp-content/uploads/2012/03/big-data.jpg" />
<p>This is a big picture. It's here to show what this thing is supposed to be doing. However, this picture has been squished so that it can fit within the div nicely. I am writing a bit so that I can take up space.</p>
</div>
</body>
</html>
CSS:
div.example, div.example img {
border: 3px solid #402468;
border-radius: 6px;
}
div.example {
color: white;
margin: 0 15px;
background-color: #504689;
overflow: hidden;
}
img {
display: block;
margin: 0 auto;
}
img.big {
width: 85%;
height: 85%;
}
/*further formatting: pay no mind*/
div p {
text-indent: 15pt;
margin: 0 15px;
}
.h2 {
font: 32px"Times New Roman", serif;
color: #678900;
}
aaa 和 jQuery:
$(document).one("ready", function () {
$("div.example").each(function () {
$(this).data("height0", $(this).height());
$(this).height("32px");
});
});
$(document).ready(function () {
$("div.example").click(function () {
if ($(this).height() !== 32) {
$(this).animate({
height: '32px'
});
} else {
$(this).animate({
height: $(this).data("height0")
});
}
});
});
这是小提琴: https://jsfiddle.net/AirStyle/rb95K/35/
这也是我得到的: https://jsfiddle.net/AirStyle/rb95K/35/embedded/result/
注意:对于所使用的图片,我可能没有使百分比足够小。如有必要,请减少它们。
【问题讨论】:
标签: javascript jquery css html