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中的class和style绑定
在 Vue 中,class 和 style 绑定是基于数据驱动视图的强大功能。通过 class 绑定,可以动态更新元素的 class 属性,支持对象和数组语法,适用于普通元素和组件。style 绑定则允许以对象或数组形式动态设置内联样式,Vue 会根据数据变化自动更新 DOM。
|
8天前
|
移动开发 JavaScript API
Vue Router 核心原理
Vue Router 是 Vue.js 的官方路由管理器,用于实现单页面应用(SPA)的路由功能。其核心原理包括路由配置、监听浏览器事件和组件渲染等。通过定义路径与组件的映射关系,Vue Router 将用户访问的路径与对应的组件关联,支持哈希和历史模式监听 URL 变化,确保页面导航时正确渲染组件。
|
8天前
|
JavaScript 前端开发 数据安全/隐私保护
Vue Router 简介
Vue Router 是 Vue.js 官方的路由管理库,用于构建单页面应用(SPA)。它将不同页面映射到对应组件,支持嵌套路由、路由参数和导航守卫等功能,简化复杂前端应用的开发。主要特性包括路由映射、嵌套路由、路由参数、导航守卫和路由懒加载,提升性能和开发效率。安装命令:`npm install vue-router`。
|
12天前
|
监控 JavaScript 前端开发
ry-vue-flowable-xg:震撼来袭!这款基于 Vue 和 Flowable 的企业级工程项目管理项目,你绝不能错过
基于 Vue 和 Flowable 的企业级工程项目管理平台,免费开源且高度定制化。它覆盖投标管理、进度控制、财务核算等全流程需求,提供流程设计、部署、监控和任务管理等功能,适用于企业办公、生产制造、金融服务等多个场景,助力企业提升效率与竞争力。
66 12
|
29天前
|
JavaScript 安全 API
iframe嵌入页面实现免登录思路(以vue为例)
通过上述步骤,可以在Vue.js项目中通过 `iframe`实现不同应用间的免登录功能。利用Token传递和消息传递机制,可以确保安全、高效地在主应用和子应用间共享登录状态。这种方法在实际项目中具有广泛的应用前景,能够显著提升用户体验。
60 8
|
30天前
|
存储 设计模式 JavaScript
Vue 组件化开发:构建高质量应用的核心
本文深入探讨了 Vue.js 组件化开发的核心概念与最佳实践。
77 1
|
2月前
|
JavaScript
vue使用iconfont图标
vue使用iconfont图标
151 1
|
2月前
|
JavaScript 关系型数据库 MySQL
基于VUE的校园二手交易平台系统设计与实现毕业设计论文模板
基于Vue的校园二手交易平台是一款专为校园用户设计的在线交易系统,提供简洁高效、安全可靠的二手商品买卖环境。平台利用Vue框架的响应式数据绑定和组件化特性,实现用户友好的界面,方便商品浏览、发布与管理。该系统采用Node.js、MySQL及B/S架构,确保稳定性和多功能模块设计,涵盖管理员和用户功能模块,促进物品循环使用,降低开销,提升环保意识,助力绿色校园文化建设。
|
3月前
|
JavaScript API 开发者
Vue是如何进行组件化的
Vue是如何进行组件化的
|
3月前
|
JavaScript 前端开发 开发者
Vue是如何进行组件化的
Vue是如何进行组件化的

热门文章

最新文章