首页 > 其他分享 >区块-自动自下向上移动(字幕滚动)

区块-自动自下向上移动(字幕滚动)

时间:2023-11-10 16:12:16浏览次数:36  
标签:outer name refs 字幕 scrollBox let 自下 滚动 区块

示例图

引入组件

import autoScroll from './components/autoMove.vue'

 页面使用组件

<div class="pro-body">
   <autoScroll  :list="list"
                :speed="0.5"
                 :waitTime="2000"
                 :singleHeight="100"
                  >
       <!-- 移动的主体块 可替换成别的 -->
          <div class="t-item">
              <div class="t-value">示例样式</div>
     <div class="t-value">示例样式</div>
             <div class="t-value">示例样式</div>
             <div class="t-value">示例样式</div>
             <div class="t-value">示例样式</div>
             <div class="t-value">示例样式</div>
          </div> 
  </autoScroll> 
</div>


// 部分样式
<style scoped lang="scss">
.pro-body {
  height: 80%;

  .t-item {
    width: 100%;
  }

  .t-value {
    text-align: center;
    height: 2.5rem;
    line-height: 2.5rem;
    background: linear-gradient(
      90deg,
      rgba(129, 228, 251, 0) 0%,
      rgb(113 219 249 / 0.2) 15%,
      rgb(8 144 255 / 0.2) 52%,
      rgb(74 197 245 / 0.2) 83%,
      rgba(74, 197, 245, 0) 100%
    );
    border-radius: 3px;
    margin-top: 0.4rem;
    color: #133554;
    font-size: 1.1rem;
    font-weight: 400;
    overflow: hidden;
  }
}
</style>        

组件代码

