Search code examples
javascriptprototypejsscriptaculous

Stopping Effect.ScrollTo() while it's scrolling?


Yeah, basically I have this:

Effect.ScrollTo("bottom", { duration: 5.0 });

So what I want is to stop this effect while it's scrolling whenever I want to.

Any help?


Solution

  • var scrollEffect = Effect.ScrollTo("bottom", { duration: 5.0 });
    
    ...
    
    scrollEffect.cancel();
    

    This code below works perfectly for me:

    <html>
    <head>
        <script src="js/prototype.js" type="text/javascript"></script>
        <script src="js/scriptaculous.js" type="text/javascript"></script>
    
        <style type="text/css" media="screen">
            body { font-size: 30px; }
            #destination { margin-top: 1400px; }
            #start { position: fixed; top: 10px; left: 20px;}
            #stop { position: fixed; top: 50px; left: 20px;}
        </style>
    
        <script type="text/javascript" charset="utf-8">
            function startEffect() {
                scrollEffect = Effect.ScrollTo("destination", { duration: 8.0 }); return false;
            }
    
            function stopEffect() {
                scrollEffect.cancel(); return false;
            }
        </script>
    </head>
    <body>
        <a id="start" href="#" onclick="return startEffect();">Start Effect</a>
        <a id="stop" href="#" onclick="return stopEffect();">Stop Effect</a>
        <p id="destination">Scroll Destination</p>
    </body>
    </html>