jquery.hammer.js – Implement delegated swipe event for inner elements

This example make use of jquery.hammer.js for implementing delegated events for inner elements. Just like what we did in the TouchSwipe Example.

 

<!doctype html>
<html>
<head>
  <title>Eureka! - hammer.js Example</title>
  <style type="text/css">
    .box {
      background-color: red;
      width: 400px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      list-style: none;
      margin-bottom: 10px;
    }
  </style>
</head>
<body>
  <ul id="boxes">
    <li class="box">Old box</li>
    <li class="box">Old box</li>
    <li class="box">Old box</li>
  </ul>
  <button id="add">Add box</button>
  <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" src="js/jquery.hammer.min.js"></script>
  <script type="text/javascript">
    $("#add").click(function(){
      $("#boxes").append('<li class="box">New box</li>');
    });

    var hammertime = $("#boxes").hammer();

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

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

 

Done =)

Reference:

Advertisement

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.