css 实现跑马灯/走马灯无缝衔接 js实现无缝滚动图片

先上效果图

首先呈上效果图,主要是针对图片跑马灯效果(请忽略我的快速播放和录屏图标)
在这里插入图片描述

具体实现方式

目前我需要展示6张图片,但是为了实现无缝衔接,我的列表放了12张(放2遍图片),这样就不至于在切换时出现那么几毫秒的跳动。

<div id="swiper-roll">
      <div class="roll-wrapper">
          <ul>
              <li><img src="./static/img/firm1.png"></li>
              <li><img src="./static/img/firm2.png"></li>
              <li><img src="./static/img/firm3.png"></li>
              <li><img src="./static/img/firm4.png"></li>
              <li><img src="./static/img/firm5.png"></li>
              <li><img src="./static/img/firm1.png"></li>
              <li><img src="./static/img/firm2.png"></li>
              <li><img src="./static/img/firm3.png"></li>
              <li><img src="./static/img/firm4.png"></li>
              <li><img src="./static/img/firm5.png"></li>
          </ul>
      </div>
  </div>

//js部分
/**
               * 无缝滚动,可通过以下两种方式调节滚动速度:
               * 1、可通过设置iSpeed 大小调节单次滚动速度;
               * 2、可通过调节timer 定时器中最后一个参数(60)的大小来控制滚动的频率,同时达到调节速度的作用;
               */
window.onload = function(){
       let oDiv=document.getElementById('swiper-roll');
       let oUl=oDiv.getElementsByTagName('ul')[0];
       let aLi=oUl.getElementsByTagName("li");
       let iSpeed=-4;
       let timer=null;
       oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
       timer=setInterval(function(){
           oUl.style.left=oUl.offsetLeft+iSpeed+'px';
           if(oUl.offsetLeft <= -oUl.offsetWidth/2){
               oUl.style.left='0px';
           }
       },60);

       oUl.onmouseover=function(){
           clearInterval(timer);
       }
       oUl.onmouseout=function(){
           timer=setInterval(function(){
               oUl.style.left=oUl.offsetLeft+iSpeed+'px';
               if(oUl.offsetLeft <= -oUl.offsetWidth/2){
                   oUl.style.left='0px';
               }
           },60);
       }
   };

//css
#swiper-roll .roll-wrapper{
  width:100%;
  overflow: hidden;
  height: 130px;
  margin: 0 auto;
  position: relative;
}
.roll-wrapper li{
  float: left;
  list-style: none;
  width: 150px;
  height: 130px;
}
.roll-wrapper ul{
  position: absolute;
  top: 0;
  left: 0;
}

注意:1、具体图片,各位宝宝请自行找图片哟。

实现CSS文字跑马灯无缝衔接,可以使用CSS3的动画特性和伪元素来实现。以下是一个示例代码: HTML代码: ``` <div class="marquee"> <span>This is a demo of CSS Marquee</span> </div> ``` CSS代码: ``` .marquee { position: relative; overflow: hidden; white-space: nowrap; } .marquee span { position: absolute; left: 100%; display: inline-block; animation: marquee 10s linear infinite; } @keyframes marquee { 0% { transform: translateX(0); } 100% { transform: translateX(-100%); } } ``` 解释: - `.marquee` 元素的 `position` 属性设置为 `relative`,以便让内部元素 `span` 的绝对定位相对于 `.marquee` 元素。 - `.marquee` 元素的 `overflow` 属性设置为 `hidden`,以隐藏 `span` 元素超出父元素的内容。 - `.marquee` 元素的 `white-space` 属性设置为 `nowrap`,以防止换行。 - `.marquee span` 元素的 `position` 属性设置为 `absolute`,以便让它们相对于 `.marquee` 元素的左边界定位。 - `.marquee span` 元素的 `left` 属性设置为 `100%`,以便让它们开始时从父元素的右侧进入。 - `.marquee span` 元素的 `display` 属性设置为 `inline-block`,以便让它们在同一行显示。 - `.marquee span` 元素的 `animation` 属性定义了动画名称、持续时间、动画速度和循环次数。 - `@keyframes marquee` 定义了动画的关键帧,从 0% 到 100% 分别表示动画的开始和结束状态。在这个例子中,我们使用 `transform: translateX()` 属性将 `span` 元素向左移动,从而实现了文字跑马灯效果。 通过以上代码,你可以实现一个CSS文字跑马灯无缝衔接效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【拾光静好 微微一笑】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值