使用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>
总结
到这里这个代码基本上是能用的。
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Vue.js 来手一个 PC 端的轮播组件。下面是一个简单的示例代码: ```html <template> <div class="carousel"> <div class="carousel-inner"> <div class="carousel-item" v-for="(item, index) in items" :key="index"> <img :src="item.imgUrl" alt="carousel-item"> </div> </div> <button class="carousel-prev" @click="prevItem"><</button> <button class="carousel-next" @click="nextItem">></button> </div> </template> <script> export default { data() { return { items: [ { imgUrl: 'image1.jpg' }, { imgUrl: 'image2.jpg' }, { imgUrl: 'image3.jpg' }, ], currentItemIndex: 0, }; }, methods: { prevItem() { if (this.currentItemIndex > 0) { this.currentItemIndex--; } else { this.currentItemIndex = this.items.length - 1; } }, nextItem() { if (this.currentItemIndex < this.items.length - 1) { this.currentItemIndex++; } else { this.currentItemIndex = 0; } }, }, }; </script> <style scoped> .carousel { position: relative; } .carousel-inner { display: flex; overflow: hidden; } .carousel-item { flex: 0 0 100%; } .carousel-item img { width: 100%; } .carousel-prev, .carousel-next { position: absolute; top: 50%; transform: translate(0, -50%); padding: 10px; } .carousel-prev { left: 10px; } .carousel-next { right: 10px; } </style> ``` 上述代码中,使用了一个数组 `items` 存储轮播项的信息,每个项包含一个图片的 URL 地址。`currentItemIndex` 用于追踪当前显示的轮播项的索引。通过点击“上一项”和“下一项”按钮,可以切换轮播项。 你可以根据实际需求进行样式和功能的定制。希望这个示例对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值