Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to NOT-click a href while dragging in iScroll

I have the iScroll enabled on my page.

Notice the images in the scroller are links (so a popup opens for the bigger image, y'know the deal). BUT one of the lovely features of iScroll is that you can drag your mouse to scroll. BUT now, when someone drags it, it automatically opens the image instead of scrolling the bar. Is there a workaround?

like image 241
Don Munter Avatar asked Jan 22 '12 22:01

Don Munter


2 Answers

I would say append a class to each anchor while the scroller is being dragged. For example append a class name of "dragging" to each anchor while being dragged then remove the class when dragging stops.

That means that you can add a preventDefault to any link which has the "dragging" class. Something along the lines of:

    myScroll1 = new iScroll('scroll1', {
        snap: 'li',
        momentum: false,
        hScrollbar: false,
        onScrollStart: function () {
            $('div#iscroll1 a').addClass("dragging");
        },
        onScrollEnd: function () {
            $('div#iscroll1 a').removeClass("dragging");
            document.querySelector('.indicator > li.active').className = '';
            document.querySelector('.indicator > li:nth-child(' + (this.currPageX+1) + ')').className = 'active';
        }
    });
    $('.dragging').click(function (e) {
        e.preventDefault();
    }

This is however untested code so you may need to refine the selectors.

like image 170
Zappa Avatar answered Nov 06 '22 09:11

Zappa


I've created a patch to iScroll that fixes this issue. You can see the diff here: https://github.com/zmathew/iscroll/commit/3d77681a9f4b3a6b5a7579df4443bc53356d5584

Alternatively you can grab the entire patched version of iScroll from here: https://github.com/zmathew/iscroll/tree/prevent-scroll-clicks

like image 24
Zach Mathew Avatar answered Nov 06 '22 08:11

Zach Mathew