2016-06-21 11 views
1

私は理解できない2つのことを除いて、ほとんどの場合機能するCSSスライダーを持っています。まず最初に、スライダーのナビゲーションボタンをどのようにして後の方法で動作させるか。今すぐクリックすると、私のaddClassは、クリックされたボタンだけでなく、すべてのボタンに.activeを追加します。それに加えて、スライダの位置をナビゲーションメニューに関連付ける方法もわかりません。私が意味することは、ユーザーがボタンのどれもクリックしていない場合でも、どのスライドがアクティブであるかをボタンに表示させたいということです。CSSスライダー付きのナビゲーションボタン

私の2番目の問題は、ボタンをクリックするたびにスライドショーを停止することです。

私の試みは間違っていますか?

トピックはありませんが、スライドをスライドさせるのではなく、スライドをフェードインさせるのが難しいですか?これについて

$('.control-button').click(function() { 
 
    button = $(this).attr('id'); 
 
    id = button.replace('slide', ''); 
 
    pos = (id - 1) * 100; 
 
    $('div#slider figure').css('animation-play-state', 'paused'); 
 
    $('div#slider figure').removeClass('figure2'); 
 
    $('.control-button').addClass('active'); 
 
    posStr = '-' + pos + '%'; 
 
    $('.figure').css('left', posStr); 
 
}); 
 

 
$('img').click(function() { 
 
    $('div#inner').css('left', '0px'); 
 
    $('div#slider figure').addClass('figure2'); 
 
    $('div#slider figure').css('animation-play-state', 'running'); 
 

 
})
div#slider { 
 
    width: 100%; 
 
    overflow: hidden; 
 
} 
 
div#slider .figure { 
 
    position: relative; 
 
    width: 400%; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 0; 
 
    text-align: left; 
 
    /*animation: 20s company-slider infinite;*/ 
 
} 
 
.figure2 { 
 
    animation: 20s company-slider infinite; 
 
} 
 
@keyframes company-slider { 
 
    0% { 
 
     left: 0%; 
 
    } 
 
    30% { 
 
     left: 0%; 
 
    } 
 
    35% { 
 
     left: -100%; 
 
    } 
 
    55% { 
 
     left: -100%; 
 
    } 
 
    60% { 
 
     left: -200%; 
 
    } 
 
    90% { 
 
     left: -200%; 
 
    } 
 
    95% { 
 
     left: -300%; 
 
    } 
 
    100% { 
 
     left: -300%; 
 
    } 
 
} 
 
div#slider figure img { 
 
    width: 25%; 
 
    min-height: 100%; 
 
    float: left; 
 
} 
 
/*div#slider figure:hover { animation-play-state:paused; }*/ 
 

 
div#slider li { 
 
    list-style: none; 
 
} 
 
div#slider label { 
 
    background-color: #111; 
 
    bottom: .5em; 
 
    cursor: pointer; 
 
    display: block; 
 
    height: .5em; 
 
    position: absolute; 
 
    width: .5em; 
 
    z-index: 10; 
 
} 
 
#controls { 
 
    width: 100%; 
 
    height: auto; 
 
} 
 
#control-container { 
 
    padding: 25px 12%; 
 
} 
 
.control-button { 
 
    display: inline; 
 
    margin: 0 2%; 
 
    width: 25%; 
 
    background: gray; 
 
    height: 10px; 
 
    border: none; 
 
} 
 
