0

Assume 6 absolute positioned HTML elements stacked one on top of the other, A-F, with "A" on top.

<body data-swipe-threshold="100">
<div><img alt = "F"/></div>
<div><img alt = "E"/></div>
<div><img alt = "D"/></div>
<div><img alt = "C"/></div>
<div><img alt = "B"/></div>
<div><img alt = "A"/></div>
</body>

For touch-enabled screens I want the user to be able to swipe to the right over the "A" div/image to place it on the bottom of the stack (exposing "B"). The next right-swipe exposing "C", etc. Conversely, a swipe left should bring the "card" on the bottom of the deck to the top, where it becomes visible. I would allow "F" to expose "A" with a right-swipe when "F" is on top, and also allow "A" to expose "F" with a left swipe. I realize I'll be manipulating zIndex, but I seem unable to capture/process a swipe event with elements stacked in this way. "swiped-events.js" is the script I am using. My code is here: https://jsfiddle.net/okrcmLw5/ but even when I test outside of "fiddle" the demo seems to ignore my swipes. Thanks!

Tony Shaw
  • 3
  • 2
  • I too cannot get it to see a swipe in your fiddle - but I can't see where/how you load swiped-events.js, can you point that out, thanks. I have managed to get it to create a swiped event using a slightly altered swiped-events.js but on small objects it is not very easy to create one. When you let me know how you load it I'll try that and put up what I've seen in an answer. – A Haworth Jan 09 '21 at 07:37
  • The source is here: https://github.com/john-doherty/swiped-events – Tony Shaw Jan 09 '21 at 10:12
  • Here it is in fiddle: https://jsfiddle.net/54qfnxz1/ – Tony Shaw Jan 09 '21 at 10:19

1 Answers1

0

swiped-events.js creates an event when the user swipes. It does this on the element at which the touchstart event was seen, and it looks for touchstart events on the whole document.

Putting an event listener on the deck element receives the 'swipe' event but the target of the event seems to be the contained img element. So we cannot use event.target to find the whole swiped div, but it's the whole swiped div we want to move.

If we do not try to do things with z-index but instead use the JS prepend and appendChild functions we can move the top element to the bottom or vice versa without worrying about which of the div's child elements (there is only one at the moment, but there is no reason there could not be several, a caption for instance) has been swiped.

Here's the JS to do this:

  document.getElementById('deck').addEventListener('swiped-left', function () { deck.appendChild(deck.firstElementChild); });
  document.getElementById('deck').addEventListener('swiped-right', function () { deck.prepend(deck.lastElementChild); });

and here is a snippet. It includes all the js code from github.com/john-doherty/swiped-event in case that gets moved/deleted. Snippet tested on touch device - iPad with IOS14.2 - and on Edge dev tools 'emulator'.

/*!
 * swiped-events.js - v@version@
 * Pure JavaScript swipe events
 * https://github.com/john-doherty/swiped-events
 * @inspiration https://stackoverflow.com/questions/16348031/disable-scrolling-when-touch-moving-certain-element
 * @author John Doherty <www.johndoherty.info>
 * @license MIT
 */

