使用vue 写 轮播文字通告组件

编程入门 行业动态 更新时间:2024-10-09 04:17:29

使用vue 写 轮播文字通告<a href=https://www.elefans.com/category/jswz/34/1771375.html style=组件"/>

使用vue 写 轮播文字通告组件

使用vue 写 轮播文字通告组件

我们在使用 电商网站 经常会看到 轮播文字的通告。今天我记录一下我使用vue2.0版本来写这个轮播组件,先贴一波代码
style
技术栈 less.scroll{height: 32px;line-height: 28px;padding: 4px 0;white-space: nowrap;.content{width: 100%;word-wrap: normal;margin-right: 140px;}.title{color: #ad6f0c;font-weight: bold;}.text {color: #805518;}
}
.textScroll{background-color: #f7f7f7;border-bottom: 4px solid #999;position: fixed;bottom: 0;left: 0;width: 100%;
}

html 代码

    <div class="textScroll" @mousemove="pauseAn" @mouseout="startAn" ><div class="scroll" :style="{marginLeft: '-' + marginLeft + 'px' }"> <span @click="itemClick(item,$event)" v-for="(item,index) in datas" :key="index" class="content"><span class="title">【特推公告:{{item.title}}】</span><span class="text">{{item.text}}</span></span> </div>
</div>

js 代码

<script>export default {
name:'textScroll',
data(){return {datas,marginLeft: 0,prevLeft: 0,an: '',place: '',}
},
props:{data: {type: Array,},time: {type: Number,default: 100,},placement: {type: String,default: 'bottom'}
},
created(){switch( this.placement ){ /** 此功能可自己扩展,展示方式,此代码中未完成扩展 **/case 'top': this.place = 'top';break;case 'bottom': this.place = 'bottom';break;default : this.place = 'bottom';break;}
},
mounted(){this.$nextTick (function(){this.startAn();})},
beforeDestroy(){this.stopAn();
},
watch:{},
methods:{startAn: function(){ // 开始 let _this = this;let width = document.querySelector('.scroll').offsetWidth;this.an = setInterval( function() {if (_this.marginLeft > width) {_this.marginLeft = 0;}_this.marginLeft += 2;} , _this.time);},stopAn: function(){ // 停止this.prevLeft = this.marginLeft;this.marginLeft = 0;clearInterval(this.an);this.$emit('on-stop-An');},pauseAn: function(){ // 暂停动画clearInterval(this.an);},itemClick: function( item, e ) {this.$emit('on-item-click',item );}}}
</script>
总结
到这里这个代码基本上是能用的。

更多推荐

使用vue 写 轮播文字通告组件

本文发布于:2024-02-13 16:14:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   通告   文字   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!