Darlyn
Darlyn

Reputation: 4940

disable scrolling while scrolling

I am writting a code , which animates scroll of 100% body.height. Everything works fine , but i am trying to disable the scrolling while the animation last to prevent further unwanted behavior. i am using this code

function animate(x) {
    var start = new Date();
    var id = setInterval(function (e) {

        var timepassed = new Date() - start;
        var progress = timepassed / x.duration;
        if (progress > 1) {
            progress = 1;
        }
        var delta = x.delta(progress);
        x.step(delta);
        if (progress == 1) {
            clearInterval(id);

        }

    }, x.delay);


}

function fak(e) {
    e.preventDefault();
    return false;
}

function move(e) {
    e.preventDefault();
    var wheel = e.wheelDelta;
    wheel = (wheel == 120) ? "-1" : "1";
    var body_height = document.body.offsetHeight;
    var scrollIt = body_height * wheel;
    var page = window.pageYOffset;
    animate({
        delay: 10,
        duration: 700,
        delta: function (p) {
            return p;
        },
        step: function (delta) {
            window.scrollTo(0, page + (delta * scrollIt));
        }
    });
    return false;
}
document.body.addEventListener("mousewheel", move, false);
document.body.addEventListener("DOMMouseScroll", move, false);

I tried to assign function fak , on mousewheel , mousescroll in interval and then restoring original assigment to them at the end using

function animate(x) {
    var start = new Date();
    var id = setInterval(function (e) {
    document.body.addEventListener("mousewheel", fak, false);
    document.body.addEventListener("DOMMouseScroll", fak, false);
        var timepassed = new Date() - start;
        var progress = timepassed / x.duration;
        if (progress > 1) {
            progress = 1;
        }
        var delta = x.delta(progress);
        x.step(delta);
        if (progress == 1) {
            clearInterval(id);
  document.body.addEventListener("mousewheel", move, false);
  document.body.addEventListener("DOMMouseScroll", move, false);

        }

    }, x.delay);


}

But didnt work either. Live demo : http://jsfiddle.net/Trolstover/o2pvo2t8/ Did i overlook something?

Upvotes: 1

Views: 99

Answers (1)

cari
cari

Reputation: 2300

i changed a bit on your code.

http://jsfiddle.net/o2pvo2t8/2/

just set a flag "running" while scrolling (at start of animate() ), and clear it at the end. and execute mov only when no scrolling appears.

hope it helps.

var running;

function animate(x) {
    running = 1;
    var start = new Date();
    var id = setInterval(function (e) {

        var timepassed = new Date() - start;
        var progress = timepassed / x.duration;
        if (progress > 1) {
            progress = 1;
        }
        var delta = x.delta(progress);
        x.step(delta);
        if (progress == 1) {
            clearInterval(id);
            running = 0;
        }

    }, x.delay);


}

function fak(e) {
    e.preventDefault();
    return false;
}

function move(e) {
    e.preventDefault();
    if (running==1) return;
    var wheel = e.wheelDelta;
    console.log(wheel);
    wheel = (wheel == 120) ? "-1" : "1";
    var body_height = document.body.offsetHeight;
    var scrollIt = body_height * wheel;
    var page = window.pageYOffset;
    animate({
        delay: 10,
        duration: 700,
        delta: function (p) {
            return p;
        },
        step: function (delta) {
            window.scrollTo(0, page + (delta * scrollIt));
        }
    });
    return false;
}
document.body.addEventListener("mousewheel", move, false);
document.body.addEventListener("DOMMouseScroll", move, false);

Upvotes: 2

Related Questions