<template>
    <div class="scroll-outer" ref="outer" @mouseover="onMouseover" @mouseleave="onMouseleave">
      <div class="scroll-inner-box" ref="scrollBox">
        <div class="scroll-item-box" ref="scrollItemBox">
          <slot></slot>
        </div>
        <div v-if="showSecond" class="scroll-item-box">
          <slot></slot>
        </div>
      </div>
    </div>
  </template>
    <script>
  export default {
    name: "my-auto-scroll",
    props: {
      list: {
        type: Array,
        // default: () => [
        //   { name: "张三1" },
        //   { name: "张三2" },
        //   { name: "张三3" },
        //   { name: "张三4" },
        //   { name: "张三5" },
        //   { name: "张三6" },
        //   { name: "张三7" },
        //   { name: "张三8" },
        //   { name: "张三9" },
        //   { name: "张三10" },
        // ],
      },
      speed: {
        type: Number,
        default: 0.1,
      },
      //滚动作单步运动时的单纯运动距离
      singleHeight: {
        type: Number,
        default: 0,
      },
      //单步运动的时间间隔
      waitTime: {
        type: Number,
        default: 0,
      },
    },
    data() {
      return {
        rafId: null,
        y: 0,
        showSecond: false,
        controleHeight: 0,
      }
    },
    watch: {
      list: {
        handler(newVal) {
          var that = this;
          this.$nextTick(() => {
            // console.log(newVal);
            if (newVal && newVal.length > 0) {
              let scrollBox = that.$refs.scrollBox;
              let outer = that.$refs.outer;
   
              if (this.myReq) {
                cancelAnimationFrame(this.myReq);
              }
              // 开启动画
              if (this.canRun()) this.reqAnimationFrame();
              // this.reqAnimationFrame();
              // 手动滚动到底部时滚动条重置到最上边,同时滚动盒子重置为top:0
              outer.addEventListener("scroll", function () {
                if (
                  outer.scrollTop + outer.clientHeight + 4 >=
                  outer.scrollHeight
                ) {
                  outer.scrollTop = 0;
                  that.y = 0;
                  scrollBox.style.top = 0;
                }
              })
            }
          })
        },
        deep: true,
        immediate: true,
      }
    },
    mounted() {
    //   window.addEventListener("resize", this.listenResizeFn);
    },
    methods: {
      listenResizeFn() {
        cancelAnimationFrame(this.myReq);
        if (this.canRun()) this.reqAnimationFrame();
      },
      onm ouseover() {
        clearTimeout(this.timer);
        cancelAnimationFrame(this.myReq);
      },
      onm ouseleave() {
        if (this.canRun()) this.reqAnimationFrame();
      },
      canRun() {
        let scrollItemBox = this.$refs.scrollItemBox;
        let scrollBox = this.$refs.scrollBox;
        let outer = this.$refs.outer;
        // 开启动画条件:滚动盒子(scrollBox)高度高于外层容器(outer)高度
        if (outer.offsetHeight >= scrollItemBox.offsetHeight) {
          this.showSecond = false;
          outer.scrollTop = 0;
          this.y = 0;
          scrollBox.style.top = 0;
          return false;
        } else {
          this.showSecond = true;
          return true;
        }
      },
      //获取dom元素的高度:content+padding+margin+border
      getComputedHeight(dom) {
        let computedStyle = getComputedStyle(dom);
   
        let computedHeight =
          dom.offsetHeight +
          parseFloat(computedStyle.marginTop) +
          parseFloat(computedStyle.marginBottom);
        return computedHeight;
      },
      reqAnimationFrame() {
        //外层容器
        let outer = this.$refs.outer;
        //滚动盒子
        let scrollBox = this.$refs.scrollBox;
        //滚动盒子下边的第一个scroll-item-box,
        let scrollItemBox = this.$refs.scrollItemBox;
   
        //滚动速度
        this.speed = this.speed > 1 ? 1 : this.speed < 0 ? 0.1 : this.speed;
   
        //取第一个scrollItemBox高度
        let definedHeight = this.getComputedHeight(scrollItemBox);
        //持续滚动
        this.y = this.y + this.speed;
        scrollBox.style.top = -this.y + "px";
   
        //====添加滚动间隔控制====开始
        if (this.singleHeight >= 20 && this.waitTime > 500) {
          if (this.controleHeight >= this.singleHeight) {
            cancelAnimationFrame(this.myReq);
            this.controleHeight = 0;
            this.timer = setTimeout(() => {
              if (this.canRun) this.reqAnimationFrame();
            }, this.waitTime);
            return;
          } else {
            // 一次移动高度未达到指定距离继续执行动画
            this.controleHeight += this.speed;
          }
        }
        //====添加滚动间隔控制====结束
   
        //当滚动到第一个scroll-item-box高度时scrollBox重置为top:0,视觉上是无缝滚动
        if (this.y >= definedHeight) {
          this.y = 0;
        }
        this.myReq = window.requestAnimationFrame(this.reqAnimationFrame);
      },
    },
    destroyed() {
      window.removeEventListener("resize", this.listenResizeFn);
      cancelAnimationFrame(this.myReq);
      if (this.timer) clearTimeout(this.timer);
    },
  }
  </script>
    <style lang="scss">
  .scroll-outer {
    height: 100%;
    overflow-x: hidden;
    position: relative;
    &::-webkit-scrollbar {
      width: 0.3vw;
    }
    &:hover::-webkit-scrollbar-track {
      -webkit-box-shadow: inset 0 0 0.1vw rgba(0, 0, 0, 0.3);
      border-radius: 0.1vw;
      background-color: #295099;
      opacity: 1;
      // display: none;
    }
    &:hover::-webkit-scrollbar-thumb {
      opacity: 1;
      border-radius: 0.1vw;
      -webkit-box-shadow: inset 0 0 0.1vw rgba(0, 0, 0, 0.3);
      background-color: #0ba9ea;
    }
  }
  .scroll-inner-box {
    height: auto;
    position: absolute;
    width: 100%;
    top: 0;
    left: 0;
  }
  </style>
    

 

标签:outer,name,refs,字幕,scrollBox,let,自下,滚动,区块
From: https://www.cnblogs.com/qiuqiu001/p/17824298.html

