JS 图片缩放、显示全图、鼠标滑轮控制显示大小

来源:互联网 发布:员工管理系统java 编辑:程序博客网 时间:2024/04/24 04:53
<html>
<head>
<script type="text/javascript">
function resizeimg(ImgD, iwidth, iheight) {
var image = new Image();
image.src = document.getElementById(ImgD).src;
if (image.width > 0 && image.height > 0) {
if (image.width / image.height >= iwidth / iheight) {
if (image.width > iwidth) {
document.getElementById(ImgD).width = iwidth;
document.getElementById(ImgD).height = (image.height * iwidth)/ image.width;
} 
else {
document.getElementById(ImgD).width = image.width;
document.getElementById(ImgD).height = image.height;
}
document.getElementById(ImgD).alt = image.width + "×" + image.height;
} 
else {
if (image.height > iheight) {
document.getElementById(ImgD).height = iheight;
document.getElementById(ImgD).width = (image.width * iheight)/ image.height;
} else {
document.getElementById(ImgD).width = image.width;
document.getElementById(ImgD).height = image.height;
}
document.getElementById(ImgD).alt = image.width + "×" + image.height;
}
document.getElementById(ImgD).style.cursor = "pointer"; //改变鼠标指针
document.getElementById(ImgD).onclick = function() {
window.open(this.src);
}; //点击打开大图片
if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器,如果是IE
document.getElementById(ImgD).title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
document.getElementById(ImgD).onmousewheel = function img_zoom() //滚轮缩放
{
var zoom = parseInt(this.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom > 0) this.style.zoom = zoom + "%";
return false;
};
} else { //如果不是IE
document.getElementById(ImgD).title = "点击图片可在新窗口打开";
}
}
}
</script>
</head>
<body onload="resizeimg('testImg',780,500)">
<img src="xxx.jpg" id="testImg" />
</body>
</html>转载自:http://blog.csdn.net/Andy_Sha/article/details/36628739
0 0