Kinetic js滚动鼠标滚轮事件

时间:2014-03-28 14:11:53

标签: jquery mousewheel

我正在使用kinetic js在div上实现滚动,它运行正常,但我还需要滚动鼠标滚轮事件。

<div id="wrapper"  style="cursor: move;">
 <div id="inner">
  <img src="http://davetayls.me/jquery.kinetic/wembley.jpg" alt="wembley stadium">
 </div>
</div>

JQuery的

  $('#wrapper').kinetic();

我不知道该怎么做,请帮助我。

Fiddle

1 个答案:

答案 0 :(得分:0)

使用Brandon Aaron的jwheel鼠标插件。它运作良好!

https://github.com/brandonaaron/jquery-mousewheel

示例代码(请务必下载jquery.mousewheel.js并相应地调整脚本标记)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <script src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.1.min.js"></script>
    <script src="includes/jquery.mousewheel.js"></script>
<style>
body{padding:20px;}
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:350px;
  height:350px;
}
</style>        
<script>
$(function(){

    var stage = new Kinetic.Stage({
        container: 'container',
        width: 350,
        height: 350
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);


    var circle1 = new Kinetic.Circle({
        x:100,
        y:100,
        radius: 30,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4,
        draggable: true
    });
    layer.add(circle1);
    layer.draw();


    $('#container').bind('mousewheel', function(event) {
      event.preventDefault();
      event.stopPropagation();
      var deltaY=event.deltaY;
      circle1.y(circle1.y()-deltaY*10);
      layer.draw();
    });


}); // end $(function(){});

</script>       
</head>

<body>
    <h4>Click in the gray rect to get focus then<br>Scroll the mousewheel to move the circle.</h4>
    <div id="container"></div>
</body>
</html>