相关文章

  • 区块链技术在跑腿服务中的应用与App系统开发
    区块链技术为跑腿服务App系统带来了新的可能性,如支付安全、合同自动化、透明性等。本文将介绍如何在一个简单的跑腿服务App系统中应用区块链技术。智能合约智能合约是区块链的核心概念之一。它是在区块链上运行的自动化合同,可以自动执行、管理和验证合同条款。Solidity语言示例以......
  • 区块链导论:数字经济需要培养多学科交叉综合人才
    日前,在第三届“一带一路”国际合作高峰论坛数字经济高级别论坛上,香港科技大学校长叶玉如提出了一个引人深思的观点:“数字经济是多个学科领域交叉融合,我们需要的人才既要懂得经济学,也要理解人工智能、大数据、区块链等前沿科技。传统的学科设计已不能满足数字经济发展的需求。大学需......
  • 区块链平台终极对决:Solana Vs. Polygon Vs. Ethereum
    区块链技术是目前世界上最受关注的技术之一。它几乎进入了每一个领域,以其中心化的系统来改善现有技术。随着区块链的进入,也为这些细分领域开发了许多不同种类的应用。它还催生了诸如NFT、去中心化金融(Defi)、加密货币等很多东西。原文:https://www.blockchain-council.org/b......
  • Qt开发实现字幕滚动效果
    1、效果展示我们经常能够在外面看到那种滚动字幕,那么就拿qt来做一个吧。2、实现思路实现一个窗口部件,这个窗口部件显示了一串文本标语,它会每t毫秒向左移动一个像素。如果窗口部件比文本宽,那么文本将会被多次重复,直到能够填满整个窗口部件的宽度为止。3、滚动窗口部件创建一个滚......
  • 给视频添加字幕
    给视频添加字幕加字幕Arctimehttps://arctime.org/调轴工具,修改文本https://subplayer.js.org/......
  • 系统集成易混淆知识点汇总-参数估算、类比估算、三点估算、自下而上估算
    概念:(1)参数估算:参数估算是一种基于历史数据和项目参数,使用某种算法来计算成本或工期的估算技术。利用历时数据之间的统计关系和其他变量,来估算诸如成本、预算和持续时间等活动参数。(2)类比估算:类比估算是指以过去类似项目的参数值或规模指标为基础,来估算当前项目的同类参数或指标......
  • 在以太坊区块链上添加一个区块
    包括json库的相关读取,proof-of-work算法的实现,MerkelTree的构建,使用hash创建新块等内容,使用本地json文件模拟mempool和blockchain,C++编写。#include<iostream>#include<fstream>#include<string>#include<nlohmann/json.hpp>#include<zlib.h>#include<openssl/......
  • 区块链技术软件开发师:打造区块链应用的专家,掌握开发实战技能
    区块链技术软件开发师:打造区块链应用的专家,掌握开发实战技能 专业技能:一、编程语言方面C/C++(必须熟悉C++语言),Golang(必须熟悉GO语言)、Python、Java、Solidity,能独立开发Chaincode熟练掌握golang的goroutine,  channel,gRPC等技术可以额外学习一些前端技术,HTML5、DIV、CSS、J......
  • 从0到1,全面学透区块链:掌握区块链的基础理论和技术
    从0到1,全面学透区块链:掌握区块链的基础理论和技术1、简介​区块链是一个又一个区块组成的链条。每一个区块中保存了一定的信息,它们按照各自产生的时间顺序连接成链条。这个链条被保存在所有的服务器中,只要整个系统中有一台服务器可以工作,整条区块链就是安全的。这些服务器在......
  • 从零构建以太坊智能合约到项目实战:掌握区块链编程的精髓 成为区块链编程大师
    从零构建以太坊智能合约到项目实战:掌握区块链编程的精髓成为区块链编程大师为什么说现在学习区块链才是最好的时机?区块链技术不只是能开发数字货币,不只是能进行ICO。当我分享一些区块链文章的时候,偶尔总会有人艾特我说,春哥,现在国家都不让炒币了,还弄个毛的区块链啊。我一般会......