用jQuery怎样实现数据滚动的效果?
Admin 2021-09-01 群英技术资讯 789 次浏览
这篇文章给大家分享的是用jQuery实现数据滚动的效果,下文实例实现了表格的数据滚动,感兴趣的朋友可以借鉴参考,接下来一起跟随小编看看吧。
HTML代码:
<div class="box">
<div class="box-header">
<div class="col">测试1</div>
<div class="col">测试2</div>
<div class="col">测试3</div>
<div class="col">测试4</div>
</div>
<div id="font-scroll">
<div class="box-body">
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
<div class="row">
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
<div class="col">测试文字</div>
</div>
</div>
</div>
</div>
CSS样式代码:
.box {
width: 400px;
text-align: center;
font-size: 14px;
border: 1px solid rgba(0, 0, 0, .3);
}
.box .box-header {
display: flex;
justify-content: space-evenly;
}
.box-body .row {
display: flex;
justify-content: space-evenly;
}
.box-header,
.box-body .row {
border-bottom: 1px dashed #404040;
}
.box .col {
padding: 10px 0 10px 0;
}
.box .col:nth-child(1) {
width: 80px;
}
.box .col:nth-child(2) {
width: 60px;
}
.box .col:nth-child(3) {
width: 80px;
}
.box .col:nth-child(4) {
width: 60px;
}
/* 内容滚动 */
#font-scroll {
/* 内容滚动可视高度 */
height: 199px;
overflow: hidden;
}
JS代码:
(function ($) {
$.fn.FontScroll = function (options) {
let d = { time: 1000 }
$.extend(d, options);
// 需要滚动的div父盒子
let box = this[0]
// 滚动间隔
let _time = d.time
// 这个办法只适合每行数据的高度都相同的情况
// // 每次滚动的高度(一般是一条数据的高度)
// let _contentChildHeight = box.children[0].children[0].offsetHeight
// // 滚动总高度,即内容的总高度(所有数据的总高度)
// let _contentTotalHeight = _contentChildHeight * box.children[0].children.length
// 这种办法适合所有情况,包括每行数据的高度都不相同的情况
// 获取所有行元素
let all_row_el = box.children[0].children
// 行总高度
let _contentTotalHeight = 0
// 每一行数据与前面所有行高度的叠加高度
let _contentChildHeight = []
for (let i in all_row_el) {
if ((new RegExp("^\\d+$")).test(i)) {
_itemHeight = all_row_el[i].offsetHeight
_contentTotalHeight += _itemHeight
i == 0 ? _contentChildHeight.push(_itemHeight) : _contentChildHeight.push(_contentChildHeight[i - 1] + _itemHeight)
}
}
// 需要滚动的div子盒子
let child1 = this.children('.box-body')
// 克隆出来滚动的div子盒子
// 克隆方法一
// let child1 = this.children('.box-body')[0]
// let child2 = this.children('.box-body')[1]
// child2.innerHTML = child1.innerHTML
// 克隆方法二
if ((box.offsetHeight + 5) < _contentTotalHeight) {
// 如果数据没有达到一定的高度,则不会执行滚动效果
child1.clone().insertAfter(child1)
/*启动定时器*/
let timer = setInterval(autoScrollLine, 30)
/*单行向上滚动效果*/
function autoScrollLine() {
/*判断滚动内容是否已经滚完,滚完了则滚动的值重新设置到0
否则就每隔30毫秒向上滚动1px*/
if (box.scrollTop >= _contentTotalHeight) {
box.scrollTop = 0;
} else {
box.scrollTop++;
}
/*判断滚动的距离刚好为一条--的高度时停掉定时器,
隔 _time 之后重新启动定时器即可实现--滚动停留效果 */
if (_contentChildHeight.indexOf(box.scrollTop) >= 0) {
clearInterval(timer)
setTimeout(() => {
timer = setInterval(autoScrollLine, 30)
}, _time)
}
}
}
}
})(jQuery);
使用方法:
$('#font-scroll').FontScroll({ time: 1000 });
效果图:

以上就是关于jQuery实现表格数据滚动效果的代码,希望本文对大家学习jQuery有帮助,想要了解更多jQuery实现数据滚动的内容,大家可以关注群英网络其它相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了JavaScript实现伸缩二级菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了key在Vue列表渲染时的原理和作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要为大家详细介绍了原生js实现无缝轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了JS 9个Promise面试题,对异步Promise感兴趣的同学,可以参考下
这篇文章给大家分享的是用JS实现鼠标移动格子随机变色的内容,也就是鼠标移入可以随机变换颜色的效果,对大家学习鼠标移动事件有一定的帮助,感兴趣的朋友可以参考,接下来一起跟随小编看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008