用jQuery怎样实现数据滚动的效果?
Admin 2021-09-01 群英技术资讯 788 次浏览
这篇文章给大家分享的是用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
图像优化对于网站建设和优化来说,是很重要的一部分,能大大提高网站的性能。对此,本文就给大家来分享一下JPEG/JPG、PNG、WebP这些常见的图片怎样优化,这些图像的应用场景及优缺点?
我们运行打包后会发现less转为了css文件,但css文件确通过js加入style标签,下面我们将css进行拆分出来,并以link标签引入,具体实现步骤一起看看吧
js如何查找字符串的最长单词?这篇文章给大家分享是关于基于Free Code Camp基本算法脚本来查找字符串的最长单词,本文会介绍三种方法,是小编比较推荐的,有需要的朋友可以看一看。
JavaScript属性操作,下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
typescript不仅可以约束我们的编码习惯,还能起到注释的作用,当我们看到一函数后我们立马就能知道这个函数的用法,需要传什么值,返回值是什么类型一目了然,这篇文章主要介绍了Vue-cli3中使用TS语法示例代码,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008