### Track Complete Swipe Lifecycle with TouchJs Source: https://context7.com/rehiy/touch-js/llms.txt This advanced example shows how to track the entire lifecycle of a swipe gesture, including start, move, and end phases. It demonstrates updating an element's position during a swipe and resetting it upon completion, while preventing default browser behavior. ```javascript // Track complete swipe gesture lifecycle var dragElement = document.getElementById('draggable'); var initialTop = 0, initialLeft = 0; touch(dragElement) .on('swipestart', function(e) { console.log('Swipe started at:', e.pageX, e.pageY); // Store initial position initialTop = parseInt(this.style.top || 0); initialLeft = parseInt(this.style.left || 0); this.style.position = 'relative'; this.style.background = '#ddd'; }) .on('swipe', function(e) { console.log('Swiping - Move delta:', e.moveX, e.moveY); // Update element position during drag this.style.top = (initialTop + e.moveY) + 'px'; this.style.left = (initialLeft + e.moveX) + 'px'; // Prevent browser default behavior (scrolling) return false; }) .on('swipeend', function(e) { console.log('Swipe ended'); console.log('Total movement:', e.moveX, e.moveY); // Snap back to original position this.style.position = 'static'; this.style.top = '0'; this.style.left = '0'; this.style.background = '#fff'; }); ``` -------------------------------- ### TouchJs Basic Usage and Event Handling (JavaScript) Source: https://github.com/rehiy/touch-js/blob/master/README.md Demonstrates the basic initialization of TouchJs on an element and how to attach event listeners for 'tap' and 'longtap' gestures. It also shows how to use event delegation to handle taps on elements with a specific class within the target element. The 'swipe' event example illustrates how to prevent the browser's default behavior for smoother interactions. Dependencies include the TouchJs library itself. Inputs are DOM elements or selectors, and outputs are console logs. Limitations include the lack of support for two-finger gestures. ```javascript touch(document.getElementById('toucher')) //轻击事件 .on('tap',function(e) { console.log(this, e); }) //长按事件 .on('longtap',function(e) { console.log(this, e); }) //委托轻击事件 .on('tap', '.class', function(e) { console.log(this, e); return false }); ``` -------------------------------- ### JavaScript Accessing Event Properties with Touch.js Source: https://context7.com/rehiy/touch-js/llms.txt Illustrates how to access detailed information about touch and gesture events through the event object `e`. Properties include event type, current and start coordinates, movement deltas, and the target element. Useful for calculating distances, angles, and understanding gesture specifics. ```javascript touch(document.getElementById('touchArea')) .on('swipe', function(e) { // Event type console.log('Type:', e.type); // 'swipe' // Current position console.log('Current X:', e.pageX); console.log('Current Y:', e.pageY); console.log('Client X:', e.clientX); console.log('Client Y:', e.clientY); // Start position (only available in swipe events) console.log('Start X:', e.startX); console.log('Start Y:', e.startY); // Movement delta (only available in swipe events) console.log('Move X:', e.moveX); console.log('Move Y:', e.moveY); // Target element console.log('Target:', e.target); // Calculate distance and angle var distance = Math.sqrt(e.moveX * e.moveX + e.moveY * e.moveY); var angle = Math.atan2(e.moveY, e.moveX) * 180 / Math.PI; console.log('Distance:', distance, 'Angle:', angle); }); ``` -------------------------------- ### Implement Vertical Range Control with TouchJs Source: https://github.com/rehiy/touch-js/blob/master/ex2.html This example demonstrates the creation of a vertical slider or range control using TouchJs. Similar to the horizontal version, it responds to 'swipestart', 'swipe', and 'swipeend' events. The 'swipe' event adjusts the element's top position, and 'swipeend' calculates and displays the corresponding percentage value. ```javascript var start_top, elem = document.getElementById('progressB'); function rangeControl(num,max){ num = Math.max(num,0); return Math.min(num,max); } touch(elem) .on('swipestart','.progress-bar',function(e){ start_top = parseInt(this.style.top) || 0; this.innerHTML = ''; }).on('swipe','.progress-bar',function(e){ this.style.top = rangeControl(start_top + e.moveY,elem.clientHeight - this.clientHeight) + 'px'; return false; }).on('swipeend','.progress-bar',function(e){ this.innerHTML = Math.ceil(rangeControl(start_top + (e.moveY||0),elem.clientHeight - this.clientHeight)/(elem.clientHeight - this.clientHeight) * 100) + '%'; return false; }); ``` -------------------------------- ### Detect Swipe Gestures in Four Directions with TouchJs Source: https://context7.com/rehiy/touch-js/llms.txt This code illustrates how to detect swipe gestures in the four cardinal directions: up, down, left, and right. It provides examples of corresponding actions for each swipe direction, such as navigation or content refresh. ```javascript // Handle all four swipe directions var container = document.getElementById('swipeArea'); touch(container) .on('swipeup', function(e) { console.log('Swiped up'); console.log('Start position:', e.startX, e.startY); console.log('End position:', e.pageX, e.pageY); console.log('Movement delta:', e.moveX, e.moveY); // Navigate to previous page navigatePrevious(); }) .on('swipedown', function(e) { console.log('Swiped down'); // Pull to refresh refreshContent(); }) .on('swipeleft', function(e) { console.log('Swiped left'); // Show next slide nextSlide(); }) .on('swiperight', function(e) { console.log('Swiped right'); // Show previous slide previousSlide(); }); ``` -------------------------------- ### Implement Long Press Delete with TouchJs Source: https://github.com/rehiy/touch-js/blob/master/ex2.html This example shows how to implement a 'longtap' gesture using TouchJs for a delete action. It attaches a long press event listener to list items within a specific group, triggering an alert dialog upon detection. This pattern is useful for hiding delete functionality until explicitly invoked by the user. ```javascript touch(document.getElementById('listGroupB')) .on('longtap','.list-item',function(){ alert('这只是个demo!'); }); ``` -------------------------------- ### Initialize TouchJs Instance for DOM Element Source: https://context7.com/rehiy/touch-js/llms.txt This snippet demonstrates how to create a new TouchJs instance for a given DOM element. This is the initial step to enable gesture event handling on that element. It can target native DOM elements or jQuery-selected elements. ```javascript var element = document.getElementById('toucher'); var touchInstance = touch(element); // Can also work with jQuery-selected elements var jqueryElement = $('#toucher')[0]; var touchInstance = touch(jqueryElement); ``` -------------------------------- ### JavaScript Image Gallery with Touch Gestures Source: https://context7.com/rehiy/touch-js/llms.txt Implements a swipeable image gallery using TouchJS. It handles swipe left/right for navigation, double-tap for zooming, and long-press to show image data. Dependencies include the TouchJS library and basic HTML structure for the gallery and images. Returns `false` from handlers to prevent default browser actions. ```javascript // Complete swipeable image gallery implementation var gallery = document.getElementById('imageGallery'); var currentIndex = 0; var images = gallery.querySelectorAll('.image'); var totalImages = images.length; touch(gallery) // Swipe left to next image .on('swipeleft', function(e) { if (currentIndex < totalImages - 1) { currentIndex++; updateGallery(); } return false; // Prevent scrolling }) // Swipe right to previous image .on('swiperight', function(e) { if (currentIndex > 0) { currentIndex--; updateGallery(); } return false; }) // Double tap to zoom .on('doubletap', function(e) { var currentImage = images[currentIndex]; if (currentImage.classList.contains('zoomed')) { currentImage.classList.remove('zoomed'); currentImage.style.transform = 'scale(1)'; } else { currentImage.classList.add('zoomed'); currentImage.style.transform = 'scale(2)'; } }) // Long press to show info .on('longtap', function(e) { var imageInfo = images[currentIndex].getAttribute('data-info'); showImageInfo(imageInfo); }); function updateGallery() { var offset = -currentIndex * 100; gallery.style.transform = `translateX(${offset}%)`; } // Additional helper function function showImageInfo(info) { var infoPanel = document.getElementById('infoPanel'); infoPanel.textContent = info; infoPanel.style.display = 'block'; setTimeout(() => { infoPanel.style.display = 'none'; }, 3000); } ``` -------------------------------- ### JavaScript Avalon.js Integration with Touch.js Source: https://context7.com/rehiy/touch-js/llms.txt Shows how to integrate Touch.js with the Avalon.js framework (version 1.5.x) using a provided plugin (`touch_for_avalon.js`). The plugin automatically registers event hooks for Avalon, allowing direct use of touch events in Avalon view models and HTML templates via directives like `ms-tap` and `ms-swipe`. ```javascript // Avalon.js framework integration (using touch_for_avalon.js) // The plugin automatically registers event hooks for Avalon // In Avalon view model var vm = avalon.define({ $id: 'app', handleTap: function(e, args) { console.log('Tap event from Avalon:', e); console.log('Additional args:', args); }, handleSwipe: function(e, args) { console.log('Swipe event:', e.moveX, e.moveY); } }); // In HTML template //