この場合、jQueryは必要ありません。これは基本的なことであり、関数を使用してそれがどのように行われるかを説明しています。
function showDiv(divID)
{
if(document.getElementById(divID).style.display=='none')
{
document.getElementById(divID).style.display='block';
}
}
関数が実行しているのは、基本的にBOXモデルから要素全体を削除することです(ブロックを切り替えると、要素がBOXモデルから完全に削除されないため、スペースなどを占有しません。ただし、レイアウトの問題が発生する場合と発生しない場合があります)。
スローモーションでアニメーション化するには、タイミング機能が必要です。
タイミング関数は、特定の時間または他のパラメータに応じて、プロパティの値(この場合は不透明度)を与える単純な数学関数です。
それ以外の場合は、フェードするために不透明度などのプロパティを使用する必要があります(不透明度は、要素とその子の透明度を定義するCSSプロパティです)
それでは、JSでsetTimeout関数を使用して非常に基本的な表示/非表示から始めましょう。
function getValue(t,dir){
if( dir > 0){
return 0.5*t; /* Y = mx + c */
}else{
return 1-(0.5*t);
}
/*
Here the slope of line m = 0.5.
t is the time interval.
*/
}
function animator(divID){
if(!(this instanceof animator)) return new animator(divID); /* Ignore this */
var Node = document.getElementById(divID),
start = new Date.getTime(), // The initiation.
now = 0,
dir = 1,
visible = true;
function step( ){
now = new Date.getTime();
var val = getValue( now - start,dir)
Node.style.opacity = val;
if( dir > 0 && val > 1 || dir < 0 && val < 0 ){
visible = !(visible*1);
// Optionally here u can call the block & none
if( dir < 0 ) { /* Hiding and hidden*/
Node.style.display = 'none'; // So if were repositioning using position:relative; it will support after hide
}
/* Our animation is finished lets end the continous calls */
return;
}
setTimeout(step,100); // Each step is executated in 100seconds
}
this.animate = function(){
Node.style.display = 'block';
dir *= -1;
start = new Date.getTime();
setTimeout(step,100);
}
}
これで、関数を呼び出すだけで済みます
var magician = new animator('divName');
次に、アニメーションを次のように切り替えます
magician.animate();
今、タイミング機能で遊んで、あなたはあなたが望むどんな可能性も作り出すことができます
return t^2 / ( 2 *3.23423 );
またはのようなさらに高い多項式
return t^3+6t^2-38t+12;
ご覧のとおり、この関数は非常に基本的ですが、純粋なjsを使用してアニメーションを作成する方法のポイントを説明しています。後でアニメーションにCSS3モジュールを使用し、javascriptでそれらのクラスをトリガーできます:-)
または、利用可能な場合はCSS3を使用してクロスブラウザポリフィルを作成し(より高速です)、そうでない場合はJSを作成します:-)