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

css 左右滚轮无缝衔接

最近的项目有做到一个功能 类似跑马灯或者公告栏那种
有文字 也有列表的 所以 写了两种
第一种公告栏文字是用的js
第二种图文类型是用的css
两种方法 记录一下

第一种 纯文字滚动

其实也是根据js去计算dom的宽度 通过js去给css赋值

<div class="div1"><div class="div2"><a :href="i.link" v-for="(i,j) in disArr" :key="j" target="_blank" class="tagName" :ref="'tagName' + j" style="margin-right: 118px;"><div style="color: #fff;margin-right: 10px;"><img src="~@/assets/home/s_icon.png" style="width: 32px;margin-right: 20px;">{{i.title}}</div></a></div></div>  
let width = 0;
let els = this.disArr;
console.log(this.disArr,'dis');
let _this = this;
for(var i = 0 ; i < els.length;i++){let tag = _this.$refs['tagName' + i];width+= tag[0].offsetWidth+118;// console.log(tag,'tag');
}
// console.log(width,'width');
document.styleSheets[0].insertRule("@keyframes anima" +"{" +"0%{transform: translateX(100%);}" +"100%{transform: translateX(-"+ width +"px);" +"}"
)
.div1 {width: 100%;height: 63px;line-height: 63px;background: linear-gradient(90deg, #FE890C 0%, #D549F5 100%);overflow: hidden;box-sizing: border-box;
}
.div1:hover .div2{-webkit-animation-play-state: paused; /*动画暂停播放*/
}
.div2 {white-space: nowrap;display: flex;color: #fff;font-size: 20px;font-family: "Black";animation: 10s anima linear infinite normal;cursor: pointer;
}

因为文字内容无法确定 如果是固定的话是不需要js的 在css里直接给一个固定值就可以了

第二种 图片文字

<div class="wrap"><div class="list"><div class="my-list" v-for="(item,index) in sendVal" :key='index'><div class="my-uname"><a-col :span="6"><img :src="require(`../assets/home/${item.url}.png`)" alt="" class="winner_icon"></a-col><a-col :span="9" class="w_name">{{item.account}}</a-col><a-col :span="9" class="w_gusdt" style="text-align: right;">{{item.roll}}</a-col></div></div></div></div>
* {
margin: 0;padding: 0;}.wrap {width: 100%;height: 100px;overflow: hidden;position: relative;padding-left: 3480px;}.list {list-style: none;width: 6960px;height: 100px;position: absolute;top: 0px; /*right:0px;向右*/left: 0px; /*向左*/animation: mymove 34.8s 1s infinite linear;display: flex;align-items: center;.my-list{margin:0 12px;display: inline-block;font-size: 24px;line-height: 50px;padding: 20px 16px; width: 324px;border-radius: 20px;border: 1px solid #7C8097;.my-uname{color: #FFFFFF;font-size: 14px;.winner_icon{width: 56px;}.w_name{font-weight: 500;}.w_gusdt{color: #665DF5;font-family: 'Roman';}.winner_icon{object-fit: contain;width: 56px;height: 56px;}}}}@keyframes mymove {from {left: 0px;}to {left: -100%;}}.list:hover {animation-play-state: paused;cursor: pointer;}

css的这种方法我自己摸索出来的 要做到完全无缝相接 例如一行需要展示10条数据 那么我们需要把这10条数据先重复一遍 就是一行20条 这样滚动起来才会达到无缝的效果 一个box的宽度例如是324px 那么加上margin的宽度 一共是348 10个box就是3480 20个就是6960px 还有一个地方的属性我没有弄明白 就是padding-left这个一定要加上 如果不加上 就达不到完全无缝滚动的效果

现在的滚动效果是向左滚动 如果是希望达到向右滚动的效果 那么就把css里的属性改一下就可以了

@keyframes mymove {from {right: 0px;}to {right: -100%;}
}

在这里插入图片描述

http://www.lryc.cn/news/195863.html

相关文章:

  • Hadoop分布式文件系统-HDFS
  • 专业图表绘制软件 OmniGraffle Pro mac v7.22.1中文版软件介绍
  • Git 本地文件合并和恢复
  • 记录git仓库pr没有显示贡献者的问题,以及如何提交一个pr(简)
  • 入侵检测代码
  • 数字孪生技术如何提高化工生产安全性?
  • PHP 如何查看php函数源码
  • 前端web自动化测试:selenium怎么实现关键字驱动
  • C++标准模板(STL)- 类型支持 (数值极限,min,lowest,max)
  • 国际SPEC CPU创榜以来整机最高纪录!浪潮信息八路服务器TS860G7刷新权威算力基准评测性能
  • 【linux】重定向+缓冲区
  • 【vim 学习系列文章 10 -- vim 将代码中空格高亮显示方法】
  • 吴恩达深度学习笔记
  • 基于SpringBoot的医疗预约服务管理系统
  • Java本地缓存的使用
  • 华为数通方向HCIP-DataCom H12-831题库(单选题:281-300)
  • 【分享Python代码】图片转化为素描画
  • 汇川Easy521PLC与压力传感器485通讯实例
  • 创意作品管理软件 Bridge 2024 mac中文版 br2024功能特色
  • 【分享】教你加速访问GitHub,进来学!
  • 利用在线培训系统提升员工技能,助力企业发展
  • 深度学习小工具:Linux 环境下的用户命令和脚本顺序执行器
  • c# WPF 应用程序在屏幕上居中显示
  • JVM之class文件结构剖析
  • TDengine 签约中石化,支撑八大油田 PCS 系统
  • win11 定时计划任务
  • C++入门之引用与内联函数
  • 浅谈智能照明控制系统应用在城市轨道交通
  • macos 中ios系统升级,但是macos还是老系统,在手机上无法安装ios软件
  • Pytest+Allure生成可添加附件的测试报告