文字滚动-走马灯的文字实现(Vue)

该博客介绍了如何使用JavaScript动态调整div宽度,并实现文字滚动效果。通过监听面包屑宽度变化,实时更新滚动区域宽度,结合CSS样式实现无缝滚动。同时,文章提供了marquee标签的替代实现方式,展示了上下滚动和左右滚动的文字效果。

JS的实现

在这里插入图片描述

实时更改div宽度

// 监听面包屑宽度,面包屑宽度改变后会自动改变文字滚动区域宽度
breadCrumbList () {
      this.$nextTick(() => {
        this.windowResize();
        window.onresize = () => {
          this.windowResize()
        }
      })
      return this.$store.state.breadCrumbList;
},

动态获取滚动文字的div宽度

在这里插入图片描述

windowResize () {
	 const headerWidth = document.getElementById('header-bar')?.offsetWidth;
	 const crumbWidth = document.getElementById('custom-bread-crumb')?.offsetWidth;
	 const conWidth = document.getElementById('custom-content-con')?.offsetWidth
	 this.titleWidth = headerWidth - (crumbWidth + conWidth) - 60;
},

实现文字滚动

setScroll () {
  let speed = 10;
  let tab = document.getElementById("notice");
  let tab1 = document.getElementById("notice1");
  let tab2 = document.getElementById("notice2");
  function Marquee () {
    if (tab2.offsetWidth - tab.scrollLeft <= 0) {
      tab.scrollLeft -= tab1.offsetWidth
    }
    else {
      tab.scrollLeft++;
    }
  }
  let MyMar = setInterval(Marquee, speed);
  tab.onmouseover = function () { clearInterval(MyMar) };
  tab.onmouseout = function () { MyMar = setInterval(Marquee, speed) };
},

html

{{content}} 显示的滚动内容

<template>
  <span class="notice-tip" :style="{width:titleWidth+'px'}">
    <div id="notice">
      <div id="innotice" :style="{color:color}">
      <!--这里是实现的另一种方式 文字上下滚动   direction="up"-->
        <!-- <marquee behavior="scroll" direction="up" :style="{color:color}" :scrollamount="5" :scrolldelay="2" onMouseOut="this.start()" onMouseOver="this.stop()">
          <p v-for="(item,index) in content.split(/。|,/)" :key="index">{{item}}</p>
        </marquee> -->
        <div id="notice1">
          {{content}}
        </div>
        <div id="notice2">
          {{content}}
        </div>
      </div>
    </div>
  </span>
</template>

css

<style scoped lang="less">
.notice-tip {
  height: 40px;
  line-height: 40px;
  display: inline-block;
  vertical-align: top;
  text-align: center;
  background-color: #fffff7;
}
#notice {
  overflow: hidden;
}

#innotice {
  float: left;
  width: 100%;
  height: 40px;
}
#notice1 {
  float: left;
}
#notice2 {
  float: left;
}

#innotice {
  margin-top: 0px;
  font-size: 0.96rem;
  margin-left: 21px;
  /deep/ p {
    text-align: center;
  }
}
</style>

marquee 标签

<marquee behavior="scroll" direction="up" :style="{color:color}" :scrollamount="5" :scrolldelay="2" onMouseOut="this.start()" onMouseOver="this.stop()">
	<p v-for="(item,index) in content.split(/。|,/)" :key="index">{{item}}</p>
</marquee>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值