jQuery实现大屏滚动播放的效果

叁歲伎倆 2022-10-06 00:41 271阅读 0赞

场景需求:
在大屏幕上,消息会进行一个实时滚动播报的效果,将现有的内容进行一个来回滚动的播放~~

代码:

  1. <!DOCTYPE HTML>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>复选框checkbox自定义样式</title>
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  8. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  9. <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  10. <style>
  11. * {
  12. margin: 0;
  13. padding: 0;
  14. }
  15. .contScend {
  16. width: 400px;
  17. height: 200px;
  18. background: #000000;
  19. margin: 20px auto;
  20. overflow: hidden;
  21. }
  22. .contScend ul {
  23. list-style: none;
  24. width: 100%;
  25. height: 100%;
  26. color:red;
  27. font-size: 13px;
  28. }
  29. .contScend ul li {
  30. width: 100%;
  31. height: 40px;
  32. box-sizing: border-box;
  33. line-height: 40px;
  34. text-align: center;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <!-- 中间部分 -->
  40. <div class="contScend">
  41. </div>
  42. </body>
  43. <script type="text/javascript">
  44. $.ajax({
  45. url: "test.json",
  46. type: 'GET',
  47. dataType: 'json',
  48. success: function(data) {
  49. var html = "";
  50. html += '<ul>';
  51. $.each(data, function(i, item) { //
  52. html += '<li>' + item.name + ':' + item.numb + '人' + '</li>';
  53. });
  54. html += '</ul>';
  55. $(".contScend").html(html);
  56. scroll();
  57. }
  58. });
  59. function scroll() {
  60. //获得当前<ul>
  61. var $uList = $(".contScend ul");
  62. var timer = null;
  63. //触摸清空定时器
  64. $uList.hover(function() {
  65. clearInterval(timer);
  66. },
  67. function() { //离开启动定时器
  68. timer = setInterval(function() {
  69. scrollList($uList);
  70. },
  71. 1000);
  72. }).trigger("mouseleave"); //自动触发触摸事件
  73. //滚动动画
  74. function scrollList(obj) {
  75. //获得当前<li>的高度
  76. var scrollHeight = $("ul li:first").height();
  77. //滚动出一个<li>的高度
  78. $uList.stop().animate({
  79. marginTop: -scrollHeight
  80. },
  81. 600,
  82. function() {
  83. //动画结束后,将当前<ul>marginTop置为初始值0状态,再将第一个<li>拼接到末尾。
  84. $uList.css({
  85. marginTop: 0
  86. }).find("li:first").appendTo($uList);
  87. });
  88. }
  89. }
  90. </script>
  91. </html>

test.json

  1. [{
  2. "name": "体验区统计",
  3. "numb": 0
  4. }, {
  5. "name": "test909",
  6. "numb": 0
  7. }, {
  8. "name": "test910",
  9. "numb": 0
  10. }, {
  11. "name": "111",
  12. "numb": 0
  13. }, {
  14. "name": "test",
  15. "numb": 0
  16. }, {
  17. "name": "test11111",
  18. "numb": 0
  19. }, {
  20. "name": "记忆区统计",
  21. "numb": 0
  22. }]

效果如下:

6ec8225e1d7c0b647fdc17031642172e.gif

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读