0

フェード アニメーションを使用して小さな画像回転子をスクリプト化する方法を知りたいのですが、いくつかの div があり、 next と prev の 2 つのボタンを追加します。

<div id="target" class="cible" style="height: 240px;">   
     <div id="211" class="block" style="display: block;"> </div>
     <div id="491" class="block" style="display: none;"> </div>
     <div id="496" class="block" style="display: none;"> </div>

およびボタン付きのdiv

   <div id="next" class="next"></div>
   <div id="prev" class="prev"></div>

私は .next() と .prev() と addClass() の関数を試してみましたが、現在の div を知るメカニズムが本当にわかりませんでした。説明できる人がいたら

ありがとう

私はこれを試してみましたが、うまくいきますが、実際にはうまくいきません。

 $(".next, .prev").click(function() {
var next_visible = $(this).hasClass(".prev") ? 
        $(".block:visible").prev(".block") : 
        $(".block:visible").next(".block");
$(".block").hide();
next_visible.show();
   });
4

3 に答える 3

1

あなたの新しい要件を考えると、私はこれを行います:

アイデアは、アクティブな要素をクラスとして持つことであり、前または次のボタンをクリックすると、アクティブな要素を前または次の要素に変更します。CSS では、表示されているアクティブなスライドを除いて、すべてのスライドが非表示になっていると言います。

HTML:

<div id="target" class="cible" style="height: 240px;">   
     <div id="211" class="slide">211</div>
     <div id="491" class="slide active">491</div>
    <div id="496" class="slide">496</div>
</div>
<!-- same next and prev buttons as yours -->

CSS:

.slide {
  display:none;   
}
.active {
 display:block;   
}

jQuery:

$("#next").click(function() {
    activeElem = $("#target .active");
    if (!activeElem .is(':last-child')) {
     activeElem .removeClass('active').next().addClass('active');
    }
});
// similar click event for the $("#prev") button

jsFiddle はこちら: http://jsfiddle.net/guz7D/


同じ考えに基づいて、コードを変更して、新しい要素のフェードインを含めました。

activeElem .removeClass('active').fadeOut(0).next().addClass('active').fadeIn(400);

jsFiddle はこちら: http://jsfiddle.net/guz7D/1/

于 2012-04-06T18:07:34.593 に答える
0

そのためには、本当に jQuery プラグインを使用する必要があります。たとえば、nivo スライダーは優れた多価スライダー プラグインです。

非常に単純なものが必要な場合は、jQuery プラグインtiny casouselがあります。

于 2012-04-06T13:09:04.227 に答える
0

多くのカスタム スライダーを作成した後、これが私が使用する方法です。作成される jQuery オブジェクトの数を最小限に抑えるために、このようにしています。これは、変数によって管理されているアクティブな要素を見つけるためにテストしていないためです。$(elem:visable) や $(elem).hasClass のようなことをするのは jQuery オブジェクトの無駄です。本当に必要でない限り、jQuery を使用してアプリケーションの状態を検出するべきではありません。

//create a universal block jQuery object, and give it an index property (current)
var blocks = $('div.blocks');
    blocks.current = 0;

var next   = $('.next'),
    prev   = $('.prev');

//make a universal slide handler
function blockControl(index){
    //using hide and show here to fit your example, 
    //but this is where you would do slide or fades or whatever animation
    //the important part is it is 'showing' based on .eq()
    blocks.hide().eq(index).show();
}

//setup the initial state
blockControl(blocks.current);

next.on('click', function(){
    //move current up one        
    blocks.current ++;
    //test if its the last block
    if( blocks.current >= blocks.length ){
        //prevent over reach
        blocks.current = blocks.length;
        //handle how the last slide will work, you could loop back the beginning or 
        //whatever. Here we are just going to hide the next button.
        next.fadeOut();
        prev.fadeIn();
    }else{                        
        //insure prev button is visalbe
        prev.fadeIn();            
        //do slider
        blockControl(blocks.current);
    }           
});

prev.on('click', function(){
    //move current down one        
    blocks.current --;
    //test if its the first block
    if( blocks.current <= 0 ){
        //prevent negative numbers
        blocks.current = 0;
        //Here we are just going to hide the prev button. But we also could put in control 
        //to loop the last or whatever
        prev.fadeOut();
        next.fadeIn();
    }else{                        
        //insure next button is visalbe
        next.fadeIn();            
        //do slider
        blockControl(blocks.current);
    }           
});

ここでのロジックの分離は重要です。ブロックの視覚化を制御するための単一のハンドラーがあるということは、矢印以外のものによってトリガーできることを意味します。また、矢印のロジックとコントローラーのロジックは、互いに独立して変更できます。また、テストやクエリを実行することなく、アプリケーションのどの部分からでも、現在表示されている要素にいつでもアクセスできます。

ここでの概念と、なぜそれが分離されているのかを本当に理解していることを願っています. このパターンは、ほぼすべての種類のスライダーまたはコンテンツ ローテーターに使用できます。たとえば、小さなクリック可能なブレット インジケーターが必要な場合は、簡単にフックできます。

var bullets = $('a.bullet');
bullets.on('click',function(){
     var index = $(this).index();
     bullets.removeClass('active').eq(index).addClass('active');
     blockControl(index);
});

など。

これを OO スタイルにするとさらにクールになるので、1 ページに複数のスライダーを配置できます。しかし、それは別の話です。

于 2012-04-06T18:57:39.900 に答える