Javascript – Implement touch and gesture based interaction using hammer.js

We have talked about TouchSwipe in previously.

 

Unlike TouchSwipe which is a jQuery plugin, hammer.js is independent of jQuery. It supports tap, swipe, drag and other mobile touch events. Here is an simple example.

<!doctype html>
<html>
<head>
  <title>Eureka! - hammer.js Example</title>
  <style type="text/css">
    #box {
      background-color: red;
      width: 400px;
      height: 225px;
      line-height: 225px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="box">Swipe here!</div>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script type="text/javascript" src="js/hammer.js"></script>
  <script type="text/javascript">
    var box = document.getElementById("box");

    Hammer(box).on("swipeleft", function() {
      $(this).text("You swiped left" );
    });

    Hammer(box).on("swiperight", function() {
      $(this).text("You swiped right" );
    });
  </script>
</body>
</html>

 

If you love using jQuery, you can consider the jQuery version of hammer.js.

 

Done =)

Reference: GitHub – EightMedia / hammer.js

Advertisement

One thought on “Javascript – Implement touch and gesture based interaction using hammer.js”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.