鼠标滚轮控制图片大小

<!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="加" />&nbsp;<input id="b" type="button" value="减" />
</body>
</html> 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值