消息滚动切换

CSS代码

<style>
      .tip-bar {
        display: inline-flex;
        align-items: center;
        height: 30px;
        background-color: rgba(0, 0, 0, 0.4);
        border-radius: 16px;
      }
      img {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        margin-right: 5px;
      }
      span {
        font-size: 13px;
        color: white;
        margin-right: 8px;
      }
    </style>

HTML代码

<div class="tip-bar">
      <img
        src="https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png"
        alt=""
      />
      <span>183***138对这件商品感兴趣</span>
    </div>

JS代码

<script>
      // 1.从服务器拿到数据ajax/fetch请求
      let tipList = [
        {
          icon: "https://bfs.biyao.com/group1/M01/A6/97/rBACYWBCHqyAFH5tAAANZXX5Eww646.png",
          title: "coderwhy对这件商品感兴趣",
        },
        {
          icon: "https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png",
          title: "123***814对这件商品感兴趣",
        },
        {
          icon: "https://bfs.biyao.com/group1/M00/7F/4E/rBACYV16HseAP-PnAAAW9bbVoKE463.png",
          title: "刘军对这件商品感兴趣",
        },
      ];

      // 2.动态的切换数据
      // 2.1. 获取元素
      var tipBar = document.querySelector(".tip-bar");
      var imgEl = tipBar.querySelector("img");
      var spanEl = tipBar.querySelector("span");

      // 2.1. 3s切换一次数据
      var currentIndex = 0; //记录当前展示到的索引位置
      setInterval(function () {
        // 1> 根据索引获取item
        var tipItem = tipList[currentIndex];

        // 2>给DOM设置内容
        imgEl.src = tipItem.icon;
        spanEl.textContent = tipItem.title;

        // 3>重新计算索引
        currentIndex++;
        if (currentIndex === tipList.length) {
          currentIndex = 0;
        }
      }, 3000);

      // 随机滚动,只写了随机值
      Math.floor(Math.random() * tipList.length);
    </script>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值