vue实现公告文字横向滚动

简介: vue实现公告文字横向滚动

1.

可以通过改变元素的marginLeft值和计时器来实现滚动

代码实现如下:

<template><divid="box"ref="box"><divclass="marquee-box"ref="marquee"@mouseover="menter"@mouseleave="mleave"><pref="cmdlist"id="pWidth"><imgstyle="width: 12px;height: 12px"src="../assets/logo.png"alt="">这是一条公告这是一条公告这是一条公告</p></div></div></template>
exportdefault {
name: 'HelloWorld',
data () {
return {
value: 0,
timer: '',//计时器pwidth:0,//公告文本的宽度windowWidth:document.documentElement.clientWidth,// 设备屏幕的宽度      }
    },
mounted() {
letelement=this.$refs.cmdlist;
this.pwidth=document.defaultView.getComputedStyle(element,'').width.split('px');
this.timer=setInterval(this.clickCommend, 20);
  },
watch:{
value(newValue, oldValue) {
letallWidth=parseInt(this.windowWidth)+parseInt(this.pwidth[0]);
if(newValue<=-allWidth){
this.$refs.cmdlist.style.marginLeft=this.windowWidth+"px";
this.value=0;
      }
    },
  },
methods:{
clickCommend(e) {
let_this=this;
this.$nextTick(() => {
this.value-=1;
this.$refs.cmdlist.style.marginLeft=_this.windowWidth+this.value+"px";
      });
    },
menter(){
clearInterval(this.timer);
    },
mleave(){
this.timer=setInterval(this.clickCommend, 20);
    },
  },
beforeDestroy() {
clearInterval(this.timer);
  }
}

css代码:

<stylescoped>#box {
width: 100%;
height: 50px;
margin-top: 50px;
position: relative;
}
.marquee-box  {
position: relative;
width: 100%;
height: 100%;
overflow:auto;
background-color: #f8f8f8;
}
#pWidth{
width: 100%;
height: 50px;
padding: 0;
margin: 0;
line-height: 50px;
display: block;
word-break: keep-all;
white-space: nowrap;
overflow:hidden;
font-family: 微软雅黑; fontSize:14px;
background-color: #f8f8f8}
::-webkit-scrollbar {
width: 0!important;
}
::-webkit-scrollbar {
width: 0!important;height: 0;
}
</style>

2.也可以通过改变元素的left值。计时器来实现滚动

代码如下只需修改css和js代码

mounted() {
// let element = this.$refs.cmdlist;// this.pwidth = document.defaultView.getComputedStyle(element,'').width.split('px');this.timer=setInterval(this.clickCommend, 20);
  },
watch:{
value(newValue, oldValue) {
// let allWidth= parseInt(this.windowWidth)+parseInt(this.pwidth[0]);letallWidth=parseInt(this.windowWidth)+document.getElementById('pWidth').offsetWidth;
if(newValue<=-allWidth){
// this.$refs.cmdlist.style.marginLeft = this.windowWidth+"px";this.$refs.cmdlist.style.left="100%";
this.value=0;
      }
    },
  },
methods:{
clickCommend(e) {
let_this=this;
this.$nextTick(() => {
this.value-=1;
// this.$refs.cmdlist.style.marginLeft = _this.windowWidth+this.value+"px";this.$refs.cmdlist.style.left=_this.windowWidth+this.value+"px";
      });
    },
    ... ...
  },
.marquee-box{
position: relative;
width: 100%;
height: 50px;
background-color: white;
overflow: auto;
background-color: #f8f8f8;
}
#pWidth{
margin-top: 0px!important;
margin-bottom: 0px!important;
position: absolute; /*  作了修改*/top: 0;      /*  作了修改*/left: 100%; /* 作了修改*/line-height: 50px;
display: block;
word-break: keep-all;
text-overflow: ellipsis;
white-space: nowrap;
overflow:hidden;
font-family: 微软雅黑; fontSize:14px;
background-color: #f8f8f8}


其实无论是横向滚动还是上下滚动,都是使用计时器和改变元素的位置实现滚动的。然后就能实现了

相关文章
|
8月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
731 2
|
7月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
622 137
|
11月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
1168 0
|
10月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
744 1
|
10月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
523 0
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1458 4
|
11月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
12月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
1680 78
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
11月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
728 17