Skip to content

Instantly share code, notes, and snippets.

@cssoul
Created April 25, 2013 02:18
Show Gist options
  • Save cssoul/5457019 to your computer and use it in GitHub Desktop.
Save cssoul/5457019 to your computer and use it in GitHub Desktop.
让博文中的图片不超出盒子外面,给以自适应宽度
<!--HTML-->
<div id="blog_wrap">
<div class="side"></div>
<div class="post">
<img src="/" alt="图片" style="width:900px;height:100px;"/>
</div>
</div>
<!--JavaScript-->
<script>
var a = parseFloat(document.body.clientWidth),
b = 0;
var $img = $('.post img');
for(var i=0; i<$img.length; i++){
b = parseFloat($img.eq(i).width());
//a-500表示博文区域的宽度,
if (b >= a-500){
//当博文中banner图片大于该宽度,移除style属性,用css 的max-width控制
$img.eq(i).removeAttr('style');
$img.eq(i).css('max-width','100%');
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment