控制图片大小的问题

yanbinquan 2011-01-22 10:56:53
我在网页中插入图片

<div style=" text-align:center; padding:10px; width:95%; margin:0 auto" class="border" id="proc">
<asp:Image ID="product_img" runat="server"/>
</div>


用jquery控制该图片宽度

<script language="javascript">
$(document).ready(function() {
var maxwidth = 550;
if ($("#proc img").attr("width") > maxwidth) {
$("#proc img").attr("width", maxwidth);
};
})
</script>


但是,打开页面的时候,图片有时候会是按原来大小显示,刷新页面才被控制宽度。
请问这是什么问题,该怎么解决
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovehong123 2011-01-24
  • 打赏
  • 举报
回复
图片加载出来的时候就直接去设置图片
# $(window).load(function() {
var maxwidth = 550;
if ($("#proc img").attr("width") > maxwidth) {
$("#proc img").attr("width", maxwidth);
};

});

还有一个办法就是直接写死的,这样不管什么图片都会显示你规定的大小了
<asp:Image ID="product_img" runat="server" width="550"/>
phoebuswei 2011-01-23
  • 打赏
  • 举报
回复
写死他,一出来就是规定样式
需要修改在通过javascript
<asp:Image ID="product_img" runat="server" width=‘550’/>
yanbinquan 2011-01-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxr0323 的回复:]

JScript code
# $(window).load(function() {
var maxwidth = 550;
if ($("#proc img").attr("width") > maxwidth) {
$("#proc img").attr("width", maxwidth);
……
[/Quote]

这样可以实现效果了,谢谢。
不过页面载入的时候,图片先显示原始大小,再缩到规定的大小,不是很美观,有办法做得更好吗?
子夜__ 2011-01-22
  • 打赏
  • 举报
回复
# $(window).load(function() {
var maxwidth = 550;
if ($("#proc img").attr("width") > maxwidth) {
$("#proc img").attr("width", maxwidth);
};

});
wuyq11 2011-01-22
  • 打赏
  • 举报
回复
等比例缩放图片
<script language="JavaScript">
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}

}
}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