用vue怎样编写一个能控制跑马灯的组件
Admin 2022-09-08 群英技术资讯 491 次浏览
用vue编写一个可以快进后退的跑马灯组件
由于业务需求,要实现一个会可以控制速度的跑马灯,初开始用js的setinterval每隔几毫秒来减取一个字符拼接到后面,效果不理想就放弃了。后查询用js的animate这个api改造大功告成!
效果图
组件代码
<template>
<div class="marquee" @mouseover="pause(true)" @mouseleave="pause()">
<i
class="marquee-btn btn-left el-icon-d-arrow-left"
@mousedown="speedUp(true)"
@mouseup="speedStop()"
></i>
<div ref="marqueeText" class="marquee-text">
<div v-if="itemClick">
<span
v-for="item in text.split(splitSymbol)"
:key="item"
@click="$emit('itemClickEvent', item)"
>{{item + ' 、'}}</span>
</div>
<div v-else>{{text}}</div>
</div>
<i
class="marquee-btn btn-right el-icon-d-arrow-right"
@mousedown="speedUp()"
@mouseup="speedStop()"
></i>
</div>
</template>
<script>
export default {
name: "marquee",
props: {
text: {
type: String,
required: true
},
speed: {
type: Number,
required: false,
default: 110
},
// 是否每个都可以点击触发事件
itemClick: {
type: Boolean,
required: false,
default: false
},
// 每个触发事件元素的分割符号
splitSymbol: {
type: String,
required: false,
default: ''
}
},
data() {
return {
aniInstance: null,
speedTimer: null
};
},
methods: {
setAnimate() {
const contentWidth = this.$refs.marqueeText.scrollWidth;
const keyframes = [
{ transform: "translateX(100%)" },
{ transform: `translateX(-${contentWidth}px)` }
];
const animateOptions = {
duration: (contentWidth / this.speed) * 1000,
iterations: Infinity,
easing: "linear"
};
this.aniInstance = document.querySelector(".marquee-text").animate(keyframes, animateOptions);
},
/**
* 快进
* @param { Boolean } isLeft 是否为左方向
*/
speedUp(isLeft = false) {
const set = () => {
if (this.aniInstance.currentTime > 0) {
this.aniInstance.currentTime = this.aniInstance.currentTime + (isLeft ? 2000 : -2000);
this.aniInstance.currentTime <= 0 && (this.aniInstance.currentTime = 0);
}
}
// 鼠标单击
set();
// 鼠标长按
this.speedTimer = setInterval(() => {
set()
}, 100);
},
// 快进停止
speedStop() {
clearInterval(this.speedTimer);
this.speedTimer = null;
},
/**
* 暂停、播放
* @param { Boolean } isPause 是否暂停
*/
pause(isPause = false) {
this.aniInstance[["play", "pause"][Number(isPause)]]();
}
},
mounted() {
this.$nextTick(() => {
this.setAnimate();
});
}
};
</script>
<style lang="less" scoped>
.marquee {
position: relative;
padding: 10px 0;
overflow: hidden;
width: 100%;
font-size: 16px;
color: #fff;
background-image: linear-gradient(
to left,
#b9565e,
#cb655a,
#da7655,
#e58a50,
#eb9f4b
);
&:hover .marquee-btn {
opacity: 1;
}
}
.marquee-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
padding: 15px;
color: #fff;
background: rgba(1, 1, 1, 0.4);
z-index: 999;
cursor: pointer;
opacity: 0;
transition: all 0.2s linear;
}
.btn-left {
left: 0;
}
.btn-right {
right: 0;
}
.marquee-text {
white-space: nowrap;
span {
&:hover {
cursor: pointer;
color: #2c3e50;
}
}
}
</style>
父组件代码
<Marquee
:text="overdueInfo.content"
:itemClick="true"
:speed="120"
splitSymbol="、"
@itemClickEvent="marqueeSearch"
class="marquee-box__container"
></Marquee>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了vue+ts实现元素鼠标拖动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
typeof操作符返回字符串,表示未计算操作数的类型。typeof一般用来检验简单的数据类型,返回的基本类型用字符串表示,而复杂的数据类型中function返回的是Function,其他的都返回Object,其中null特殊,表示一个空对象。
这篇文章主要给大家分享jquery实现记分进度条效果的内容,感兴趣的朋友可以借鉴参考一下,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
用jQuery怎样实现隐藏表的列元素?隐藏表的列元素还是比较实用的,本文实现的需求是隐藏表格里的某一列,这里使用到的方法是hide(),实现步骤及代码如下,需要的朋友可以参考,接下来跟随小编一起来学习一下吧。
JavaScript属性操作,下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008