跑马灯 Vue

1664074923343

Html

<body>
    <div id="app">
        <p>{{str}}</p>
        <button @click="play()">飘</button>
        <button @click="stop()">定</button>
    </div>
</body>

vue.js

<script src="js/vue.min.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                str: "大风起兮云飞扬",
                timer: null
            }
        },
        //Vue创建完毕,自动会自行play
        // created() {
        //     this.play()
        // },
        methods: {
            play() {
                clearInterval(this.timer);
                //停止动画
                this.timer = setInterval(() => {
                    //字符串转数组
                    var temp = this.str.split('');
                    //获取第一个字符串
                    var first = temp.shift();
                    //添加到最后
                    temp.push(first);
                    //转换为字符串。赋值个str
                    this.str = temp.join('')
                }, 50);
            },
            stop() {
                clearInterval(this.timer)
            }
        }

    })
</script>

全部代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <p>{{str}}</p>
        <button @click="play()">飘</button>
        <button @click="stop()">定</button>
    </div>
</body>
<script src="js/vue.min.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                str: "大风起兮云飞扬",
                timer: null
            }
        },
        //Vue创建完毕,自动会自行play
        // created() {
        //     this.play()
        // },
        methods: {
            play() {
                clearInterval(this.timer);
                //停止动画
                this.timer = setInterval(() => {
                    //字符串转数组
                    var temp = this.str.split('');
                    //获取第一个字符串
                    var first = temp.shift();
                    //添加到最后
                    temp.push(first);
                    //转换为字符串。赋值个str
                    this.str = temp.join('')
                }, 50);
            },
            stop() {
                clearInterval(this.timer)
            }
        }

    })
</script>

</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是九宫格抽奖带马灯Vue 代码实现: Template 部分: ``` <template> <div class="container"> <div class="grid"> <div class="cell" v-for="(item, index) in items" :key="index"> <div class="content" :class="{active: item.active}" @click="handleClick(index)"> <span class="title">{{item.title}}</span> <img :src="item.img" alt="" class="img"> </div> </div> </div> <div class="marquee"> <ul ref="marquee"> <li v-for="item in marqueeItems" :key="item">{{item}}</li> </ul> </div> </div> </template> ``` Script 部分: ``` <script> export default { data() { return { items: [ {title: '奖品1', img: 'img1.jpg', active: false}, {title: '奖品2', img: 'img2.jpg', active: false}, {title: '奖品3', img: 'img3.jpg', active: false}, {title: '奖品4', img: 'img4.jpg', active: false}, {title: '奖品5', img: 'img5.jpg', active: false}, {title: '奖品6', img: 'img6.jpg', active: false}, {title: '奖品7', img: 'img7.jpg', active: false}, {title: '奖品8', img: 'img8.jpg', active: false}, {title: '奖品9', img: 'img9.jpg', active: false} ], marqueeItems: ['恭喜 xxx 获得奖品1', '恭喜 yyy 获得奖品2', '恭喜 zzz 获得奖品3'], activeIndex: -1, isRunning: false, timer: null } }, methods: { handleClick(index) { if (this.isRunning) return this.activeIndex = index this.isRunning = true this.timer = setInterval(() => { this.items[this.activeIndex].active = false this.activeIndex = (this.activeIndex + 1) % 9 this.items[this.activeIndex].active = true }, 100) setTimeout(() => { clearInterval(this.timer) this.isRunning = false this.items[this.activeIndex].active = false // TODO: 判断中奖情况 }, 3000) } } } </script> ``` Style 部分: ``` <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 500px; } .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; } .cell { position: relative; border: 1px solid #ccc; border-radius: 5px; overflow: hidden; } .content { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 100%; height: 100%; cursor: pointer; transition: all 0.3s ease; } .content.active { background-color: #f00; color: #fff; } .title { font-size: 24px; margin-bottom: 10px; } .img { width: 80%; height: auto; } .marquee { margin-top: 20px; } ul { position: relative; list-style: none; padding: 0; margin: 0; } li { display: inline-block; margin-right: 20px; white-space: nowrap; animation: marquee 10s linear infinite; } @keyframes marquee { from { left: 100%; } to { left: -100%; } } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值