Грамотное построение карусели
Написал карусельку, подскажите - как лучше доработать код?
Ошибка появляется, если кликать очень быстро на 'prev' или 'next' - карусель "проезжает" наше else $(document).ready(function() { $('.slider ul').each(function() { // ширина каруселек child = $(this).children('li'); $(this).width(child.length * (child.width() + parseInt(child.css('marginLeft'), 10) + parseInt(child.css('marginRight'), 10) ) ); $(this).after('<div class="next"></div>').before('<div class="prev"></div>') }); slide=$('.slider ul li'); mLeft=parseInt(slide.css('marginLeft'), 10); mRight=parseInt(slide.css('marginRight'), 10); totWidth=mLeft+mRight+slide.width(); // Полная длинна одного слайда $('.slider .next').click(function() { // Вправо if ($(this).prev('ul').css('marginLeft') != -(totWidth)*(slide.length-1)+'px') { $(this).prev('ul').stop(false, true).animate({ marginLeft: -(totWidth)+parseInt(slide.parent().css('marginLeft'), 10) }, 300); } else { // вперед не заезжаем $(this).prev('ul').stop(false, true).animate({marginLeft: 0}, 300); } }) $('.slider .prev').click(function() { // Влево if (parseInt($(this).next('ul').css('marginLeft'), 10) != '0') { $(this).next('ul').stop(false, true).animate({ marginLeft: totWidth + parseInt(slide.parent().css('marginLeft'), 10) } ,300) } else { // назад не заезжаем $(this).next('ul').stop(false, true).animate({marginLeft: -(totWidth)*(slide.length-1)}, 300) } }); }); Не критикуйте сильно, только начинаю писать на js. Думал по поводу события dblclick() - задать ему return false, но ведь от "третьего быстрого клика" не избавит? |
а готовую карусель взять не вариант??))
|
смотрите в фаербаге (отладчик ошибок). что в консоль выводит? или в консоли пусто?
|
Цитата:
|
Цитата:
|
Цитата:
http://codepen.io/alex_misch/pen/hncjD вот пример, попробуйте пощелкать по красным прямоугольникам очень быстро |
Цитата:
|
Цитата:
|
Цитата:
задаем глобальную переменную var aa=0; $('.slider .next').click(function() { if(aa)return;aa=1;// а по завершении анимации в функции complete aa=0 ----------------- идея на время анимации запретить клики на prev и next |
попробуйте так:
$('.slider .prev').click(function() { // Влево if (parseInt($(this).next('ul').css('marginLeft'), 10) != '0') { $(this).next('ul').stop(false, true).animate({ marginLeft: totWidth + parseInt(slide.parent().css('marginLeft'), 10) } ,300) } else { // назад не заезжаем $(this).next('ul').stop(false, true).animate({marginLeft: -(totWidth)*(slide.length-1)}, 300) } else console.log(1); }); посмотрите, в консоль ничего не валится?? |
Цитата:
Цитата:
|
я вам и показываю, в каком месте у вас не доработка
|
else console.log(1);
в середине функции - к какому if оно относится? Извините, но вы написали бред. Код доработал, спасибо vadim5june! $(document).ready(function() { $('.slider ul').each(function() { // ширина каруселек child = $(this).children('li'); $(this).width(child.length * (child.width() + parseInt(child.css('marginLeft'), 10) + parseInt(child.css('marginRight'), 10) ) ); $(this).after('<div class="next"></div>').before('<div class="prev"></div>') }); aa=true; slide=$('.slider ul li'); mLeft=parseInt(slide.css('marginLeft'), 10); mRight=parseInt(slide.css('marginRight'), 10); totWidth=mLeft+mRight+slide.width(); // Полная длинна одного слайда $('.slider .next').click(function() { // Вправо if ($(this).prev('ul').css('marginLeft') != -(totWidth)*(slide.length-1)+'px' && aa) { aa=false; $(this).prev('ul').stop(false, true).animate({ marginLeft: -(totWidth)+parseInt(slide.parent().css('marginLeft '), 10) }, 300, function() {aa=true}); } else { if(aa) {// вперед не заезжаем $(this).prev('ul').stop(false, true).animate({marginLeft: 0}, 300); } } }) $('.slider .prev').click(function() { // Влево if (parseInt($(this).next('ul').css('marginLeft'), 10) != '0' && aa) { aa=false; $(this).next('ul').stop(false, true).animate({ marginLeft: totWidth + parseInt(slide.parent().css('marginLeft'), 10) } ,300, function() {aa=true}) } else { if(aa) { // назад не заезжаем $(this).next('ul').stop(false, true).animate({marginLeft: -(totWidth)*(slide.length-1)}, 300) } } }); }); |
согласен, не доглядел. я просто к такому стилю закрыванию\открыванию кавычек не привык. огляделся
|
Цитата:
|
Часовой пояс GMT +3, время: 23:41. |