Shiva Brahma
Shiva Brahma

Reputation: 49

javascript auto scroll with the ability of adding and minimizing the speed with out killing the animated event

Please help ! I have made a smooth scrolling application using jquery jquery.min.js everything work fine , and it scroll very well down and up , it make the speed faster and slower BUT !!!!! each time i want to make the scroll faster or slower the past animation is stopped and a new animation start again with new Speed (time to end). Can any one help me to finding a way to not kill the past events ("which make a stop scrolling ").

Here is My Main Function Below . Any help would be thankful.

//User can enter the wanted time to read (smooth) the whole text.
//if user did not enter any value => i will take the height of the whole document and multiply it by 25 (since normal person can read 25 word per minute)



    var w = $(window);
    var n = Math.max($(document).height(), $(window).height());
    var Speed = $("#<%=txtConsiderTime.ClientID%>").val();
    var offset = $("#fixedBar").offset();
  if (Speed == "" || Speed == null)
    {
        fixedSize = documentHeight;
        var documentHeight = Math.max($(document).height(), $(window).height());
        Speed = documentHeight * 25;
    }
    else
    {
        Speed = Speed * 60000;
    }
    var smallHeight;

    var TimesOfFaster = 0;
    var TimesOfSlower = 0;

    $(document).ready(function ()
    {

        var newSpeed = ($(window).scrollTop() * 25);


        var $element = $("html,body");
        var IsDown = false;
        var IsUp = false;

        $("body").keydown(function (e)
        {
            var n = Math.max($(document).height(), $(window).height());
            var $element = $("html,body");
            var offset = $("#fixedBar").offsetParent();
            var w = $(window);
            var y = offset.top - w.scrollTop();           


            //Going Up
            if (e.keyCode == 38)
            {
                newSpeed = ($(window).scrollTop() * 25);
                $('html, body').stop().animate({ scrollTop: 0 }, newSpeed);
                IsDown = false;
                IsUp = true;              
            }

                //Going Down 
            else if (e.keyCode == 40)
            {                
                $('html, body').stop().animate({ scrollTop: n }, Speed);
                IsDown = true;
                IsUp = false;                
            }

             //slower
            else if (e.keyCode == 37)
            {
                if (IsDown)
                {                    
                     Speed = Speed + (Speed/15);
                    $('html, body').stop().animate({ scrollTop: n }, Speed);                    
                }
                else if (IsUp)
                {
                    TimesOfSlower += 2;
                    if (TimesOfFaster >= 2)
                    {
                        TimesOfFaster -= 1;
                    }


                    //newSpeed = newSpeed + (newSpeed / 10);
                    if (15 - TimesOfSlower >= 0)
                    {
                        var result = newSpeed + (newSpeed / (15 - TimesOfSlower));
                        $('html, body').stop().animate({ scrollTop: 0 }, result);
                    }


                }
            }
                //faster
            else if (e.keyCode == 39)
            {               
                if (IsDown)
                {
                    Speed = Speed -(Speed/25);
                    $('html, body').stop().animate({ scrollTop: n }, Speed);                    
                }
                else if (IsUp)
                {
                    TimesOfFaster += 2;
                    if (TimesOfSlower > 2)
                    {
                        TimesOfSlower -= 1;
                    }

                    //newSpeed = newSpeed - (newSpeed / 25);
                    var result = newSpeed - (newSpeed / (15 + TimesOfFaster));

                    $('html, body').stop().animate({ scrollTop: 0 }, result);
                }
            }
            else if (e.keyCode == 32)
            {
                //$('html,body').closest('p').stop(true,false);
                $('html,body').stop(true, false);
            }

    });
    });

Upvotes: 1

Views: 291

Answers (1)

Kostas
Kostas

Reputation: 140

Well I get that you tried to implement the animation with jQuery. Keep in mind though that jQuery basic animations are for something simple. If you need that much of control try something else like:

Upvotes: 1

Related Questions