垂直自动滚动

class Scroll {
  constructor (options = {}) {
    // 滚动固定区域
    this.fixedBox = options.el
    // 复制的滚动区域
    this.copyFixedBox = null
    // 滚动的区域
    this.scrollBox = null
    // 滚动区域里的子元素
    this.scrollChildrens = []
    // 滚动的区域className
    this.className = options.className || ''
    // 滚动方向
    this.direction = options.direction || 'top'
    // 是否开启分页加载功能
    this.isPage = options.isPage || false
    // 是否正在处于分页加载状态,如果加载时间太长,组织下一次加载
    this.isAjax = false
    // 回调函数
    this.callBack = options.callBack || null
    // 滚动时间
    this.timer = null
    // 滚动速度
    this.speed = options.speed || 2
    // x轴移动值
    this.x = 0
    // y轴移动值
    this.y = 0
    this.methods = options.methods || null
    this.init()
  }
  
  // 初始化
  init () {
    if (!this.fixedBox) { 
      console.error('请传入需要滚动的元素')
      return
    }
    const children = this.fixedBox.childNodes
    if (children[0].offsetHeight < this.fixedBox.offsetHeight) {
      return false
    }
    this.fixedBox.style.display = 'none'
    const contentParent = this.fixedBox.parentNode
    const contentHTML = contentParent.innerHTML
    const div = document.createElement('div')
    div.innerHTML = contentHTML
    const divFirstChildNode = div.childNodes[0]
    contentParent.appendChild(divFirstChildNode)
    this.copyFixedBox = divFirstChildNode
    const fixedHtml = this.fixedBox.innerHTML
    
    this.copyFixedBox.style.display = 'block'
    
    this.scrollBox = document.createElement('div')
    this.scrollBox.className = this.className
    for (let i = 0; i < 2; i++) {
      this.scrollBox.innerHTML += fixedHtml
    }
    this.copyFixedBox.innerHTML = ''
    this.scrollChildrens = this.scrollBox.childNodes
    if (this.methods) {
      this.scrollChildrens.forEach(item => {
        item.childNodes.forEach((child, childIndex) => {
          child.onclick = () => {
            this.methods(childIndex)
          }
        })
      })
    }
    this.copyFixedBox.appendChild(this.scrollBox)

    this.animloop()
    this.copyFixedBox.onmouseenter = () => {
      cancelAnimationFrame(this.timer)
    }
    this.copyFixedBox.onmouseleave = () => {
      this.animloop()
    }
    this.copyFixedBox.onscroll = async () => {
      if (this.copyFixedBox.scrollTop >= this.scrollChildrens[0].offsetHeight - 200) {
        if (this.isPage && this.callBack && !this.isAjax) {
          this.isAjax = true
          this.callBack(() => {
            const fixedHtml = this.fixedBox.innerHTML
            this.scrollChildrens[0].innerHTML = fixedHtml
            this.scrollChildrens[1].innerHTML = fixedHtml
            this.isAjax = false
          })
        }
      }
      if (this.copyFixedBox.scrollTop >= this.scrollChildrens[0].offsetHeight) {
        this.copyFixedBox.scrollTop = 0
      }
    }
  }

  computed () {
  }

  run = () => {
    if (this.direction === 'top') {
      this.copyFixedBox.scrollTop = this.copyFixedBox.scrollTop + this.speed

    }
  }

  animloop = () => {
    this.run()
    this.timer = window.requestAnimationFrame(this.animloop)
  }
}

export default Scroll
    <div>
      <div 
        ref="listBox"
        class="list_box"
      >
        <div class="list-content">
          <div
            v-for="(item,index) in list"
            :key="index"
            class="item"
          >
            <p>
              <span>{{ index + 1 }}</span>
              <span>{{ formatData(new Date(item.date), 'YYYY年MM月DD日') || '' }}</span>
            </p>
            <p>{{ item.title }}</p>
          </div>
        </div>
      </div>
    </div>


<script> 
import Scroll from '@/utils/Scroll'
mounted(){
    new Scroll({
      el: this.$refs.listBox,
      speed:1,
    }) 
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,可以通过设置TextView的属性和使用一些代码来实现垂直自动滚动效果。 首先,我们可以使用xml布局文件或者代码动态创建一个TextView。在xml布局文件中,我们可以设置TextView的宽度和高度、文字内容等属性。 接下来,可以在代码中通过设置TextView的属性来实现垂直自动滚动。首先,我们可以使用setMaxLines方法来设置TextView的最大行数,让它只显示一行。然后,可以使用setEllipsize方法来设置TextView的省略方式为Marquee,表示文字超出一行时以滚动的形式显示。最后,再调用setSelected方法来启动滚动效果。 示例代码如下: ```java TextView textView = findViewById(R.id.text_view); textView.setMaxLines(1); textView.setEllipsize(TextUtils.TruncateAt.MARQUEE); textView.setSelected(true); ``` 这样,当TextView的文字内容超过一行时,就会自动滚动的方式显示。需要注意的是,在xml布局文件中或者代码中,需要将TextView的单行显示设置为true,并且保证TextView可获取焦点才能触发滚动效果。 除了上述方法,还可以使用属性动画或者使用Handler来实现TextView的垂直自动滚动效果。使用属性动画可以设置动画效果的速度和插值器,更加灵活。使用Handler的方式可以自定义滚动的速度和间隔时间。 总之,通过设置TextView的属性和使用一些代码,我们可以实现Android中TextView的垂直自动滚动效果,提升用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值