flesler / jquery.localScroll

Animated anchor navigation made easy with jQuery

Home Page:http://demos.flesler.com/jquery/localScroll/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Respect isDefaultPrevented()

TheRND opened this issue · comments

IMHO plugin should not scroll page if isDefaultPrevented() returns true. I've patched your code with following lines, probably it's worth to include it into mainstream.

    return settings.lazy ?
        // use event delegation, more links can be added later.     
        this.bind( settings.event, function( e ){
            // Could use closest(), but that would leave out jQuery -1.3.x
            var a = $([e.target, e.target.parentNode]).filter(filter)[0];
            // if a valid link was clicked
                    // PATCHED NEXT LINE ================================================
        if( a && !e.isDefaultPrevented())                                                                    
                scroll( e, a, settings ); // do scroll.
        }) :
        // bind concretely, to each matching link
        this.find('a,area')
            .filter( filter ).bind( settings.event, function(e){
               // PATCHED NEXT LINE ==================================================
               if(!e.isDefaultPrevented()) {                                                                                
                    scroll( e, this, settings );
                }
            }).end()
        .end();

The change seems legit, though I'm worried someone would preventDefault for some reason or blindly return false and unexpectedly break the plugin.
The truth is you have 2 ways to prevent scroll already, one is binding the click before the plugin does and calling e.stopImmediatePropagation() or simply returning false within the onBefore handler, I'm not sure this is truly needed.

Some input from other people would be great. What do you think?

Sorry, my English is poor, but I'll try my best to explain. Using e.stopImmediatePropagation is not a valid solution for me, because of web analytics solution handlers, counting clicks. Even if I don't have that web analytics, there are thousands of other JavaScript code lines on the site already, and tons of anchors (tabs, hints, popups) with handlers attached and preventDefault() to eliminate scroll. So expected plugin behaviour for me is to change nothing on the existing website, but introduce a smooth scroll only.

I mean if someone called preventDefault() in the link click handler, he is definitely trying to prevent the default link action - scroll. So it seems rational to keep the same behaviour after plugin installation.

I'm just worried about how many people might be calling it for w/e the reason or adding return false at the end and they don't expect the scroll to be skipped. Some plugins like history ones handle links as well and could call preventDefault in parallel.
I think, for the sake of backwards-compatibility, that this should remain the way it is, considering you already have 2 ways to achieve this and one is officially provided by the plugin.

Closing for aforementioned reasons