4

前後の矢印を使用して、水平スクロール サイトを構築しようとしています (大まかな js フィドルをここで見ることができます: http://jsfiddle.net/KcDqu/5/ )

これは私がこれまでに持っているjqueryです:

$(document).ready(function() {
$('a.right-arrow').click(function() {
   $('section').animate({
   marginLeft: "+=920"
   }, "medium");
   });
   $('a.left-arrow').click(function() {
   $('section').animate({
   marginLeft: "-=920"
   }, "medium");
   });
});

これはこれまでのところうまくいきます。ただし、これに追加したいことが2つあります。まず、最初の表示では、左側に表示されるコンテンツがなく、ユーザーが空白にスクロールするだけでなく、左矢印が表示されないようにする必要があります。

次に、同じ理由で右に表示するコンテンツがなくなったときに、右矢印を非表示にします。

これを行うための最良の方法を理解できないようです...

どんな助けでも大歓迎です。

4

3 に答える 3

1

水平方向のスライドは920にハードコーディングされており、それが常にウィンドウの幅であるとは限らないため、コーディングは少し困難でした。これにより、ウィンドウのサイズによっては一部のコンテンツがスキップされます。

このjQueryで正しく機能するように矢印を取得しました:

$(document).ready(function () {
    var leftMargin = 0;
    var width = $(document).width();
    var windowWidth = $(window).width();
    $('.left-arrow').click(function () {
        $('section').animate({
            marginLeft: "+=" + windowWidth 
        }, "medium");

        $('.right-arrow').show();
        leftMargin = (leftMargin - windowWidth)
        if (leftMargin == 0) {
            $('.left-arrow').hide();
        }
    });
    $('.right-arrow').click(function () {
        $('section').animate({
            marginLeft: "-=" + windowWidth
        }, "medium");

        $('.left-arrow').show();
        leftMargin = (leftMargin + windowWidth);
        if (leftMargin > width - windowWidth) {
            $('.right-arrow').hide();
        }
    });
});

これにより、矢印がウィンドウの幅にスライドするように設定されるため、コンテンツを見逃すことはありません。

また、jsFiddle

編集:不要だったため、elseを削除しました。

于 2013-01-24T22:03:27.697 に答える
0

セクションのmargin-left値を取得して、これを条件の記述に使用できます。正確な値を取得するために、アニメーション関数のコールバック関数内で条件を使用することを忘れないでください。

ここにjsFiddleがあります。

$(document).ready(function() {
    var windowWidth = $(window).width();
    var maxRange = windowWidth * -2;

    //note: all of the conditions are same
    var val = parseInt($('section').css('margin-left'));
    if( val >= 0 ) { $('.right-arrow').hide();$('.left-arrow').show(); }
    else if( val < 0 && val > maxRange) { $('.right-arrow, .left-arrow').show(); }
    else if( val <= maxRange) { $('.left-arrow').hide();$('.right-arrow').show();  }


    $('a.right-arrow').click(function() {
        $('section').animate({ marginLeft: "+=" +windowWidth+ "px" },600,function() {
            var val = parseInt($('section').css('margin-left'));
            if( val >= 0 ) { $('.right-arrow').hide();$('.left-arrow').show(); }
            else if( val < 0 && val > maxRange) { $('.right-arrow, .left-arrow').show(); }
            else if( val <= maxRange) { $('.left-arrow').hide();$('.right-arrow').show();  }
        });
    });
    $('a.left-arrow').click(function() {
        $('section').animate({ marginLeft: "-=" +windowWidth+ "px" },600,function() {
            var val = parseInt($('section').css('margin-left'));
            if( val >= 0 ) { $('.right-arrow').hide();$('.left-arrow').show(); }
            else if( val < 0 && val > maxRange) { $('.right-arrow, .left-arrow').show(); }
            else if( val <= maxRange) { $('.left-arrow').hide();$('.right-arrow').show();  }
        });
    });
});

また、これらの状況では使用しないelseでください。競合が発生する可能性があることに注意してください。

于 2013-01-24T21:58:17.297 に答える
0

通常、既存のスライダー スクリプトへのリンクをいくつか提供しますが、サイズ変更を適切に処理するものは知りません。

だから私は自分のPHP例外ハンドラからいくつかのコードを適応させました:)

$('#list').each(function(){

  var list = $(this),
      currentPos = 0,
      itemCount = list.children().length;

  $('.right, .left').click(function(){

    // +100 = right, -100 = left
    var direction = $(this).hasClass('right') ? 100 : -100,
        nextIndex = direction > 0 ? currentPos + 1 : currentPos - 1;

    // do nothing if there is animation happening,
    // or if index is out of boundaries
    if($('> li', list).is(':animated') 
       || (direction > 0 && nextIndex > itemCount - 1)
       || (direction < 0 && nextIndex < 0)
     ) return;

    var next = $('> li:eq(' + nextIndex + ')', list),
        current = $('> li:eq(' + currentPos + ')', list);

    currentPos = nextIndex;

    // determine if the link needs to be hidden
    $('.left, .right').removeClass('hide');      
    if(currentPos === 0 || currentPos === itemCount - 1)
        $(this).addClass('hide');

    // adjust height of the container
    list.css('height', Math.max(current.outerHeight(true), next.outerHeight(true)) + 'px');

    // make the current slide absolute
    current.css({
      position: 'absolute',
      left: 0,
      top: 0,
      width: '100%',
      height: current.outerHeight(true) + 'px'
    });

    // show the next slide
    next.css({
      position: 'absolute',
      left: direction + '%',
      top: 0,
      width: '100%',
      height: next.outerHeight(true) + 'px'
    }).show().animate({left: '0%'}, 300, 'swing', function(){
      next.css({
        position: 'relative',
        left: 'auto',
        top: 'auto'
      });

    });

    // hide the current slide
    current.animate({left: (-direction) + '%'}, 300, 'swing', function(){
      current.hide();                      
    });                                       

  });

  // mouse wheel action within the list area
  list.mousewheel(function(event, delta){               
    (delta > 0) ? $('.right').click() :$('.left').click();                                 
  });

});           

CSS:

.hide{
  display:none;
}    

#list{
  position: relative;
  width: 100%;
  overflow: hidden;
}

#list > li{
  display: none;
}

#list > li:first-child{
  display: block;
}

HTML 構造は次のようになります。

<a class="left"> left </a>
<a class="right"> right </a>

<ul id="list">                    
   <li> ... </li>
   ...         
</ul>

デモ

マウスホイールjQuery プラグイン。マウスホイールのサポートが必要ない場合は、最後のイベントを削除してください。

于 2013-01-24T22:20:04.093 に答える