Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Before inspecting examples, you should know two jQuery functions which i used most. </p> <p><a href="http://api.jquery.com/index/">index()</a> returns value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.</p> <p><a href="http://api.jquery.com/eq/">eq()</a> selects of an element based on its position (index value).</p> <p>Basicly i take selected trigger element's <code>index value</code> and match this value on images with <code>eq</code> method.</p> <p><a href="http://jsfiddle.net/mjaA3/1662/"> - <strong>FadeIn / FadeOut</strong> effect.</a></p> <p><a href="http://jsfiddle.net/mjaA3/1672/"> - <strong>Sliding</strong> effect.</a></p> <p><a href="http://jsfiddle.net/mjaA3/332/"> - alternate <strong>Mousewheel</strong> response.</a></p> <hr> <p>​<strong>html sample:</strong></p> <pre><code>&lt;ul class="images"&gt; &lt;li&gt; &lt;img src="images/1.jpg" alt="1" /&gt; &lt;/li&gt; &lt;li&gt; &lt;img src="images/2.jpg" alt="2" /&gt; &lt;/li&gt; ... &lt;/ul&gt; &lt;ul class="triggers"&gt; &lt;li&gt;1&lt;/li&gt; &lt;li&gt;2&lt;/li&gt; ... &lt;/ul&gt; &lt;span class="control prev"&gt;Prev&lt;/span&gt; &lt;span class="control next"&gt;Next&lt;/span&gt; </code></pre> <hr> <h2>OPACITY EFFECT: <a href="http://jsfiddle.net/mjaA3/1662/"><strong>jsFiddle.</strong></a></h2> <p><strong><em>css trick:</strong> overlapping images with position:absolute</em></p> <pre><code>ul.images { position:relative; } ul.images li { position:absolute; } </code></pre> <p><strong>jQuery:</strong></p> <pre><code>var target; var triggers = $('ul.triggers li'); var images = $('ul.images li'); var lastElem = triggers.length-1; triggers.first().addClass('active'); images.hide().first().show(); function sliderResponse(target) { images.fadeOut(300).eq(target).fadeIn(300); triggers.removeClass('active').eq(target).addClass('active'); } </code></pre> <hr> <h2>SLIDING EFFECT: <a href="http://jsfiddle.net/mjaA3/1672/"><strong>jsFiddle.</strong></a></h2> <p><strong><em>css trick:</strong> with double wrapper and use child as mask</em></p> <pre><code>.mask { float:left; margin:40px; width:270px; height:266px; overflow:hidden; } ul.images { position:relative; top:0px;left:0px; } /* this width must be total of the images, it comes from jquery */ ul.images li { float:left; } </code></pre> <p><strong>jQuery:</strong></p> <pre><code>var target; var triggers = $('ul.triggers li'); var images = $('ul.images li'); var lastElem = triggers.length-1; var mask = $('.mask ul.images'); var imgWidth = images.width(); triggers.first().addClass('active'); mask.css('width', imgWidth*(lastElem+1) +'px'); function sliderResponse(target) { mask.stop(true,false).animate({'left':'-'+ imgWidth*target +'px'},300); triggers.removeClass('active').eq(target).addClass('active'); } </code></pre> <hr> <h2>Common jQuery response for both slider:</h2> <p><em>( <strong>triggers + next/prev click and timing</strong> )</em></p> <pre><code>triggers.click(function() { if ( !$(this).hasClass('active') ) { target = $(this).index(); sliderResponse(target); resetTiming(); } }); $('.next').click(function() { target = $('ul.triggers li.active').index(); target === lastElem ? target = 0 : target = target+1; sliderResponse(target); resetTiming(); }); $('.prev').click(function() { target = $('ul.triggers li.active').index(); lastElem = triggers.length-1; target === 0 ? target = lastElem : target = target-1; sliderResponse(target); resetTiming(); }); function sliderTiming() { target = $('ul.triggers li.active').index(); target === lastElem ? target = 0 : target = target+1; sliderResponse(target); } var timingRun = setInterval(function() { sliderTiming(); },5000); function resetTiming() { clearInterval(timingRun); timingRun = setInterval(function() { sliderTiming(); },5000); } </code></pre> <hr>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload