Browse Source

Filter children to slides only when determining number of slides

When using MVC frameworks (Ember) to generate the HTML, additional
`<script>` nodes are inserted inside the `.slide-group` element. These
extra nodes get returned by `slider.children`, causing an incorrect
number of slides to be calculated, and hence when scrolling to the end
of the slides, you scroll into nothingness.

This patch counts only the `.slide` nodes within the slider to get the
correct slide count.
pull/708/head
Ruy Diaz 10 years ago
parent
commit
8acc21b02f
  1. 10
      js/sliders.js

10
js/sliders.js

@ -47,11 +47,12 @@
}; };
var setSlideNumber = function (offset) { var setSlideNumber = function (offset) {
var slideCount = slider.querySelectorAll('.slide').length;
var round = offset ? (deltaX < 0 ? 'ceil' : 'floor') : 'round'; var round = offset ? (deltaX < 0 ? 'ceil' : 'floor') : 'round';
slideNumber = Math[round](getScroll() / (scrollableArea / slider.children.length)); slideNumber = Math[round](getScroll() / (scrollableArea / slideCount));
slideNumber += offset; slideNumber += offset;
slideNumber = Math.min(slideNumber, 0); slideNumber = Math.min(slideNumber, 0);
slideNumber = Math.max(-(slider.children.length - 1), slideNumber); slideNumber = Math.max(-(slideCount - 1), slideNumber);
}; };
var onTouchStart = function (e) { var onTouchStart = function (e) {
@ -60,14 +61,15 @@
if (!slider) { if (!slider) {
return; return;
} }
var slideCount = slider.querySelectorAll('.slide').length;
var firstItem = slider.querySelector('.slide'); var firstItem = slider.querySelector('.slide');
scrollableArea = firstItem.offsetWidth * slider.children.length; scrollableArea = firstItem.offsetWidth * slideCount;
isScrolling = undefined; isScrolling = undefined;
sliderWidth = slider.offsetWidth; sliderWidth = slider.offsetWidth;
resistance = 1; resistance = 1;
lastSlide = -(slider.children.length - 1); lastSlide = -(slideCount - 1);
startTime = +new Date(); startTime = +new Date();
pageX = e.touches[0].pageX; pageX = e.touches[0].pageX;
pageY = e.touches[0].pageY; pageY = e.touches[0].pageY;

Loading…
Cancel
Save