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}


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

相关文章
|
1天前
|
JavaScript
vue打印v-model 的值
vue打印v-model 的值
|
1天前
|
JavaScript
vue知识点
vue知识点
4 1
|
1天前
|
JavaScript
Vue实战-组件通信
Vue实战-组件通信
4 0
|
1天前
|
JavaScript
Vue实战-将通用组件注册为全局组件
Vue实战-将通用组件注册为全局组件
5 0
|
2天前
|
JavaScript 前端开发
vue的论坛管理模块-文章评论02
vue的论坛管理模块-文章评论02
|
JavaScript 测试技术 容器
Vue2+VueRouter2+webpack 构建项目
1). 安装Node环境和npm包管理工具 检测版本 node -v npm -v 图1.png 2). 安装vue-cli(vue脚手架) npm install -g vue-cli --registry=https://registry.
987 0
|
3天前
|
存储 JavaScript
Vue当前时间与接口返回时间的判断
Vue当前时间与接口返回时间的判断
8 0
|
3天前
|
JavaScript 前端开发
Vue生成Canvas二维码
Vue生成Canvas二维码
7 0
|
2天前
|
JavaScript Java
vue的论坛管理模块-文章查看-01
vue的论坛管理模块-文章查看-01
|
2天前
|
JavaScript
vue页面加载时同时请求两个接口
vue页面加载时同时请求两个接口