【发布时间】:2017-12-09 17:46:37
【问题描述】:
我设置了一个滚动轮播,当您使用鼠标滚轮滚动时,轮播向左/向右滚动。
当您向下滚动时,轮播向右移动,当偏移量为零时,它会停止滚动。但是,当您向上滚动时,轮播到达轮播的末尾时,它会弹回到开头。我怎样才能让它在它结束时停止?我知道我的第二个三元运算符的数学是错误的,但我不确定它的数学是什么。
https://jsfiddle.net/obo4LkLL/
// Stop the scroll from going backwards too far
this.offset = this.offset > 0 ? 0 : this.offset
// Stop the scroll from going forwards too far
this.offset = Math.abs(this.offset) + this.element.clientWidth > this.element.scrollWidth ?
Math.abs(this.offset) + this.element.clientWidth : this.offset
// Set the offset foreach child in the carousel
this.items.forEach(item => {
if (!item.style) return
item.style.transform = `translateX(${this.offset}px)`
})
【问题讨论】:
-
如果 Math.abs(offset) 大于 total_width_of_children_divs (800px) - 容器宽度 (500px),停止滚动...在您的情况下应该是数学...300:jsfiddle.net/obo4LkLL/1
-
500px 不是轮播的保证宽度。它通常设置为父级的 100%,因此它应该是动态的
-
嗯……你可以通过js轻松获取容器宽度,对吧?
标签: javascript carousel