Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30467258
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-14 13:49:34

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="">
  3.  <head>
  4.   <title> ImageZoom </title>
  5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.  </head>
  7.  <body>
  8.   <img src="3.jpg" id="photo"/>
  9.   <script>
  10.     /*!
  11.         ImageZoom
  12.     */
  13.     var ImageZoom = function() {
  14.         var Event = {}, ua = navigator.userAgent, isIE = /msie/i.test(ua), isFF = /firefox/i.test(ua);
  15.         var zoom = 100, min = 50, o = document.getElementById('photo'), w = o.offsetWidth, h = o.offsetHeight;
  16.         Event.addEvent = function(obj, type, fn) { window.addEventListener ? obj.addEventListener(type, fn, false) : obj.attachEvent('on'+type, fn); };
  17.         Event.format = function(event) {
  18.             if(isIE) event.preventDefault = function() { event.returnValue = false };
  19.             event.data = event.wheelDelta || -event.detail*40;
  20.             return event;
  21.         }
  22.         function ZOom(event) {
  23.             var e = Event.format(event);
  24.             zoom += e.data / 12;
  25.             if(zoom > min) {
  26.                 if(isIE) {
  27.                     o.style.zoom = zoom + '%';
  28.                 } else {
  29.                     o.style.width = w * zoom / 100 + 'px';
  30.                     o.style.height = h * zoom / 100 + 'px';
  31.                 }
  32.                 e.preventDefault();
  33.             }
  34.         }
  35.         return {
  36.             init: function() {
  37.                 Event.addEvent(o, isFF? 'DOMMouseScroll' : 'mousewheel', ZOom);
  38.             }
  39.         }
  40.     }();

  41.     ImageZoom.init();
  42.   </script>
  43.  </body>
  44. </html>

主要是滚轮鼠标的兼容性:

1. 绑定滚轮事件. 这次只有FF浏览器特殊用DOMMouseScroll 其余浏览器使用onmousewheel. 代码可以写为:


Event.addEvent(o, isFF? 'DOMMouseScroll' : 'mousewheel', ZOom);

2. 控制滚轮数值大小和方向. 仅有FF使用 detail 其它的都用wheelDelta. detail每次滚动的值为加减3 而wheelDelta为加减120. 且两者加减针对的方向正好相反.


event.data = event.wheelDelta || -event.detail*40;
阅读(1430) | 评论(0) | 转发(0) |
0

上一篇:javascript画圆

下一篇:javascript模板技术

给主人留下些什么吧!~~