1

カルーセルを作成しようとしていますが、要素を div リストの末尾から削除して先頭に適用した後、リストの先頭のスペースを占有することなく、他の要素を下に移動するだけです。この件で私を助けてくれるかどうか見てください。

コードは次のとおりです。ボタンを使用してカルーセルを開始します。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="jquery-1.3.2.js"></script>
<script>
$(function(){
    $("#but").click(function(){
        $(".slides").animate({left: "+=170"},2000);
        //$('.wrap div').last().before$('.wrap div').first(;
        timer=setInterval(function() {
            //$(".wrap div:first-child").before($(".wrap div:last-child"))
            $( ".wrap div:first-child" ).before($( ".wrap div:last-child" ));
        },2000);
    });
});
</script>
<style>
.wrap
{
border:1px solid red;   
width:1000px;   
overflow:hidden;
height:100px;   

}
.slides
{
width:150px;
height:150px;
border:1px solid #C6F;  
margin-left:2px;
margin-right:2px;
float:left;
margin-top:2px;
margin-bottom:2px;  
text-align:center;
position:relative;

}
</style>
</head>
<body>
<div class="container">
  <div class="wrap">
    <div class="slides">1</div>
    <div class="slides">2</div>
    <div class="slides">3</div>
    <div class="slides">4</div>
    <div class="slides">5</div> 
  </div>
</div>
<button id ="but">hello</button>
</body>
</html>
4

3 に答える 3

0

1) アニメーションが終了したら、すべてのスライドの style="left:170px" を 0 に設定する必要があります

2) クリック時に div を変更する場合は、setTimeout を使用し、時間間隔をアニメーションよりも少し大きく設定する必要があります。そうしないと、動作しない場合があります (最後のアニメーション ティックとゼロ化の間の競合状態のようなものです)。

3) 170px は大きすぎます。155px にする必要があります。

div_width 150px + ボーダー 1px + マージン (左から) 2px + マージン (右から) 2px.

于 2013-09-18T19:46:25.460 に答える
0

一方向だけをアニメートすることはできません。170 ピクセルを追加し、それらを減算することはありません。リストの先頭に挿入したらすぐに左に移動し、ゆっくりとアニメーション化して表示する必要があります

<script>
$(function(){
    $("#but").click(function(){
        //$('.wrap div').last().before$('.wrap div').first(;
        timer=setInterval(function() {
            //$(".wrap div:first-child").before($(".wrap div:last-child"))
            $(".slides").animate({left: "-=170"},0);
            $(".slides").animate({left: "+=170"},2000);
            $( ".wrap div:first-child" ).before($( ".wrap div:last-child" ));
        },2000);
    });
});
</script>
于 2013-09-18T19:33:01.677 に答える