function init() {

    'use strict';
    
    // patch CustomEvent to allow constructor creation (IE/Chrome)
    if (typeof window.CustomEvent !== 'function') {

        window.CustomEvent = function (event, params) {

            params = params || { bubbles: false, cancelable: false, detail: undefined };

            var evt = document.createEvent('CustomEvent');
            evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
            return evt;
        };

        window.CustomEvent.prototype = window.Event.prototype;
    }

    document.addEventListener('touchstart', handleTouchStart, false);
    document.addEventListener('touchmove', handleTouchMove, false);
    document.addEventListener('touchend', handleTouchEnd, false);

    var xDown = null;
    var yDown = null;
    var xDiff = null;
    var yDiff = null;
    var timeDown = null;
    var startEl = null;

    /**
     * Fires swiped event if swipe detected on touchend
     * @param {object} e - browser event object
     * @returns {void}
     */
    function handleTouchEnd(e) {

        // if the user released on a different target, cancel!
        // !!THIS NEVER HAPPENS AT LEAST ON SAFARI IPAD IOS 14.2, IF RELEASE OUTSIDE THE START ELEMENT IT GIVES THE TARGET AS THE START ELEMENT
        if (startEl !== e.target) { return;}

        var swipeThreshold = parseInt(getNearestAttribute(startEl, 'data-swipe-threshold', '20'), 10); // default 20px
        var swipeTimeout = parseInt(getNearestAttribute(startEl, 'data-swipe-timeout', '500'), 10);    // default 500ms
        var timeDiff = Date.now() - timeDown;
        var eventType = '';
        var changedTouches = e.changedTouches || e.touches || [];

        if (Math.abs(xDiff) > Math.abs(yDiff)) { // most significant
            if (Math.abs(xDiff) > swipeThreshold && timeDiff < swipeTimeout) {
                if (xDiff > 0) {
                    eventType = 'swiped-left';
                }
                else {
                    eventType = 'swiped-right';
                }
            }
        }
        else if (Math.abs(yDiff) > swipeThreshold && timeDiff < swipeTimeout) {
            if (yDiff > 0) {
                eventType = 'swiped-up';
            }
            else {
                eventType = 'swiped-down';
            }
        }

        if (eventType !== '') {

            var eventData = {
                dir: eventType.replace(/swiped-/, ''),
                xStart: parseInt(xDown, 10),
                xEnd: parseInt((changedTouches[0] || {}).clientX || -1, 10),
                yStart: parseInt(yDown, 10),
                yEnd: parseInt((changedTouches[0] || {}).clientY || -1, 10)
            };

            // fire `swiped` event event on the element that started the swipe
            startEl.dispatchEvent(new CustomEvent('swiped', { bubbles: true, cancelable: true, detail: eventData }));
            // fire `swiped-dir` event on the element that started the swipe
            startEl.dispatchEvent(new CustomEvent(eventType, { bubbles: true, cancelable: true, detail: eventData }));
        }

        // reset values
        xDown = null;
        yDown = null;
        timeDown = null;
    }

    /**
     * Records current location on touchstart event
     * @param {object} e - browser event object
     * @returns {void}
     */
    function handleTouchStart(e) {    
        
        // if the element has data-swipe-ignore="true" we stop listening for swipe events
        if (e.target.getAttribute('data-swipe-ignore') === 'true') return;

        startEl = e.target;

        timeDown = Date.now();
        xDown = e.touches[0].clientX;
        yDown = e.touches[0].clientY;
        xDiff = 0;
        yDiff = 0;
    }

    /**
     * Records location diff in px on touchmove event
     * @param {object} e - browser event object
     * @returns {void}
     */
    function handleTouchMove(e) {
        
        if (!xDown || !yDown) return;

        var xUp = e.touches[0].clientX;
        var yUp = e.touches[0].clientY;

        xDiff = xDown - xUp;
        yDiff = yDown - yUp;
    }

    /**
     * Gets attribute off HTML element or nearest parent
     * @param {object} el - HTML element to retrieve attribute from
     * @param {string} attributeName - name of the attribute
     * @param {any} defaultValue - default value to return if no match found
     * @returns {any} attribute value or defaultValue
     */
    function getNearestAttribute(el, attributeName, defaultValue) {

        // walk up the dom tree looking for data-action and data-trigger
        while (el && el !== document.documentElement) {

            var attributeValue = el.getAttribute(attributeName);

            if (attributeValue) {
                return attributeValue;
            }

            el = el.parentNode;
        }

        return defaultValue;
    }

}
document.getElementById('deck').addEventListener('swiped-left', function () { deck.appendChild(deck.firstElementChild); });
  document.getElementById('deck').addEventListener('swiped-right', function () { deck.prepend(deck.lastElementChild); });
  init();
  body { overflow: hidden; padding: 100px; }
    #deck {
                position: relative;
    }
    #deck > div {
                position: absolute;
                top: 0; 
                left: 0;
    }
<div id="deck" data-swipe-threshold="100">
  <div><img src="http://dummyimage.com/250x250/000/fff&text=F" /></div>
  <div><img src="http://dummyimage.com/250x250/000/fff&text=E" /></div>
  <div><img src="http://dummyimage.com/250x250/000/fff&text=D" /></div>
  <div><img src="http://dummyimage.com/250x250/000/fff&text=C" /></div>
  <div><img src="http://dummyimage.com/250x250/000/fff&text=B" /></div>
  <div><img src="http://dummyimage.com/250x250/000/fff&text=A" /></div>
</div>
A Haworth
  • 30,908
  • 4
  • 11
  • 14
  • Thank you! That solution works great for a page with only one such deck. In production, my page will have a dozen or more of these decks, so now I just need to figure out how to position (and enable events on) multiple decks on a single page... – Tony Shaw Jan 11 '21 at 10:27
  • Should be doable by converting to class deck rather than I’d. – A Haworth Jan 11 '21 at 10:34
  • Its interesting. I did convert from #deck to .deck and I have multiple/swipable decks, however when I swipe I go from A to C to E and vice versa on each .deck. It's odd that now B and D and F are ignored/skipped. – Tony Shaw Jan 11 '21 at 11:27
  • var x = document.getElementsByClassName("deck"); var i; for (i = 0; i < x.length; i++) { x[i].addEventListener('swiped-left', function () { this.appendChild(this.firstElementChild); }); x[i].addEventListener('swiped-right', function () { this.prepend(this.lastElementChild); }); } – Tony Shaw Jan 11 '21 at 11:28
  • A swipe moves 2 elements on/off the stack (e.g. A **AND** B), not just one... Strange... – Tony Shaw Jan 11 '21 at 11:35
  • Is that with the snippet here? I’ve tested it on iPad IOS and also Chrome mobile emulator and it does only one. There will be an event fired on the img as well as the deck so you need to not catch the img one. What system/browser are you using? – A Haworth Jan 11 '21 at 12:09
  • Problem solved! My "live" test page, where I incorporated the new js snippet, still had a – Tony Shaw Jan 11 '21 at 18:52
  • Excellent to know! – A Haworth Jan 11 '21 at 22:28