$.fn.imagesSlider = function (opts) { var opts = $.extend({ touch_speed: 1, step: 50, // px duration: 700 }, opts); var el = this; var $wrap = el.find('.photos_slider-wrap'), $prev = el.find('.photos_slider-prev'), $next = el.find('.photos_slider-next'), is_overflow, last_x, last_scroll; if (!$wrap.length) { el.children().wrapAll('
'); $prev = $(' '); $next = $(' '); el.addClass('photos_slider').append($prev).append($next); $wrap = el.find('.photos_slider-wrap'); function move(pos, scroll) { var new_scroll = Math.floor(Math.max(0, (typeof scroll == "number" ? scroll : $wrap.scrollLeft()) + pos)); $wrap.scrollLeft(new_scroll); $prev.toggle($wrap.scrollLeft() > 0); if (pos < 0) { $next.show(); } else { $next.toggle(new_scroll == Math.floor($wrap.scrollLeft())); } } $prev.on('click', function (e) { e.preventDefault(); move(-opts.step); $next.show(); }); $next.on('click', function (e) { e.preventDefault(); move(opts.step); }); $wrap.on('touchstart', function (e) { var touches = e.originalEvent.touches; if (touches.length > 1) return; last_x = touches[0].clientX, last_scroll = $wrap.scrollLeft(); }); $wrap.on('touchmove', function (e) { e.stopPropagation(); var touches = e.originalEvent.touches; if (touches.length > 1) return; var x = touches[0].clientX; move((last_x - x) * opts.touch_speed, last_scroll); return false; }); } var last = $wrap.children().addClass('photos_slider-item').last(); is_overflow = (($wrap.offset().left + $wrap.innerWidth()) <= (last.offset().left + last.outerWidth())); $next.toggle(is_overflow); $prev.hide(); return this; };