ちょっとしたJavaScriptを作ります。このような:
content.css('height', contentHeight, function() {
alert("test");
$("body").scrollTo("#main", 600);
});
コンテンツの高さを設定した後、アラートを実行してほしい。しかし、私は何が間違っているのですか?
ちょっとしたJavaScriptを作ります。このような:
content.css('height', contentHeight, function() {
alert("test");
$("body").scrollTo("#main", 600);
});
コンテンツの高さを設定した後、アラートを実行してほしい。しかし、私は何が間違っているのですか?
.css()
機能は瞬時です。
content.css('height', contentHeight);
alert("test");
$("body").scrollTo("#main", 600);
のようなメソッドには「done」ハンドラーはありませんcss()
。すぐに実行されます。
したがって、次のようにコードを記述する必要があります。
content.css("height", contentHeight);
alert("test");
$("body").scrollTo("#main", 600);
ただし、高さを設定した後に遅延を追加する必要がある場合は、次を使用できますsetTimeout()
。
content.css("height", contentHeight);
setTimeout(function() {
alert("test");
$("body").scrollTo("#main", 600);
}, 1000); // 1000 is number of milliseconds
jquery cssメソッドの関数は、次のことを意味します。
content.css('height', function(){
return contentHeight;
});
コンテンツは配列です。上記のコードは、この配列内のすべての要素の高さを設定します。この関数の使用法は次のようになります。
content.css('height', function(index){
return index * contentHeight;
});
この場合、次の各要素の高さはcontentHeight値だけ増加します。
animateを使用すると、終了時にコールバックが呼び出されます。
content.animate({
height: contentHeight
}, 0, function() {
alert("test");
$("body").scrollTo("#main", 600);
});
使ってみてください
content.animate(
{height : 100},
5000,
function() {
//Animation complete.
alert("test");
$("body").scrollTo("#main", 600);
}
);