touchSwipe - Documentation

Introduction

What is touchSwipe?

touchSwipe is a lightweight JavaScript library that provides a simple and intuitive way to add touch swipe gestures to your web applications. It allows developers to easily detect and respond to various swipe directions (left, right, up, down) and other touch events on elements within their web pages. It abstracts away the complexities of handling different browsers and devices, providing a consistent cross-platform experience.

Why use touchSwipe?

Using touchSwipe offers several key advantages:

Browser Compatibility

touchSwipe is designed to work across a wide range of modern browsers and devices. While striving for maximum compatibility, optimal functionality is best achieved in browsers with robust touch event support. We recommend testing your implementation across your target browsers. Generally, good support exists in:

Getting Started: Installation and Setup

There are several ways to include touchSwipe in your project:

1. Downloading the library:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>  <!-- Or your jQuery path -->
<script src="jquery.touchSwipe.min.js"></script>

2. Using a CDN:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>  <!-- Or your jQuery path -->
<script src="[CDN link for touchSwipe]"></script>

3. Using a Package Manager (npm):

If using npm, you would install it via the command line and then import it into your project.

After including the library, you can start using the touchSwipe functions in your JavaScript code to add swipe functionality to your elements. Refer to the API documentation for details on available functions and options.

Core Functionality

Swipe Events

touchSwipe triggers several events based on swipe gestures. These events are triggered on the element to which the touchSwipe functionality is applied. The primary events are:

Each event passes an event object containing relevant data like the swipe direction and distance. Consult the detailed API documentation for the specific properties of each event object.

Event Handling

Event handling is done using jQuery’s standard on() method (or the older bind() method). You bind the desired event handler to your element.

For example, to handle a swipeLeft event on an element with the ID “myElement”:

$('#myElement').on('swipeLeft', function(event){
  console.log("Swipe Left detected!");
  //Your code to handle the swipe left event
});

Similarly, you can handle other swipe events (swipeRight, swipeUp, swipeDown, swipe) using the appropriate event names. The swipeStatus event allows for more granular control and real-time feedback.

$('#myElement').on('swipeStatus', function(event, phase, direction, distance, duration, fingers) {
    // phase: start, move, cancel, end
    // direction: left, right, up, down
    // distance: the distance of the swipe
    // duration: the time of the swipe
    // fingers: the number of fingers used in the swipe
    console.log("Swipe Status:", phase, direction, distance);

    // Example: Show visual feedback based on swipe status
    if (phase === "move") {
      // Update a progress bar or indicator
    }
});

Configuring Swipe Sensitivity

The sensitivity of swipe detection can be adjusted using the threshold option. This option determines the minimum distance (in pixels) the user must swipe before a swipe event is triggered. A higher threshold makes the detection less sensitive, requiring a larger swipe.

This option is passed as part of the touchSwipe() options object. For instance:

$('#myElement').touchSwipe({
  threshold: 100 // Requires a swipe of at least 100 pixels
});

Controlling Swipe Direction

By default, touchSwipe detects swipes in all four directions. However, you can restrict the detection to specific directions using the allowPageScroll option. This option takes a string value combining ‘vertical’ and ‘horizontal’ to control scroll directions, or ‘none’ to disable scrolling completely during swipes.

For example, to allow only horizontal swipes:

$('#myElement').touchSwipe({
  allowPageScroll: 'vertical' //Only vertical scrolling is allowed
});

To disable all scrolling while swiping:

$('#myElement').touchSwipe({
  allowPageScroll: 'none'
});

Handling Multiple Gestures

touchSwipe can handle multiple gestures on a single element. Simply bind multiple event handlers to the element. Each event handler will be triggered independently when the corresponding gesture occurs.

For example:

$('#myElement').on('swipeLeft', function(){
  //Handle left swipe
}).on('swipeRight', function(){
  //Handle right swipe
});

You can even combine swipe events with other jQuery touch events or mouse events for more complex interactions. Remember to account for potential conflicts if multiple events could be triggered simultaneously.

Advanced Configuration

Customizing Swipe Thresholds

While the global threshold option sets a minimum swipe distance, you can further refine swipe detection by customizing thresholds for individual directions. This isn’t directly supported by a single option, but can be achieved by checking the distance property within the swipeStatus event and only acting on swipes that meet specific distance requirements for each direction.

$('#myElement').on('swipeStatus', function(event, phase, direction, distance) {
  if (phase === "end") {
    if (direction === "left" && distance > 150) { // Requires a longer swipe left
      // Handle left swipe
    } else if (direction === "right" && distance > 50) { // Shorter swipe right is okay
      // Handle right swipe
    }
  }
});

This approach provides granular control over the sensitivity of swipes in different directions.

Implementing Swipe Triggers

By default, touchSwipe automatically detects swipes. However, you might want to trigger actions based on specific conditions in your application logic, rather than relying solely on the automatic detection. This is best done using the swipeStatus event and conditionally triggering your actions based on the phase property. For example, you might trigger an action only when the swipe ends (phase === "end") and meets certain distance or velocity criteria.

let swipeTriggered = false; // Flag to prevent multiple triggers

$('#myElement').on('swipeStatus', function(event, phase, direction, distance) {
  if (phase === "end" && !swipeTriggered) {
    if (direction === "left" && distance > 100) {
      swipeTriggered = true; // Prevent further triggering
      // Perform your action here...
    }
  }
});

The swipeTriggered flag prevents accidental multiple executions if the swipeStatus event triggers multiple times during one swipe.

Working with Different Finger Counts

