您的位置:首页 > 其它

鼠标滚轮控制图片大小

2007-08-01 16:13 330 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>滚动效果</title>
<script type="text/javascript">
(function (bool) {
//兼容FF一些方法
window.FF = bool;

if (bool) {

window.attachEvent = document.attachEvent = window.HTMLElement.prototype.attachEvent = function (property, func, bool) {
//兼容attachEvent方法
return this.addEventListener(property.replace(/^on/, ""), func, bool || false);
};

window.__defineGetter__("event", function () {
//兼容Event对象
var o = arguments.callee;

do {
if (o.arguments[0] instanceof Event) return o.arguments[0];
} while (o = o.caller);

return null;
});

Event.prototype.__defineGetter__("srcElement", function () {
//兼容Event.srcElement对象
return this.target;
});

MouseEvent.prototype.__defineGetter__("wheelDelta", function () {
//返回鼠标滚轮状态
return this.detail * -1;
});
}

})(/Firefox/.test(window.navigator.userAgent));

var Class = {
//创建类
create : function () {
return function () {
this.initialize.apply(this, arguments);
};
}
};

var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};

var wheel = Class.create();

wheel.prototype = {
//鼠标滚动图片类
initialize : function (obj, num) {
//初始化参数
var wc = this;
(wc.img = obj).attachEvent(!window.FF ? "onmousewheel" : "DOMMouseScroll", function () {
wc.move(num * (window.event.wheelDelta > 0 ? -1 : 1));
});

wc.item = [
[ obj.offsetWidth, "width", "offsetWidth" ], [ obj.offsetHeight, "height", "offsetHeight" ]
].sort(function (a, b) { return b[0] - a[0]; }); //把高和宽从大到小排序
},

move : function (num) {
//改变值
var wc = this, img = wc.img, a = wc.item;

//大值除以小值在乘以固定值就是比例缩放
img.style[a[0][1]] = Math.max(
img[a[0][2]] + Math.round(a[0][0] / a[1][0] * num), a[0][0]
) + "px";
//小值不用管
img.style[a[1][1]] = Math.max(
img[a[1][2]] + num, a[1][0]
) + "px";
}

};

window.attachEvent("onload", function () {
var wc = new wheel($("div"), 10);

$("a").onclick = function () {
wc.move(50);
};

$("b").onclick = function () {
wc.move(-50);
};
});
</script>
</head>
<body>
<img id="div" height="60" width="280" src="http://hi.csdn.net/images/csdnlogo.gif" alt="" />
<br />
<input id="a" type="button" value="加" /> <input id="b" type="button" value="减" />
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: