javascript - Slider continuous switch -
i have slider switching between dividers using previous/next buttons. when dividers end, slider stops. how change when reaching end 1 side(next/previous) continues. let's have 4 div, 1 2 3 4 click next.. reach 4, when click 4 should continue 1. help?
html:
<div id="wrapper"> <div id="nav"> <button id="prev" disabled><<<</button> <button id="next">>>></button> </div> <div id="mask"> <div id="item1" class="item"> <a name="item1"></a> <div class="content"> <img id="image1" src="http://placehold.it/350x150" /> <img id="image2" src="http://placehold.it/350x150" /> <img id="image3" src="http://placehold.it/350x150" /> <img id="image4" src="http://placehold.it/350x150" /> <img id="image5" src="http://placehold.it/350x150" /> <img id="image6" src="http://placehold.it/350x150" /> <img id="image7" src="http://placehold.it/350x150" /> <img id="image8" src="http://placehold.it/350x150" /> <img id="image9" src="http://placehold.it/350x150" /> <img id="image10" src="http://placehold.it/350x150" /> </div> </div> <div id="item2" class="item"> <div class="content"> <img id="image1" src="http://placehold.it/350x150" /> <img id="image2" src="http://placehold.it/350x150" /> <img id="image3" src="http://placehold.it/350x150" /> <img id="image4" src="http://placehold.it/350x150" /> <img id="image5" src="http://placehold.it/350x150" /> <img id="image6" src="http://placehold.it/350x150" /> <img id="image7" src="http://placehold.it/350x150" /> <img id="image8" src="http://placehold.it/350x150" /> <img id="image9" src="http://placehold.it/350x150" /> <img id="image10" src="http://placehold.it/350x150" /> </div> </div> </div> </div> css:
body { margin: 0; } #wrapper { width: 100%; height: 350px; /*position: absolute;*/ top: 0; left: 0; background-color: white; overflow: hidden; } #nav button { position: absolute; top: 100px; } #prev { left: 40px; } #next { right: 40px; } #mask { width: 50000px; height: 100%; background-color: white; } .item { width: 1200px; height: 100%; float: left; background-color: white; } .content img { height: 100px; width: 17%; float:left; margin-right: 10px; margin-bottom: 10px; } .content { width: 50%; height: 220px; top: 20%; margin: auto; background-color: white; position: relative; } .content { position: relative; top: -17px; left: 170px; } .selected { background: #fff; font-weight: 700; } .clear { clear:both; } jquery:
$(document).ready(function () { function shift(direction) { var $mask = $('#mask'), $items = $('.item'), items = $items.size(), currentitem = $mask.data('currentitem'), newitem; if (currentitem == undefined) { currentitem = 0; } newitem = currentitem + direction; $mask.data('currentitem', newitem).animate({ marginleft: -newitem * $items.eq(0).width() }); if (newitem == 0) { $("#prev").prop('disabled', true); } else { $("#prev").prop('disabled', false); } if (newitem == items - 1) { $("#next").prop('disabled', true); } else { $("#next").prop('disabled', false); } } $('#prev').click(function () { return shift(-1); }); $('#next').click(function () { return shift(1); }); function resizepanel() { width = $(window).width(); height = $(window).height(); $('#wrapper, .item').css({ width: width, height: height }); } $(window).resize(function () { resizepanel(); }); resizepanel(); }); jsfiddle: http://jsfiddle.net/909zce06/7/
i have changed code bit accomodate cyclic transition of images.
what have done:
- the code disable previous/next button removed, restrict cyclic sliding of images.
- extra code added handle transition when slider hits extreme ends(either direction).
js code(relevant code) :
$('#prev').click(function () { if (newitem === 0) { newitem = itemcount - 1; } else { newitem--; } return shift(); }); $('#next').click(function () { if (newitem === itemcount - 1) { newitem = 0; } else { newitem++; } return shift(); }); live demo @ jsfiddle:
http://jsfiddle.net/dreamweiver/909zce06/12/
suggestion:if planning extend current slider more features, suggest go existing slider plugins, "why re-invent wheel when have 1 already"
happy coding :)
Comments
Post a Comment