*初级小白*~ 2022-04-01 13:52 采纳率: 97.9%
浏览 66
已结题

svg时间轴每隔30秒向前移动

如何每过30秒,时间轴向前移动一点

http://code.google.com/p/chromium/issues/detail?id=166164

 <svg style="width: 100%">
      <g
        id="timeScale"
        transform="translate(0,28)"
        fill="none"
        font-size="10"
        font-family="sans-serif"
        text-anchor="middle"
      >
        <path class="domain" stroke="currentColor" d="M10.5,0.5H1890.5"></path>
        <g class="tick" :class="{major: index%12==0}" opacity="1" :transform="`translate(${(1880 / 144)*index +20 },0)`" v-for="(item,index) in 144" :key="index">
          <line stroke="currentColor" :y2="index%12==0?9:6"></line>
          <text fill="#000" y="14" dy="0.71em" v-if="index%12==0">22:35</text>
        </g>
      </g>
    </svg>


  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-04-01 14:27
    关注

    img

    
    <div id="app" style="max-width:680px;margin:0 auto">
        <template>
            <div>
                <svg style="width: 100%">
                    <g id="timeScale"
                       :transform="`translate(${-translatex},28)`"
                       fill="none"
                       font-size="10"
                       font-family="sans-serif"
                       text-anchor="middle">
                        <path class="domain" stroke="currentColor" :d="`M10.5,0.5H${4890.5+moveWidth}`"></path>
                        <g class="tick"
                           :class="{ major: index % 12 == 0 }"
                           opacity="1"
                           :transform="`translate(${(1880 / count) * index + 20},0)`"
                           v-for="(item, index) in timeList"
                           :key="index"
                           ref="time">
                            <line stroke="currentColor" :y2="showText(index)? 9 : 6"></line>
                            <text fill="#000" y="14" dy="0.71em" v-if="showText(index)">
                                {{ item }}
                            </text>
                        </g>
                    </g>
                </svg>
            </div>
        </template>
    
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="http://cdn.staticfile.org/moment.js/2.24.0/moment.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data: {
                timeList: [],
                tick: 0,
                moveWidth:0,
                translatex: 0,
                count:144
            },
            created() {
                this.moveWidth = 1880 / this.count;
                this.createData();
            },
            methods: {
                showText(index) {
                    if (index >= this.tick && (index == this.tick//第一条刻度判断
                        ||
                        (
                        index - this.tick > 2//第一条刻度和下一条刻度需要大于2,下一条刻度在显示文字和长度为9的刻度线,防止文字重叠
                        && index % 12 == 0)
                    )
                    ) return true;
                    return false
                },
                animate() {
                    var timer = setInterval(() => {
                        this.translatex += 1;
                        if (this.translatex >= this.moveWidth) {
                            this.translatex = this.moveWidth;
                            clearInterval(timer);
    
                            this.tick++;
    
                            //重新获取数据
                            this.createData();
    
                        }
                    }, 50)
                },
                createData() {
                    this.timeList.splice(0, this.timeList.length)
                    let endDate = new Date();
                    let splitTime = 5 * 60 * 1000;
                    let count = this.count + this.tick;
                    //转为时间戳
                    let endTime = endDate.getTime();
                    let mod = endTime % splitTime;
    
                    if (mod > 0) {
                        endTime = endTime - mod;
                    }
                    endTime += this.tick * splitTime;
                    while (count-- > 0) {
                        let d = new Date();
                        d.setTime(endTime - count * splitTime);
                        this.timeList.push(moment(d).format("HH:mm"));
                    }
                    this.moveWidth += 1880 / this.count
       
                    setTimeout(() => {
                        this.animate();
                    }, 1000);//移动坐标轴,这里改为30s获取一次数据,测试1s一次
                }
            }
        })
    </script>
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月1日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测