.control-button.active { 
 
    display: inline; 
 
    margin: 0 2%; 
 
    width: 25%; 
 
    background: black; 
 
    height: 10px; 
 
    border: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
 
</script> 
 
<div id="slider"> 
 
    <figure class="figure figure2"> 
 
     <img src="https://iso.500px.com/wp-content/uploads/2016/02/stock-photo-139669245.jpg" alt> 
 
     <img src="http://i.cbc.ca/1.3376224.1450794847!/fileImage/httpImage/image.jpg_gen/derivatives/4x3_620/tundra-tea-toss.jpg" alt> 
 
     <img src="https://static.pexels.com/photos/22804/pexels-photo.jpg" alt> 
 
     <img src="https://iso.500px.com/wp-content/uploads/2016/02/stock-photo-139669245.jpg" alt> 
 
    </figure> 
 
    <div id="controls"> 
 
     <div id="control-container"> 
 
      <button id="slide1" class="control-button"></button> 
 
      <button id="slide2" class="control-button"></button> 
 
      <button id="slide3" class="control-button"></button> 
 
     </div> 
 
    </div> 
 
</div>

+1

、なぜあなたは別のものを作成したいと思いますか?あなたが使っているIDやクラスを見ると、あなたは間違った方向に向いています。 – skobaljic

答えて

0

https://jsfiddle.net/q3j01xrs/
何?私はあなたのスクリプトを改善しました。あなたのCSSアニメーションを取り出し、別のJavaScriptアニメーションに置き換えました。見てみましょう!

$('.control-button').click(function() { 
    clearInterval(setInt); 
    $('.control-button').removeClass('active'); 
    $(this).addClass('active'); 
    var getIndex = $(this).index('.control-button'); 
    $('.figure img').hide(); 
    $('.figure img').eq(getIndex).show(); 
    setInt = setInterval(slide, 5000); 
}); 

function slide() { 
    var getIndex = $('.figure img:visible').index('.figure img'); 
    $('.control-button').removeClass('active'); 
    $('.figure img:visible').animate({ 
    width: 'toggle' 
    }, 350); 
    getIndex++; 
    if ((getIndex) == $('.control-button').length) { 
    $('.control-button').eq(0).addClass('active'); 
    $('.figure img').eq(0).animate({ 
     width: 'toggle' 
    }, 350); 
    } else { 
    $('.control-button').eq(getIndex).addClass('active'); 
    $('.figure img').eq(getIndex).animate({ 
     width: 'toggle' 
    }, 350); 
    } 
}; 
var setInt = setInterval(slide, 5000); 
1

限り、すべてのボタンに「アクティブ」追加などは、これを交換する必要があります。これで

$('.control-button').addClass('active'); 

$('.control-button').removeClass('active'); //this removes all '.active' first 
    $(this).addClass('active'); 
何ここで起こっていることは

であること.controlボタン内$(this)は現在の.control-button要素を表します。これは、アクティブなすべてのボタンを作る

+0

別のボタンをクリックすると、どのように削除されますか? – Becky

+0

また、どのようにしてコントロールボタンが表示されるスライドごとにアクティブなボタンを表示するようにすることができますか? – Becky

+0

私の答えを編集させてください – Brian

2

$('.control-button').addClass('active'); 

でそれを置き換えます

$('.control-button').click(function(event) { 

:あなたは event.targetを使用できるように

$('.control-button').removeClass('active'); 
$(event.target).addClass('active'); 

は、関数にeventパラメータを追加します。

編集:

画像のスライドが少し大きくなると、コントロールボタンが「自然に」アクティブになります。

各画像は、それをスライドさせていると言う属性を持っていることを確認しは次のとおりです。

<figure class="figure figure2"> 
    <img data-number="slide1" src="https://iso.500px.com/wp-content/uploads/2016/02/stock-photo-139669245.jpg" alt> 
    <img data-number="slide2" src="http://i.cbc.ca/1.3376224.1450794847!/fileImage/httpImage/image.jpg_gen/derivatives/4x3_620/tundra-tea-toss.jpg" alt> 
    <img data-number="slide3" src="https://static.pexels.com/photos/22804/pexels-photo.jpg" alt> 
    <img data-number="slide4" src="https://iso.500px.com/wp-content/uploads/2016/02/stock-photo-139669245.jpg" alt> 
</figure> 

画像が見えてくるときを検出するためのシステムを作成します。

window.setInterval(function() { 

が示さされている画像検出:

var activeImage = document.elementFromPoint($(window).width()/2, 10); // The image at the horizontal midpoint of the screen 

対応するコントロールボタンのclassをに設定します:

$('.control-button').removeClass('active'); 
    $("#"+$(activeImage).attr("data-number")).addClass('active'); // Sets whichever control button that corresponds to the image under the horizontal midpoint of the screen as active 

あなたがsetIntervalの閉鎖でチェックする頻度を設定:スライダープラグインの何百もあります

}, /*time interval in miliseconds*/); 
+0

ありがとうございました。スライドをクリックしなくてもボタンが自然にアクティブになるようにする方法はありますか? – Becky

+0

どのようにそれを行うにはどのようなアイデア? – Becky

関連する問題