touchSwipe can detect gestures using different numbers of fingers. While the default behavior is to react to single-finger swipes, you can potentially filter events based on the fingers property within the swipeStatus event. However, direct support for multi-finger gestures beyond basic swipe detection might require a different library or a more complex custom implementation. You can check the fingers property to differentiate single-finger swipes from multi-finger interactions (e.g., pinch-to-zoom).

$('#myElement').on('swipeStatus', function(event, phase, direction, distance, duration, fingers){
    if (fingers === 2 && phase === "end") {
        //Handle two-finger swipe (though exact behavior depends on the library implementation)
    } else if (fingers === 1 && phase === "end"){
        // Handle single-finger swipe
    }
});

Handling Edge Swipes

Edge swipes, where the swipe starts near the edge of the element, often require special handling to avoid interfering with the browser’s default scroll behavior or other UI elements. You can utilize the swipeStatus event to check the starting coordinates of the swipe relative to your element and modify your behavior accordingly. Consider preventing default actions for edge swipes (see the next section).

Preventing Default Actions

By default, touchSwipe might trigger browser default actions (like scrolling) when gestures are performed. You can use the preventDefaultEvents option to prevent this.

$('#myElement').touchSwipe({
    preventDefaultEvents: true
});

This prevents the browser from performing its default actions during a swipe, ensuring that your custom handlers take complete control. Alternatively, you could prevent default actions on a case-by-case basis within the swipeStatus event handler using event.preventDefault(), potentially only for edge swipes or specific conditions. However, using preventDefaultEvents: true globally is often simpler for suppressing unwanted browser behavior.

Integration and Examples

Integrating with Existing Projects

Integrating touchSwipe into existing projects is generally straightforward. Ensure jQuery is included in your project before including the touchSwipe library. Then, select the target elements using jQuery selectors and apply the touchSwipe() method. Remember to consider the potential impact on existing event handlers; you may need to adjust your code to work alongside touchSwipe’s event handling. If conflicts arise, carefully examine the event order and potentially use event.preventDefault() judiciously to prevent unwanted default behaviors.

Basic Example: Single Swipe Gesture

This example demonstrates a basic left-to-right swipe on a div element that changes its background color:

<!DOCTYPE html>
<html>
<head>
<title>touchSwipe Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="jquery.touchSwipe.min.js"></script>
<style>
#myElement {
  width: 200px;
  height: 100px;
  background-color: lightblue;
}
</style>
</head>
<body>

<div id="myElement"></div>

<script>
$('#myElement').on('swipeRight', function(){
  $(this).css('background-color', 'lightgreen');
});
</script>

</body>
</html>

Remember to replace "jquery.touchSwipe.min.js" with the actual path to your touchSwipe library file.

Example: Multi-directional Swipe Navigation

This example demonstrates multi-directional swipe navigation, changing content based on the swipe direction:

$('#navigationContainer').on('swipeLeft', function(){
  // Show next content
}).on('swipeRight', function(){
  // Show previous content
}).on('swipeUp', function(){
  // Show top content
}).on('swipeDown', function(){
  // Show bottom content
});

You would need to implement the logic for showing different content within the event handlers (e.g., using hidden elements, dynamically loading content, etc.).

Example: Pinch-to-Zoom Integration

Direct pinch-to-zoom isn’t a core feature of touchSwipe. To implement pinch-to-zoom, you’ll likely need to use a separate library specifically designed for that purpose (or create a custom solution involving touch event handling and transformations). touchSwipe primarily focuses on swipe gestures.

Example: Custom Swipe Actions

This example shows how to trigger custom actions based on swipes, going beyond simple visual changes:

$('#myElement').on('swipeLeft', function(){
    $.ajax({
        url: 'some_url',
        type: 'GET',
        success: function(data){
            // Handle successful data retrieval
        },
        error: function(error){
            // Handle error
        }
    });
}).on('swipeRight', function(){
    // Perform another custom action here
});

This demonstrates triggering an AJAX call on a left swipe, showcasing how touchSwipe can integrate with other functionalities. Remember to adapt the AJAX URL and error handling to your specific needs.

Remember to adapt these examples to your specific project requirements, HTML structure, and desired functionality. You’ll likely need to incorporate additional CSS and potentially more complex JavaScript logic to fully integrate touchSwipe into your application.

Troubleshooting

Common Issues and Solutions

Debugging Tips

Performance Optimization

Known Limitations

API Reference

This section provides a detailed overview of the touchSwipe API. Note that the specific properties and methods might vary slightly depending on the version of the library. Always refer to the latest documentation for the most accurate and up-to-date information.

touchSwipe() Method

The core function of the library is the touchSwipe() method. It’s applied to a jQuery object representing the element(s) to which you want to add swipe functionality. It takes an options object as an argument to configure the behavior.

Syntax:

$(selector).touchSwipe(options);

Event Properties

The events triggered by touchSwipe (e.g., swipe, swipeLeft, swipeRight, swipeUp, swipeDown, swipeStatus) pass an event object as an argument to the event handler function. This event object contains several useful properties:

Options Configuration

The touchSwipe() method accepts an options object to customize its behavior. Key options include:

Methods and Properties Overview

While touchSwipe() is the primary method, the library doesn’t expose many other public methods or properties directly on the jQuery object after applying touchSwipe(). Its functionality is primarily event-driven. The configuration is entirely handled through the options passed to touchSwipe(). The primary interaction is through event handlers attached to the target element(s) using jQuery’s on() or bind() methods, responding to the events fired by the library. The behavior is largely determined through the options you provide during initialization using touchSwipe().