码迷,mamicode.com
首页 > Web开发 > 详细

js禁用默认鼠标滑轮

时间:2015-03-06 23:26:34      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

一、参考插件

jQuery mouse wheel

二、样例代码

<!doctype html>
<html>
  <head>
    <title>Scroll Test</title>
    <style>
      html, body { margin: 0; padding: 0; width: 100%; height: 100%; }
      #emulated, #native {
        width: 40%;
        height: 100%;
      }
      #emulated { float: left; overflow: hidden; }
      #native { float: right; overflow: auto; }
    </style>
    <script>
      (function() {
        var verMatch = /v=([\w\.]+)/.exec(location.search),
            version = verMatch && verMatch[1],
            src;
        if (version)
          src = ‘code.jquery.com/jquery-‘ + version;
        else
          src = ‘code.jquery.com/jquery-git‘;
        document.write(‘<script src="http://‘ + src + ‘.js"><\/script>‘);
      })();
    </script>
    <script src="../jquery.mousewheel.js"></script>
    <script>
      var lorem = ‘Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus rhoncus nibh ac ultricies blandit. Nunc blandit blandit lobortis. Maecenas id dolor scelerisque, facilisis dolor eu, interdum urna. Nullam consectetur lectus quis mi interdum accumsan. Nulla malesuada est nec neque suscipit pulvinar. Vivamus sagittis, nunc a porttitor tempus, mi neque eleifend diam, nec porttitor metus dui a orci. Cras tempus lobortis nisl ut sagittis. Maecenas semper in magna mollis venenatis. Vestibulum fermentum tincidunt fringilla.‘;
      $(function() {
        for (var i=0; i<30; i++) {
          var html = ‘<p>‘ + i + ‘ ‘ + lorem + ‘</p>‘;
          $(‘#emulated‘).append(html);
          $(‘#native‘).append(html);
        }
        $(‘#native‘).bind(‘mousewheel‘, function(event) {
          event.preventDefault();
          var scrollTop = this.scrollTop;
          this.scrollTop = (scrollTop + ((event.deltaY * event.deltaFactor) * -1));
          //console.log(event.deltaY, event.deltaFactor, event.originalEvent.deltaMode, event.originalEvent.wheelDelta);
        });
      });
    </script>
  </head>
  <body>
    <div id="emulated"></div>
    <div id="native"></div>
  </body>
</html>

三、done

js禁用默认鼠标滑轮

标签:

原文地址:http://www.cnblogs.com/itliucheng/p/4319453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!