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}


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

相关文章
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
1066 2
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
931 137
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
2239 0
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
1126 1
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
1101 1
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
850 0
|
存储 前端开发 JavaScript
为什么我不再用Vue,改用React?
当我走进现代前端开发行业的时候,我做了一个每位开发人员都要做的决策:选择一个合适的框架。当时正逢 jQuery 被淘汰,前端开发者们不再用它编写难看的、非结构化的老式 JavaScript 程序了。
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1799 4
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
2134 78