2017-01-01 8 views
1

最近私はdivのトランジションアニメーションについて同様の質問をしました。 (See this postCSS3 Divアニメーション相対的な間隔

以下のコードスニペットは私のソリューションを示しています。
ただし、アニメーションは、幅がピクセルで指定されていて、パーセントではなく、である場合にのみ機能します。
誰かがこれを回避する方法を知っていますか?

EDIT(私の問題を明確にする詳細情報):ウェブサイトのこのセクションで

は、私はいつも同じで、ユーザーの入力に「スワイプ」することができ、コンテンツの3ページをとどまるべき見出しを持っています。
したがって、ページの左余白の範囲は-100%〜+ 100%の範囲になります。

ユーザーがページ2(つまり画像を表示する)からページ3(つまり、画像に関連するテキスト)に切り替えることができるようにスワイプアニメーションが必要です。

ブラウザウィンドウのサイズが異なるため、幅がパーセンテージである必要があります。悲しいことに...

$(document).ready(function() { 
 
    $(".next").click(function() { 
 
    var current = $(".container").css("left"); 
 
    if (current == "-200px") { 
 
     current = "-400px"; 
 
    } else if (current == "0px") { 
 
     current = "-200px"; 
 
    } 
 
    $(".container").css("left", current); 
 
    }); 
 
    $(".prev").click(function() { 
 
    var current = $(".container").css("left"); 
 
    if (current == "-200px") { 
 
     current = "0px"; 
 
    } else if (current == "-400px") { 
 
     current = "-200px"; 
 
    } 
 
    $(".container").css("left", current); 
 
    }); 
 
});
.row { 
 
    border: 1px solid black; 
 
    height: 200px; 
 
    margin: 0; 
 
    width: 200px; 
 
    padding: 0; 
 
    display: block; 
 
    position: relative; 
 
    overflow: hidden; 
 
} 
 
.container { 
 
    height: 200px; 
 
    margin: 0; 
 
    width: 600px; 
 
    padding: 0; 
 
    display: block; 
 
    position: absolute; 
 
    left: -200px; 
 
    top: 0; 
 
    -webkit-transition: left 0.5s ease-in-out; 
 
    -moz-transition: left 0.5s ease-in-out; 
 
    transition: left 0.5s ease-in-out; 
 
} 
 
.ins { 
 
    width: 200px; 
 
    float: left; 
 
    height: 200px; 
 
    margin: 0; 
 
    padding: 0; 
 
    background-color: red; 
 
} 
 
.div1 { 
 
    background-color: red; 
 
} 
 
.div2 { 
 
    background-color: green; 
 
} 
 
.div3 { 
 
    background-color: blue; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<!-- Thanks to kittyCat at stackoverflow.com for helping me with this website.--> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
    <title>TITLE</title> 
 
    <meta name="Title" content="Main"> 
 
</head> 
 

 
<body> 
 

 
    <div class="row"> 
 
    <div class="container"> 
 
     <div class="ins div1">div-1</div> 
 
     <div class="ins div2">div-2</div> 
 
     <div class="ins div3">div-3</div> 
 
    </div> 
 
    </div> 
 
    <button class="prev">prev</button> 
 
    <button class="next">next</button> 
 

 

 
</body> 
 

 
</html>

+0

「px」単位の代わりに使用するパーセント値は何ですか? – guest271314

+0

100%(フルブラウザのウィンドウをいっぱいにしたい)、幅は80%前後です。 – Narusan

答えて

1

が、私は個々の要素に変換するために、左の位置を変更した:

さて、また、クラスの行がフルブラウザの幅を占めるように設定されています。コンテナクラスは300%です(3つの要素のための余裕があるためです)。そして子供たちはこの33%に設定されています。最後は100%です。

var pos = 2; /* values 1 - 2 or 3 */ 
 

 
$(document).ready(function() { 
 
    $(".next").click(function() { 
 
    if (pos == 1) { 
 
     $(".container").removeClass("pos1"); 
 
     $(".container").addClass("pos2"); 
 
     pos++; 
 
    } else if (pos == 2) { 
 
     $(".container").removeClass("pos2"); 
 
     $(".container").addClass("pos3"); 
 
     pos++; 
 
    } 
 
    }); 
 
    $(".prev").click(function() { 
 
    if (pos == 3) { 
 
     $(".container").removeClass("pos3"); 
 
     $(".container").addClass("pos2"); 
 
     pos--; 
 
    } else if (pos == 2) { 
 
     $(".container").removeClass("pos2"); 
 
     $(".container").addClass("pos1"); 
 
     pos--; 
 
    } 
 
    }); 
 
});
.row { 
 
    border: 1px solid black; 
 
    height: 200px; 
 
    margin: 0; 
 
    width: 100%; 
 
    padding: 0; 
 
    display: block; 
 
    position: relative; 
 
    overflow: hidden; 
 
} 
 
.container { 
 
    height: 200px; 
 
    width: 300%; 
 
    margin: 0; 
 
    padding: 0; 
 
    display: block; 
 
    position: absolute; 
 
    top: 0; 
 
} 
 
.ins { 
 
    width: 33.33%; 
 
    height: 200px; 
 
    margin: 0; 
 
    padding: 0; 
 
    float: left; 
 
    transition: transform 0.5s ease-in-out; 
 
} 
 
.div1 { 
 
    background-color: red; 
 
} 
 
.div2 { 
 
    background-color: green; 
 
} 
 
.div3 { 
 
    background-color: blue; 
 
} 
 

 
.pos2 .ins { 
 
transform: translateX(-100%); 
 
} 
 
.pos3 .ins { 
 
transform: translateX(-200%); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<!-- Thanks to kittyCat at stackoverflow.com for helping me with this website.--> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
    <title>TITLE</title> 
 
    <meta name="Title" content="Main"> 
 
</head> 
 

 
<body> 
 

 
    <div class="row"> 
 
    <div class="container pos2"> 
 
     <div class="ins div1">div-1</div> 
 
     <div class="ins div2">div-2</div> 
 
     <div class="ins div3">div-3</div> 
 
    </div> 
 
    </div> 
 
    <button class="prev">prev</button> 
 
    <button class="next">next</button> 
 

 

 
</body> 
 

 
</html>

+0

これは完全に私が探しているものではありません(少なくとも編集で説明した方法で動作させる方法はありません)。とにかく私を助けてくれてありがとう!私は問題と目標がより明確になるように、私の質問を詳細な情報で編集しました。 – Narusan

+1

私はいくつかの幅を変更しました。幸いにも、レイアウトのほとんどは同じままにすることができます。 – vals

+0

ありがとうございました。 – Narusan

1

Narusan、

私が正しくあなたの目標を理解していた場合は、問題の一部は関係なく、jQueryのあなたにピクセル単位を返すために欲しいもの、ということです。パーセンテージの値を設定することはできますが、パーセンテージを返すことはありません。

私はいくつかは、これを実証するようにコードを変更:

$(document).ready(function() { 
 
    $(".next").click(function() { 
 
    var current = $(".container").css("left"); 
 
    console.log(current); 
 
    if (current == "-200px" || current == "-100%") { 
 
     current = "-200%"; 
 
    } else if (current == "0%") { 
 
     current = "-100%"; 
 
    } 
 
    $(".container").css("left", current); 
 
    }); 
 
    $(".prev").click(function() { 
 
    var current = $(".container").css("left"); 
 
    console.log(current); 
 

 
    if (current == "-200px" || current == "-100%") { 
 
     current = "0%"; 
 
    } else if (current == "-200%") { 
 
     current = "-100%"; 
 
    } 
 
    $(".container").css("left", current); 
 
    }); 
 
});

「あなたはコンソールに出力値はピクセルで常にあることがわかりますが、あなたはDOMあなたを調べる場合要素に%値が設定されていることがわかります。

問題に近づくと、valsのように、良いアプローチのように見えます。

+0

申し訳ありませんが、私はより明確に目標を述べなければなりませんでした。私は自分の質問を編集したので、今理解しやすいと思う。ご協力ありがとうございました! – Narusan

関連する問題