首页 技术 正文
技术 2022年11月9日
0 收藏 403 点赞 4,583 浏览 3430 个字
<!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="https://img.zhankr.net/zmfbbwgfdm165412.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('https://img.zhankr.net/fcxyyxfpkna65413.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)
-->
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290