Help taken from:
How to disable scrolling temporarily?
Although your question is not duplicate of the above question. But this will help you to have the right approach. What you are wishing is that your window should not scroll. And on your mousewheel
movement you want the animation to run.
So instead of running your animation on scroll
event. You should run it with mousewheel
event.
window scroll will essentially run when your window scrolls, which you don't wish to do until the animation is completed. So disable the window scroll
and detect the mousewheel
movement and run your animation. And once your animation is finished enable the scroll once again.
This is definitely not the finally solution for you but should guide you in the right direction.
$(document).ready(function () {
var keys = {37: 1, 38: 1, 39: 1, 40: 1};
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
function preventDefaultForScrollKeys(e) {
if (keys[e.keyCode]) {
preventDefault(e);
return false;
}
}
function disableScroll() {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}
function enableScroll() {
if (window.removeEventListener)
window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
}
disableScroll();
var i = 0;
$('.my-container').bind('mousewheel', function(e){
var animation = $(".my-container .animation"),
windowScroll = $(window).scrollTop();
if (parseInt(animation.css('left')) + animation.width() < $(window).width()) {
e.preventDefault();
animation.css('left', i * 1.5);
}
else {
// enable scroll
}
if(i >= 100){
enableScroll();
}else{
i++;
}
});
})
.my-container{
width:100%;
height:620px;
position:relative;
overflow:hidden;
background-color:#333333;
}
.my-container .animation{
position:absolute;
width:420px;
height:200px;
bottom:0;
left:0;
background-color:#02f15f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section class="my-container">
<div class="animation"></div>
</section>