2016-09-08 9 views
0

私のウェブサイトのヘッダーにiframe Vimeoのビデオを使用しています。一定の時間が経過した後、特定のdivにページがスクロールできるようにするJavaScriptを使用しています。divのスクロール時にJavaScriptを停止する

ビデオが終了すると、ページは自動的に次のdivにスクロールダウンします。

しかし、ビデオを見終わっていない状態で既にスクロールしていると、ページが自動的に再びスクロールアップするときに迷惑になります。

どうすればよいですか?これには別の解決策がありますか?または、ページがこのdivに移動したときにJavascriptの機能を停止することはできますか?

私は明確であることを望みます。

これは、あなたがここに述べたようにclearTimeout()を呼び出すことによって実行されるタイムアウト機能を停止することができ、スクロールリスナー登録する必要がありますスクリプト

$(window).load(function() { 
//normally you'd wait for document.ready, but you'd likely to want to wait 
//for images to load in case they reflow the page 
$('body').delay(36000) //wait 5 seconds 
    .animate({ 
     //animate jQuery's custom "scrollTop" style 
     //grab the value as the offset of #second from the top of the page 
     'scrollTop': $('.first-block').offset().top 
    }, 1000); //animate over 300ms, change this to however long you want it to animate for 
}); 

答えて

0

です:ボーナスとしてhttp://www.w3schools.com/jsref/met_win_settimeout.asp

var animation = setTimeout(function() { 
    $('body').animate({ 
     'scrollTop': $('.first-block').offset().top 
    }, 1000); 
}, 36000); 

$(window).scroll(function() { 
    clearTimeout(animation); // stop the timeout to be executed 
    $(window).off('scroll'); // we don't need this handler any more so unregister 
}); 

をあなたの質問のようにスクロールスクリプトが有効になっていない場合は、off()経由でスクロールリスナーを無効にすることができます。スクロール機能とclearTimeout()をもう一度呼び出す必要はありません。参照:https://api.jquery.com/off/

0

遅延をsetTimeoutで変数に割り当てます。

$(window).scroll(function() { 
    clearTimeout(delay); 
}); 
:あなたは以前に clearTimeout経由で割り当てられたタイムアウトをクリアすることができ、ユーザーがスクロールした場合

var delay = setTimeout(function(){ 
    $('body').animate({ 
     'scrollTop': $('.first-block').offset().top 
    }, 1000); 
}, 36000); 

関連する問題