因项目需要实现消息通知上下无缝轮播的效果,所以写了一下,在这个分享出来,希望再次遇到此需求的道友,可以直接拷贝来用,节约一点不必要的时间。

原生JS版本

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文字上下无缝轮播</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
#container{
width: 60%;
margin: 100px auto;
position: relative;
height: 200px;
overflow: hidden;
}
#list-wrapper{
position: relative;
}
ul {
list-style: none;
background: #f8f8f8;
text-align: center;
padding: 0 20px;
}
li{
height: 35px;
line-height: 35px;
color: #fff;
}
li:nth-child(odd){
background: #5077aa;
}
li:nth-child(even){
background: #fb6b06;
}
</style>
<body>
<div id="container">
<div id="list-wrapper" style="top: 0">
<ul class="notice-list" id="notice-list">
<li>富强、民主、文明、和谐、自由1</li>
<li>富强、民主、文明、和谐、自由2</li>
<li>富强、民主、文明、和谐、自由3</li>
<li>富强、民主、文明、和谐、自由4</li>
<li>富强、民主、文明、和谐、自由5</li>
<li>富强、民主、文明、和谐、自由6</li>
</ul>
<ul class="notice-list" id="notice-list-2">
</ul>
</div>
</div>
<script>
var ROLL_SPEED = 100
var noticeList1 = document.getElementById('notice-list');
var noticeList2 = document.getElementById('notice-list-2');
var listWrapper = document.getElementById('list-wrapper');
noticeList2.innerHTML=noticeList1.innerHTML; var timer = setInterval(rollStart, ROLL_SPEED);
function rollStart() {
if (Math.abs(_subStr(listWrapper.style.top)) >= noticeList1.clientHeight) {
listWrapper.style.top = '0px'
} else {
var top = listWrapper.style.top
listWrapper.style.top = _subStr(top)-1+'px'
}
}
// 截取px前数值
function _subStr(str) {
var index = str.indexOf('px');
if (index > -1) {
return parseFloat(str.substr(0, index + 1))
}
}
</script>
</body>
</html>

jQuery版本

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文字上下无缝轮播</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
#container{
width: 60%;
margin: 100px auto;
position: relative;
height: 200px;
overflow: hidden;
}
#list-wrapper{
position: relative;
top: 0;
}
ul {
list-style: none;
background: #f8f8f8;
text-align: center;
padding: 0 20px;
}
li{
height: 35px;
line-height: 35px;
color: #fff;
}
li:nth-child(odd){
background: #5077aa;
}
li:nth-child(even){
background: #fb6b06;
}
</style>
<body>
<div id="container">
<div id="list-wrapper">
<ul class="notice-list" id="notice-list">
<li>富强、民主、文明、和谐、自由1</li>
<li>富强、民主、文明、和谐、自由2</li>
<li>富强、民主、文明、和谐、自由3</li>
<li>富强、民主、文明、和谐、自由4</li>
<li>富强、民主、文明、和谐、自由5</li>
<li>富强、民主、文明、和谐、自由6</li>
</ul>
<ul class="notice-list" id="notice-list-2">
</ul>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
var ROLL_SPEED = 100
var noticeList1 = $('#notice-list');
var noticeList2 = $('#notice-list-2');
var listWrapper = $('#list-wrapper');
noticeList2.html(noticeList1.html())
listWrapper.css('top', 0)
var timer = setInterval(rollStart, ROLL_SPEED); function rollStart() {
if (Math.abs(_subStr(listWrapper.css('top'))) >= noticeList1.height()) {
listWrapper.css('top', 0)
} else {
var top = listWrapper.css('top');
listWrapper.css('top', _subStr(top) - 1)
}
} // 截取px前数值
function _subStr(str) {
var index = str.indexOf('px');
if (index > -1) {
return parseFloat(str.substr(0, index + 1))
}
}
</script>
</body>
</html>

最新文章

  1. Multiload-ng
  2. dbflow 批量 增删查改
  3. java时间库Joda-Time
  4. React笔记_(6)_react语法5
  5. uva 816 Abbott的复仇
  6. SQL Server select into/bulkcopy用法
  7. [转]W3C 验证 there is no attribute target for this element
  8. ios添加pre和post build action
  9. SQL基础-----DML语句
  10. 洛谷P2221 [HAOI2012]高速公路
  11. php json数据 入库时 转义字符丢失
  12. Vulnerability Scanning
  13. asp.net core的DI框架思考以及服务实例的获取方式总结
  14. win32: 静态控件(Static) - SS_NOTIFY - 响应事件
  15. [UWP 自定义控件]了解模板化控件(6):使用附加属性
  16. BootstrapTable(附源码)
  17. Echarts 地图上显示数值
  18. 第七次Scrum冲刺
  19. Centos 7 快速搭建IOS可用IPsec
  20. 在Bootstrap框架中,form-control的效果

热门文章

  1. Xcode cannot run using the selected device after upgrade
  2. VS2010提示error TRK0002: Failed to execute command
  3. Nginx负载均衡之TCP/UDP流
  4. nginx的配置系统
  5. Linux对用户态的动态内存管理
  6. CreateProcessEx创建进程
  7. 1381. 删除 (Standard IO)
  8. 带有headers的urllib库爬取
  9. this.$router.push相关的vue-router的导航方法
  10. Hibernate入门教程(二):Hibernate核心API