js获取图片的原始尺寸
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="Packages/jQuery.1.8.3/jquery-1.8.3.min.js"></script>
</head>
<body>
<img id="imgT" style="width:500px;height:150px" src="http://img11.360buyimg.com/da/g14/M07/01/0E/rBEhVlNhh8wIAAAAAADmFBLo1twAAM26gOmCgYAAOYs716.jpg"/>
<input id="Text1" type="text" style="padding:1px;margin:1px;height:30px;width:50px" value="ceshi" />
<script>
//获取图片的长宽,会受css的width与height影响。
//获取的结果是css的width与height
//看下:http://www.cnblogs.com/chengxiaohui/articles/1873605.html
$(function () {
$('img').load(function () {
//var msg = "width:" + $(this).width() + "height:" + $(this).height();
var msg = "width:" + this.width + "height:" + this.height;
alert(msg);
})
})
//获取图片原始尺寸
//load方法绑定给window对象时,会在所有内容加载后触发,包括窗口,框架,对象和图像。
//$(document).ready()是在所有DOM节点加载完毕后执行。
//而往往在DOM节点加载完毕后,img图象还未完全加载完毕的。
//此处使用$(document).ready()获取的长宽都是0,采用$(window).load()
$(window).load(function () {
////方式一
//$("<img/>").attr("src", $('#imgT').attr("src")).load(function () {
// var msg = "width:" + this.width + "height:" + this.height;//获取长宽:this.width 。不能用$(this).width()
// alert(msg);
//}) ////方式二
var img = $('#imgT');
var theImage = new Image();
theImage.src = img.attr('src');
var msg = "width:" + theImage.width + "height:" + theImage.height;//长宽theImage.width。不能用$(theImage).width()
alert(msg);
})
//jQuery中 height(width) innerHeight(innerWidth) outerHeight(outerWidth)的区别
$(function () {
var width = "width():" + $("#Text1").width() +
"\r\ninnerWidth():" + $("#Text1").innerWidth() +
"\r\nouterWidth():" + $("#Text1").outerWidth() +
"\r\nouterWidth():" + $("#Text1").outerWidth(true);
alert(width); }) //此为通过图片头数据获取长宽
//原址:http://www.planeart.cn/?p=1121
//demo:http://www.planeart.cn/demo/imgReady/
// 更新:
// 05.27: 1、保证回调执行顺序:error > ready > load;2、回调函数this指向img本身
// 04-02: 1、增加图片完全加载后的回调 2、提高性能 /**
* 图片头数据加载就绪事件 - 更快获取图片尺寸
* @version 2011.05.27
* @author TangBin
* @see http://www.planeart.cn/?p=1121
* @param {String} 图片路径
* @param {Function} 尺寸就绪
* @param {Function} 加载完毕 (可选)
* @param {Function} 加载错误 (可选)
* @example imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {
alert('size ready: width=' + this.width + '; height=' + this.height);
});
*/
var imgReady = (function () {
var list = [], intervalId = null, // 用来执行队列
tick = function () {
var i = 0;
for (; i < list.length; i++) {
list[i].end ? list.splice(i--, 1) : list[i]();
};
!list.length && stop();
}, // 停止所有定时器队列
stop = function () {
clearInterval(intervalId);
intervalId = null;
}; return function (url, ready, load, error) {
var onready, width, height, newWidth, newHeight,
img = new Image(); img.src = url; // 如果图片被缓存,则直接返回缓存数据
if (img.complete) {
ready.call(img);
load && load.call(img);
return;
}; width = img.width;
height = img.height; // 加载错误后的事件
img.onerror = function () {
error && error.call(img);
onready.end = true;
img = img.onload = img.onerror = null;
}; // 图片尺寸就绪
onready = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果图片已经在其他地方加载可使用面积检测
newWidth * newHeight > 1024
) {
ready.call(img);
onready.end = true;
};
};
onready(); // 完全加载完毕的事件
img.onload = function () {
// onload在定时器时间差范围内可能比onready快
// 这里进行检查并保证onready优先执行
!onready.end && onready(); load && load.call(img); // IE gif动画会循环执行onload,置空onload即可
img = img.onload = img.onerror = null;
}; // 加入队列中定期执行
if (!onready.end) {
list.push(onready);
// 无论何时只允许出现一个定时器,减少浏览器性能损耗
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})(); </script>
</body>
</html>
<!--说明-->
<!--jQuery中 height(width) innerHeight(innerWidth) outerHeight(outerWidth)的区别-->
<!--
height(width):高度(宽度)
innerHeight(innerWidth):高度(宽度)+内边距(padding)
outerHeight(outerWidth):高度(宽度)+内边距(padding)+边框
outerHeight(outerWidth) 参数为true时:高度(宽度)+内边距(padding)+边框+外边距(margin)
-->
js获取图片的原始尺寸的更多相关文章
- js获取图片信息(一)-----获取图片的原始尺寸
如何获取图片的原始尺寸大小? 如下,当给 img 设置一个固定的大小时,要怎样获取图片的原始尺寸呢? #oImg{ width: 100px; height: 100px; } <img src ...
- JS获取图片的原始宽度和高度
页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能首先想到的是元素的innerWidth属性,或者jQuery中的width()方法.如下: <img id="img" ...
- 如何用JavaScript在浏览器端获取图片的原始尺寸大小?
var img = $("#img_id"); // Get my img elem var pic_real_width, pic_real_height; $("&l ...
- JavaScript获取图片的原始尺寸
页面里的img元素,想要获取它的原始尺寸,以宽度为例可能首先想到的就是width,如下 <img src="http://img11.360buyimg.com/da/g14/M07/ ...
- JS获取图片的原始宽度和高度,兼容IE7,8
naturalWidth和naturalHeight 可以直接获取img的原始宽高,而innerHight,innerWith只是获取图片所占容器盒子的宽高. // 封装function getNat ...
- jq获取图片的原始尺寸,自适应布局
原理: each()遍历,width().height()获取宽高, load() 注意: 由于页面加载完了,但图片不一定加载完了,所以直接通过 $("img").width(), ...
- 【记录】JS 获取图片原始尺寸-防止图片溢出
示例代码: <div id="div_content"> <img src="http://static.cnblogs.com/images/logo ...
- Js获取图片原始宽高
如果我们页面看到的图片都是缩略图,那就需要做个图片点击放大效果,那么怎样获取图片的原始宽高呢?方法如下: //获取图片原始宽度 function getNaturalWidthAndHeight(im ...
- JS获取图片实际宽高及根据图片大小进行自适应
JS获取图片实际宽高,以及根据图片大小进行自适应 <img src="http://xxx.jpg" id="imgs" onload="ad ...
随机推荐
- AutoCAD.net支持后台线程-Socket通讯
最近因为公司项目的需求,CAD作为服务端在服务器中常驻运行,等待客户端远程发送执行任务的指令,最终确认用Socket-tcp通讯,CAD需要实时监听客户端发送的消息,这时就需要开启线程执行Socket ...
- Android 编程下的 TraceView 简介及其案例实战
TraceView 是 Android 平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到 method.详细内容参考:Profiling with ...
- [我的阿里云服务器] —— WordPress Permalink Settings
前言: 固定链接(Permalink)是博客日志.分类及其他博客内容列表的永久URL. 别人可以通过固定链接链接到你的文章上,你也可以在email中发送某篇日志的链接. 所有日志的URL应为永久性.固 ...
- 【SPL标准库专题(1)】 SPL简介
什么是SPL SPL是Standard PHP Library(PHP标准库)的缩写. 根据官方定义,它是"a collection of interfaces and classes th ...
- Prometheus Node_exporter 之 Node Exporter
Node Exporter 1. Node Exporter Scrape Time type: GraphUnit: secondsLabel: Seconds{{collector}} - 各个收 ...
- python新生类和经典类简单说明
经典类: #!/usr/bin/env python #*-* coding:utf-8 *-* class A(): def __init__(self): print 'my name is GF ...
- Oracle EBS 查看双节点是否做了信任
perl $AD_TOP/patch/115/bin/txkRunSSHSetup.pl verifyssh -contextfile=$CONTEXT_FILE -hosts=erpapp1,erp ...
- Oracle EBS OPM 事务处理
--事务处理 --created by jenrry DECLARE l_iface_rec inv.mtl_transactions_interface%ROWTYPE; l_iface_lot_r ...
- SQL Server 2014内存优化表的使用场景(转载)
最近一个朋友找到走起君,咨询走起君内存优化表如何做高可用的问题 大家知道,内存优化表作为In-Memory OLTP功能是从SQL Server 2014开始引入,用来对抗Oracle 12C的In- ...
- MySQL中的xtrabackup的原理解析
xtrabackup的官方下载地址为 http://www.percona.com/software/percona-xtrabackup. xtrabackup包含两个主要的工具,即xtraback ...