如何在Kendo ScrollView中添加自动滚动间隔(jQuery实现)
为Kendo jQuery ScrollView添加自动轮播功能
问题说明
需要给Kendo ScrollView(jQuery版本)添加短间隔自动切换功能,但官方jQuery版本未提供类似Vue/Angular版本的automaticViewChangeInterval属性,现有初始化代码如下:
<!DOCTYPE html> <html> <head> <base href="https://demos.telerik.com/kendo-ui/scrollview/events"> <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style> <title></title> <link href="https://kendo.cdn.telerik.com/themes/7.0.1/default/default-main.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script> <script src="https://kendo.cdn.telerik.com/2023.3.1010/js/kendo.all.min.js"></script> <link rel="stylesheet" href="../content/shared/styles/examples-offline.css"> <script src="../content/shared/js/console.js"></script> </head> <body> <div id="example" style="margin:auto; width:70%"> <div id="scrollView" style="width: 660px; height: 420px; max-width: 100%; margin: auto;"> <div class="photo photo1" data-role="page"> </div><div class="photo photo2" data-role="page"> </div><div class="photo photo3" data-role="page"> </div><div class="photo photo4" data-role="page"> </div><div class="photo photo5" data-role="page"> </div><div class="photo photo6" data-role="page"> </div><div class="photo photo7" data-role="page"> </div><div class="photo photo8" data-role="page"> </div><div class="photo photo9" data-role="page"> </div><div class="photo photo10" data-role="page"> </div> </div> <script> $(document).ready(function () { function onChange(e) { kendoConsole.log("page " + e.nextPage); } function onRefresh(e) { kendoConsole.log("Total: " + e.pageCount + " Current: " + e.page); } $("#scrollView").kendoScrollView({ enablePager: true, contentHeight: "100%", change: onChange, refresh: onRefresh }); }); </script> </div> <style> #scrollview-home .photo { display: inline-block; background-size: cover; background-repeat: no-repeat; background-position: center center; } .photo1 { background-image: url("../content/shared/images/photos/11.jpg"); } .photo2 { background-image: url("../content/shared/images/photos/12.jpg"); } </style> <div class="kd-example-console"> <div class="header"> <span class="title">CONSOLE LOG</span> <span class="clear kd-text-secondary">Clear log</span> </div> <div class="console"></div> </div> </body> </html>
解决方案
通过setInterval定时器结合ScrollView实例的next()方法实现自动切换,同时处理循环逻辑(最后一页回到第一页),还可添加鼠标悬停暂停的交互优化:
修改后的完整代码如下:
<!DOCTYPE html> <html> <head> <base href="https://demos.telerik.com/kendo-ui/scrollview/events"> <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style> <title></title> <link href="https://kendo.cdn.telerik.com/themes/7.0.1/default/default-main.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script> <script src="https://kendo.cdn.telerik.com/2023.3.1010/js/kendo.all.min.js"></script> <link rel="stylesheet" href="../content/shared/styles/examples-offline.css"> <script src="../content/shared/js/console.js"></script> </head> <body> <div id="example" style="margin:auto; width:70%"> <div id="scrollView" style="width: 660px; height: 420px; max-width: 100%; margin: auto;"> <div class="photo photo1" data-role="page"> </div><div class="photo photo2" data-role="page"> </div><div class="photo photo3" data-role="page"> </div><div class="photo photo4" data-role="page"> </div><div class="photo photo5" data-role="page"> </div><div class="photo photo6" data-role="page"> </div><div class="photo photo7" data-role="page"> </div><div class="photo photo8" data-role="page"> </div><div class="photo photo9" data-role="page"> </div><div class="photo photo10" data-role="page"> </div> </div> <script> $(document).ready(function () { // 初始化ScrollView并获取实例 let scrollView = $("#scrollView").kendoScrollView({ enablePager: true, contentHeight: "100%", change: onChange, refresh: onRefresh }).data("kendoScrollView"); // 设置自动切换间隔(毫秒),示例为3秒 const intervalTime = 3000; // 启动自动轮播定时器 let autoInterval = setInterval(() => { scrollView.next(); }, intervalTime); function onChange(e) { kendoConsole.log("page " + e.nextPage); // 到达最后一页时,延迟切换回第一页(匹配动画时长,避免生硬跳转) if (e.nextPage === scrollView.pageCount()) { setTimeout(() => { scrollView.goTo(0); }, scrollView.options.animation.duration); } } function onRefresh(e) { kendoConsole.log("Total: " + e.pageCount + " Current: " + e.page); } // 鼠标悬停暂停自动轮播,离开时恢复 $("#scrollView") .on("mouseenter", () => clearInterval(autoInterval)) .on("mouseleave", () => { autoInterval = setInterval(() => { scrollView.next(); }, intervalTime); }); }); </script> </div> <style> #scrollview-home .photo { display: inline-block; background-size: cover; background-repeat: no-repeat; background-position: center center; } .photo1 { background-image: url("../content/shared/images/photos/11.jpg"); } .photo2 { background-image: url("../content/shared/images/photos/12.jpg"); } </style> <div class="kd-example-console"> <div class="header"> <span class="title">CONSOLE LOG</span> <span class="clear kd-text-secondary">Clear log</span> </div> <div class="console"></div> </div> </body> </html>
关键说明
intervalTime:自定义自动切换的时间间隔,单位为毫秒scrollView.next():调用ScrollView内置方法切换到下一页- 最后一页判断:通过
scrollView.pageCount()获取总页数,到达后调用goTo(0)回到第一页,延迟时间匹配动画时长保证过渡流畅 - 鼠标交互:添加
mouseenter/mouseleave事件,实现悬停暂停、离开恢复的用户体验优化
内容的提问来源于stack exchange,提问作者Joshua Juan
相关产品推荐
相关产品推荐

