Skip to content

Enabling Mobile Support #101

Closed
Closed

Description

@action-simon

Hello.

I am using MovingBoxes which I really love.

But I run into a issue and want to ask if it's possible to enable Mobile/Touchpad Support for Mobile Devices like the iPad, iPhone or Android Phones.

I mean that you can slide the images with your finger on this devices.

Activity

  1. Mottie commented on Nov 7, 2012

    @Mottie
    Contributor

    Hi action-simon!

    I don't have an iPad, but I think just touching the outer panels or the arrows would change the slide. Or do you mean you'd like swipe support?

  2. action-simon commented on Nov 7, 2012

    @action-simon
    Author

    Hello!

    Yes I am taking about the swipe support. Touching the arrows or the outer areas does work of course ;)

  3. Mottie commented on Nov 7, 2012

    @Mottie
    Contributor

    Ok, I'll look into making a demo for you soon... I have other distractions today =(

  4. action-simon commented on Nov 7, 2012

    @action-simon
    Author

    Oh many thanks in advance! Very nice from you.

  5. Mottie commented on Nov 9, 2012

    @Mottie
    Contributor

    Ok, here is a demo of swipe support... it's a bit of code, but all you need to do is include it inside of a document ready event with the correct target - check out the demo:

    jQuery(function($){
    
        $('#slider').movingBoxes({
            startPanel: 4,
            wrap: true,
            buildNav: true,
            navFormatter: function(index, panel) {
                return "●";
            }
        });
    
        /* Add Swipe to Moving Boxes */
        var target = $('#slider'),
        // allow movement if < 1000 ms (1 sec)
        maxTime = 1000,
        // swipe movement of 50 pixels triggers the swipe
        maxDistance = 50,
    
        api = target.data('movingBoxes'),
        startX = 0,
        startTime = 0,
        touch = "ontouchend" in document,
        startEvent = (touch) ? 'touchstart' : 'mousedown',
        moveEvent = (touch) ? 'touchmove' : 'mousemove',
        endEvent = (touch) ? 'touchend' : 'mouseup';
    
        api.$wrap.on(startEvent, function(e) {
            // prevent image drag (Firefox)
            e.preventDefault();
            startTime = e.timeStamp;
            startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
        })
        .on(endEvent, function(e) {
            startTime = 0;
            startX = 0;
        })
        .on(moveEvent, function(e) {
            e.preventDefault();
            var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX,
            currentDistance = (startX === 0) ? 0 : Math.abs(currentX - startX),
            // allow if movement < 1 sec
            currentTime = e.timeStamp;
            if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) {
                if (currentX < startX) {
                    // swipe left code here
                    api.goForward();
                }
                if (currentX > startX) {
                    // swipe right code here
                    api.goBack();
                }
                startTime = 0;
                startX = 0;
            }
        });​
    
    });

    If you are using an older version of jQuery (before v1.7), then replace the on functions with bind.

  6. action-simon commented on Nov 9, 2012

    @action-simon
    Author

    Oh thank you so much!! Very nice support from you, I have to say.

  7. action-simon commented on Nov 12, 2012

    @action-simon
    Author

    Hello. It is me again.

    Finally I had time to implement it on the site, but now I have the problem that only Swipe is working on the Movingbox with the ipad.

    If i click on the non active slides or the nav arrows or thumbnails nothing happens. On PC it is still working.

    Edit: It is the same issue on the demo you have posted as well just to be sure it is not on my end ;)

    It seems that all href links are not working. (Within slider div)

  8. Mottie commented on Nov 12, 2012

    @Mottie
    Contributor

    Try changing the api.$wrap to api.$window and see if that fixes it :)

  9. action-simon commented on Nov 12, 2012

    @action-simon
    Author

    Oh wow. Many thanks. It is working like a charm now.

    Edit: Links are working on the Thumbnails and arrows now. But still not on the Slides itself.

    <div>
                <h2>Text 1</h2>
                <a href="http://www.google.com" target="blank" class="joabild"><img src="..." alt="picture"></a>
                <h2>Even more text <span class="bla">Bla</span></h2>
                <p>
                     And more text<a href="#">mehr</a>
                </p>
            </div>
    
  10. Mottie commented on Nov 12, 2012

    @Mottie
    Contributor

    LOL ok, lets go one more level deep... change the code to api.$panels

  11. action-simon commented on Nov 13, 2012

    @action-simon
    Author

    Nope. I'm afraid thats not working.

    Maybe it is useful posting the complete js?

    function fnIsAppleMobile() {
        if (navigator && navigator.userAgent && navigator.userAgent != null) {
            var strUserAgent = navigator.userAgent.toLowerCase();
            var arrMatches = strUserAgent.match(/(iphone|ipod|ipad|android)/);
            if (arrMatches)
                return true;
        }
        // End if (navigator && navigator.userAgent) 
        return false;
    }
    jQuery(function($){
    
        $('#slider').movingBoxes({
            startPanel: 4,
            reducedSize: 0.9,
            initAnimation: true,
            hashTags: false,
            wrap: true,
            buildNav: true,
            speed: 500,
            stopAnimation: true,
            fixedHeight: true,
            easing: 'easeOutBack',
            navFormatter: function(index, panel) {
                return "&#9679;";
            }
            preinit: function(e, mb, tar) {
    
    
                if (fnIsAppleMobile() == false) {
                var throttled = false,
                throttledDelay = 200;
                mb.$window.mousewheel(function(event, delta) {
                    if (!throttled) {
                        mb.change(mb.curPanel + (delta < 0 ? 1: -1));
                        throttled = true;
                        setTimeout(function() {
                            throttled = false;
                        }, throttledDelay);
                    }
                    return false;
                });
    }
    
    
            }
        });
            if (fnIsAppleMobile() == true) { 
        /* Add Swipe to Moving Boxes only when the user Agent is an Smartphone */
            var target = $('#slider'),
            // allow movement if < 1000 ms (1 sec)
            maxTime = 1000,
            // swipe movement of 50 pixels triggers the swipe
            maxDistance = 50,
    
            api = target.data('movingBoxes'),
            startX = 0,
            startTime = 0,
            touch = "ontouchend" in document,
            startEvent = (touch) ? 'touchstart': 'mousedown',
            moveEvent = (touch) ? 'touchmove': 'mousemove',
            endEvent = (touch) ? 'touchend': 'mouseup';
    
            api.$panels.on(startEvent, function(e) {
                // prevent image drag (Firefox)
                e.preventDefault();
                startTime = e.timeStamp;
                startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX: e.pageX;
            }).on(endEvent, function(e) {
                startTime = 0;
                startX = 0;
            }).on(moveEvent, function(e) {
                e.preventDefault();
                var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX: e.pageX,
                currentDistance = (startX === 0) ? 0: Math.abs(currentX - startX),
                // allow if movement < 1 sec
                currentTime = e.timeStamp;
                if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) {
                    if (currentX < startX) {
                        // swipe left code here
                        api.goForward();
                    }
                    if (currentX > startX) {
                        // swipe right code here
                        api.goBack();
                    }
                    startTime = 0;
                    startX = 0;
                }
            });
            } else {
            // Disable Image Dragging
            $("#slider img").mousedown(function() {
                return false;
            });
        }; 
    
    });
  12. Mottie commented on Nov 13, 2012

    @Mottie
    Contributor

    I think that code is just missing a comma before the preinit function (updated demo)

    },
    preinit: function(e, mb, tar) {
  13. action-simon commented on Nov 15, 2012

    @action-simon
    Author

    Sorry for the delay but I was busy. ;) I had already this comma in my JS. Maybe I made a mistake copy/pasting the code in here.

    So sadly it is still not working.

    Oh and the links are only clickable on the arrows and thumbnails. So the links next to the active slide are not working as well.

  14. Mottie commented on Nov 15, 2012

    @Mottie
    Contributor

    Well, all I can think of is to remove the e.preventDefault(); from the start event.

  15. action-simon commented on Nov 15, 2012

    @action-simon
    Author

    Oh many thanks! Finally it is working like I want. Thank you very much. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions