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 ...
随机推荐
- CentOS7安装mongodb
1.下载mongodb的*.tar.gz安装包 2.移到centos7中并解压 tar -xzvf mongodb.tar.gz 3.配置环境变量 vim /etc/profile 添加如下内容: # ...
- 【代码笔记】iOS-NSTimer
代码: RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewControl ...
- React之小知识点总结
总结react中常常被忽略的小知识点 1)即使state里设置成和之前的值一样,render也会重新渲染 2)父组件传给子组件的属性(props是只读的,在子组件中已在this.state里将属性赋值 ...
- React之函数中的this指向
我们都知道在React中使用函数时,有两种写法,一是回调函数,二是直接调用,但需要在构造函数中绑定this,只有这样,函数中的this才指向本组件 总结一下没有绑定this的函数中的this指向 不管 ...
- ViewPager实现Recycle机制和响应notifyDataSetChanged
1.目标 主界面要求水平移动翻页效果,每次只能翻一页,可以翻无数页. 2.实现思路 针对"每次只能翻一页"这个要求,简单使用SDK的话只有用ViewPager.ViewPager的 ...
- phar 反序列化学习
前言 phar 是 php 支持的一种伪协议, 在一些文件处理函数的路径参数中使用的话就会触发反序列操作. 利用条件 phar 文件要能够上传到服务器端. 要有可用的魔术方法作为"跳板&qu ...
- java持有对象【1】容器类及ArrayList
如果一个程序只包含固定数量的且其生命周期都是已知的对象,那么这是一个非常简单的程序. ----------java编程思想第十一章引言 java有许多方式引用对象,例如学过的数组,他是编译器支持的类 ...
- 缓存那些事-zz
https://tech.meituan.com/cache_about.html 前言 一般而言,现在互联网应用(网站或App)的整体流程,可以概括如图1所示,用户请求从界面(浏览器或App界面)到 ...
- ejb-jar.xml
所有bean类(无论是会话bean还是实体bean)必须实现的最基本的接口是javax.ejb.EnterpriseBean接口. 所有的会话bean必须实现javax.ejb.SessionBean ...
- Spring Cloud 子项目介绍
Spring Cloud由以下子项目组成. Spring Cloud Config 配置中心——利用git来集中管理程序的配置. 项目地址:https://spring.io/projects/spr ...