当前位置: 首页 > article >正文

纯css 轮播图片,鼠标移入暂停 移除继续

核心

滚动:
animation: 动画名称 20s linear infinite normal;

停止:
animation: 动画名称 20s linear infinite paused;

完整例子:

html:

<div class="carousel-wrapper">
  <div class="carousel">
    <div class="item"><img src="https://logo.clearbit.com/apple.com" alt="apple"></div>
    <div class="item"><img src="https://logo.clearbit.com/google.com" alt="Google"></div>
    <div class="item"><img src="https://logo.clearbit.com/amazon.com" alt="Amazon"></div>
    <div class="item"><img src="https://logo.clearbit.com/microsoft.com" alt="Microsoft"></div>
    <div class="item"><img src="https://logo.clearbit.com/facebook.com" alt="Facebook"></div>
    <div class="item"><img src="https://logo.clearbit.com/netflix.com" alt="Netflix"></div>
    <div class="item"><img src="https://logo.clearbit.com/tesla.com" alt="Testla"></div>
    <div class="item"><img src="https://logo.clearbit.com/nike.com" alt="Nike"></div>
    <div class="item"><img src="https://logo.clearbit.com/adidas.com" alt="Addidas"></div>
    <div class="item"><img src="https://logo.clearbit.com/coca-cola.com" alt="Coca-Cola"></div>
<!-- we need to add duplicate elements to make up the space - this needs to be magic numbered according to size of elements and width of container -->
    <div class="item"><img src="https://logo.clearbit.com/apple.com" alt="apple"></div>
    <div class="item"><img src="https://logo.clearbit.com/google.com" alt="Google"></div>
    <div class="item"><img src="https://logo.clearbit.com/amazon.com" alt="Amazon"></div>
    <div class="item"><img src="https://logo.clearbit.com/microsoft.com" alt="Microsoft"></div>
    
  </div>
</div>

css:

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
:root{
  --bg-clr: #64748B;
}
body {
  min-height: 100svh;
  display: grid;
  place-content: center;
  background-color: var(--bg-clr);
}

.carousel-wrapper {
  --width: 100px;
  --gap: 0;
  --num-items: 10;
  --ani-offset: calc(var(--width) * var(--num-items) * -1);
  --ani-speed: 10s;
  
  width: 400px;
  overflow: hidden;
  position: relative;
}
.carousel-wrapper::before,
.carousel-wrapper::after{
  content: '';
  position: absolute;
  width: 20%;
  height: 100%;
  z-index: 1;
  top: 0;
}
.carousel-wrapper::before{
  left: 0;
  background-image: linear-gradient(to right,var(--bg-clr) 0%,transparent 50%);
}
.carousel-wrapper::after{
  right: 0;
  background-image: linear-gradient(to left,var(--bg-clr) 0%,transparent 50%);
}

.carousel {
  display: flex;
  align-items: center;
  animation: slide var(--ani-speed) linear infinite normal;
}
.carousel:hover {
  animation: slide var(--ani-speed) linear infinite paused;
}
.item{
  flex: 1 0 var(--width);
  text-align: center;
  padding:1rem;
}
.item:last-child{
  /*background: red;*/
}
.item > img{
  width: 100%;
  height: auto;
  object-fit: cover;
}

@keyframes slide {
  100% {
    transform: translateX(var(--ani-offset));
  }
}


http://www.kler.cn/news/355890.html

相关文章:

  • C++初阶
  • 云服务解决方案,针对小程序、网页、HTML5等轻量化视频解决方案
  • (42)MATLAB中使用fftshift绘制以零为中心的功率谱
  • Python 网络编程:端口检测与IP解析
  • OBOO鸥柏自助服务终端:开启智慧城市便民新篇章
  • 【前端学习】AntV G6-09 复杂的自定义边、边动画
  • 模块化JS + OpenAI 手搓数据分析项目
  • 力扣 83.删除排序链表中的重复元素
  • Linux 系统上生成大文件的多种方法
  • mysql操作详解
  • Linux安装 php5.6
  • java线程池bug的一些思考
  • 如何在word里面给文字加拼音?
  • java对象拷贝
  • 【Linux】进程ID和线程ID在日志中的体现
  • CentOS7安装Gitlab服务
  • python爬虫登录校验之滑块验证、图形验证码(OCR)
  • nuScenes数据集使用的相机的外参和内参
  • Spring的起源与发展
  • (已解决)vscode使用launch.json进行debug调试报错:Couldn‘t spawn debuggee:embedded null byte