
写了一个简单的例子,应该是你说的动态获取!
<img id="abc" src="time1gif" />
<div id="oId" style="background-color:#ccc; border:1px solid #ddd; color:#00f; width:100px; height:100px; line-height:100px; text-align:center;"></div>
<script type="text/javascript">
var oImg = documentgetElementById('abc');
var oId = documentgetElementById('oId');
var i = 1;
function checkimg(){
if(i<3){
i++;
alert(oImgsrc);
oImgsrc='time'+i+'gif';
oIdinnerHTML = oImgwidth;
}
else i = 0;
}
setInterval("checkimg()",5000)
</script>
代码大致如下供参考:
var image=new Image();imagesrc=//你的背景的src;
var divelement = documentgetElementById(你的div的id);
divelementstyleheight=(imageheightdivelementoffsetWidth)/imagewidth + "px";
几个需要注意的地方是imagesrc=xxx后需要等待直到下载完成。但是如果这个脚本是在你的div已经初始化好之后调用的话,那么已经被下载过了,所以直接进行后面的就好。另外以styleheight来设置高度的话后面需要加单位,比如px
//给你一个比较灵活的,可自由控制
jQuery(window)load(function () {
jQuery("div1 img")each(function () {//div1下的img宽度、高度设置
DrawImage(this, 700, 470);//宽700,高470,自己改为相同即可。
});
});
function DrawImage(ImgD, FitWidth, FitHeight) {//下面是判断,可自己修改条件
var image = new Image();
imagesrc = ImgDsrc;
if (imagewidth > 0 && imageheight > 0) {
if (imagewidth / imageheight >= FitWidth / FitHeight) {
if (imagewidth > FitWidth) {
ImgDwidth = FitWidth;
ImgDheight = (imageheight FitWidth) / imagewidth;
}else {
ImgDwidth = imagewidth;
ImgDheight = imageheight;
}
} else {
if (imageheight > FitHeight) {
ImgDheight = FitHeight;
ImgDwidth = (imagewidth FitHeight) / imageheight;
} else {
ImgDwidth = imagewidth;
ImgDheight = imageheight;
}
}
}
}
假如在页面有一个img标签: <img src="hyzqjpg" id="test_img" />
编写代码:
var img = documentgetElementById("test_img");
alert(imgoffsetWidth+","+imgoffsetHeight);
打印出来就是 的 宽度 和高度。 你可以试一下,Chrome绝对可以。
以上就是关于javascript 怎么获得SRC图片的高宽全部的内容,包括:javascript 怎么获得SRC图片的高宽、js获取背景图片宽高比后根据浏览器宽度动态赋值div.style.height、js控制图片高度、宽度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)