<!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获取图片的原始尺寸的更多相关文章

  1. js获取图片信息(一)-----获取图片的原始尺寸

    如何获取图片的原始尺寸大小? 如下,当给 img 设置一个固定的大小时,要怎样获取图片的原始尺寸呢? #oImg{ width: 100px; height: 100px; } <img src ...

  2. JS获取图片的原始宽度和高度

    页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能首先想到的是元素的innerWidth属性,或者jQuery中的width()方法.如下: <img id="img" ...

  3. 如何用JavaScript在浏览器端获取图片的原始尺寸大小?

    var img = $("#img_id"); // Get my img elem var pic_real_width, pic_real_height; $("&l ...

  4. JavaScript获取图片的原始尺寸

    页面里的img元素,想要获取它的原始尺寸,以宽度为例可能首先想到的就是width,如下 <img src="http://img11.360buyimg.com/da/g14/M07/ ...

  5. JS获取图片的原始宽度和高度,兼容IE7,8

    naturalWidth和naturalHeight 可以直接获取img的原始宽高,而innerHight,innerWith只是获取图片所占容器盒子的宽高. // 封装function getNat ...

  6. jq获取图片的原始尺寸,自适应布局

    原理: each()遍历,width().height()获取宽高, load() 注意: 由于页面加载完了,但图片不一定加载完了,所以直接通过 $("img").width(), ...

  7. 【记录】JS 获取图片原始尺寸-防止图片溢出

    示例代码: <div id="div_content"> <img src="http://static.cnblogs.com/images/logo ...

  8. Js获取图片原始宽高

    如果我们页面看到的图片都是缩略图,那就需要做个图片点击放大效果,那么怎样获取图片的原始宽高呢?方法如下: //获取图片原始宽度 function getNaturalWidthAndHeight(im ...

  9. JS获取图片实际宽高及根据图片大小进行自适应

    JS获取图片实际宽高,以及根据图片大小进行自适应  <img src="http://xxx.jpg" id="imgs" onload="ad ...

随机推荐

  1. 小程序 js中获取时间new date()的用法(网络复制过来自用)

    js中获取时间new date()的用法   获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获 ...

  2. 【读书笔记】iOS-网络-错误处理的经验法则

    一,在接口契约中处理错误. 二,错误状态可能不正确. 设备模糊地确认操作是崇拜失败的.比如,移动应用发出HTTP请求以在两个账户间转账.请求被银行系统接收并正确地处理:然而,由于网络失败应答却丢失了, ...

  3. Python Python-MySQLdb中的DictCursor使用方法简介

    Python-MySQLdb中的DictCursor使用方法简介 by:授客 QQ:1033553122     DictCursor的这个功能是继承于CursorDictRowsMixIn,这个Mi ...

  4. Oracle 用户、角色管理简介

    Oracle 用户.角色管理简介 by:授客 QQ:1033553122 创建用户 形式1:创建名为testacc2的用户 CREATE USER testacc2 IDENTIFIED BY abc ...

  5. Angular调用父Scope的函数

    app.directive('toggle', function(){ return { restrict: 'A', template: '<a ng-click="f()" ...

  6. aop 拦截含有特定注解的类

    1.功能点:使用aop拦截含有自定义注解的类 1.自定义注解 package com.zhuanche.common.dingdingsync; import java.lang.annotation ...

  7. 2018-9 Java.lang.StackOverflowError

    问题: Java.lang.StackOverflowError at com.**Logger.**.**.StringFilter.isValueNull(StringFilter.java:81 ...

  8. 使用FireFox插件RESTClient、HttpRequester模拟http(get post)请求

    我们写好一个接口后,需要进行测试.有时我们会写一个html表单提交,无疑增加了工作量,尤其是当参数比较多或者传json或xml数据时,效率更是大大降低.我们可以使用基于FireFox的RESTClie ...

  9. Oracle EBS FA 资产编号跳号

  10. October 27th, 2017 Week 43rd Friday

    The only thing predictable about life is its unpredictability. 人生唯一可以预知的,就是它的变化莫测. Is it really unpr ...