【问题标题】:Stop carousel from scrolling when it gets to the end轮播结束时停止滚动
【发布时间】: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


【解决方案1】:

所以,基本逻辑是 - 我们需要可变限制,它等于:轮播中子 div 的总宽度 - 轮播/容器宽度。为了使所有动态,您可以执行以下操作:

class carousel {

  constructor(element) {
  this.total_width=0;


    this.element = element  


    this.items = []
    this.offset = 0
    this.scrollSpeed = 20
    this.element.childNodes.forEach(node => this.items.push(node))
    this.element.childNodes.forEach(node => {if(node.nodeType===1) return this.total_width+=node.clientWidth})
    element.addEventListener('wheel', this.wheel.bind(this))

  }

  wheel(e) {
    // Scroll up
    if (e.deltaY < 0) {
      this.offset -= this.scrollSpeed
    }
    // Scroll down
    else {
      this.offset += this.scrollSpeed
    }
    // Stop the scroll from going backwards too far
   // console.log(this.offset);
    this.offset = this.offset > 0 ? 0 : this.offset

    //stop scroll
    this.limit=this.total_width-this.element.clientWidth;





    if(Math.abs(this.offset)>this.limit) {
    this.offset=-this.limit;
    }

    // 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)`
    })
  }
}

document.querySelectorAll('.carousel').forEach(e => new carousel(e))

我刚刚添加了一些变量(total_width、limit)和停止滚动的条件。

演示:

class carousel {

  constructor(element) {
  this.total_width=0;
 
  
    this.element = element  
  
   
    this.items = []
    this.offset = 0
    this.scrollSpeed = 20
    this.element.childNodes.forEach(node => this.items.push(node))
    this.element.childNodes.forEach(node => {if(node.nodeType===1) return this.total_width+=node.clientWidth})
    element.addEventListener('wheel', this.wheel.bind(this))

  }

  wheel(e) {
    // Scroll up
    if (e.deltaY < 0) {
      this.offset -= this.scrollSpeed
    }
    // Scroll down
    else {
      this.offset += this.scrollSpeed
    }
    // Stop the scroll from going backwards too far
   // console.log(this.offset);
    this.offset = this.offset > 0 ? 0 : this.offset
    
    //stop scroll
    this.limit=this.total_width-this.element.clientWidth;
    
   // console.log(this.limit);
   
  
   
    if(Math.abs(this.offset)>this.limit) {
    this.offset=-this.limit;
    }
    
    // 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)`
    })
  }
}

document.querySelectorAll('.carousel').forEach(e => new carousel(e))
.carousel {
  width: 500px;
  overflow: hidden;
  white-space: nowrap;
  border:3px solid green;
}

.carousel>div {
  width: 100px;
  height: 100px;
  display: inline-block;
  margin-right:-4px;
}

.carousel>div:nth-child(even) {
  background: red;
}

.carousel>div:nth-child(odd) {
  background: blue;
}
<div class="carousel">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多