javascript - 在移动设备上的 pageshow() 事件上使用 jcarousel 框架时延迟几秒

标签 javascript jquery html jquery-mobile jcarousel

我正在使用 jQuery框架-jcarousel.js在我的移动应用程序的页面上水平订购一些项目 jQuery Mobile + HTML .

当我导航到页面时,首先我从网络服务获取所有图像,将它们放在垂直列表中 - <ul>动态使用JavaScript ,并以这种方式从索引调用脚本:

   $('#imagesPage').live('pageshow', function (event, ui) {
        jQuery('#imagesList').jcarousel({visible:2});
   });

它在某些移动设备上运行良好,但在 iPhone 中,例如,在导航到该页面时,它 缓慢完成,因此您首先会看到垂直列表,1-2 秒后它会变为水平列表。

我该如何改进它,这样我就不会看到从垂直到水平的几秒钟的延迟变化,并且仅在列表正确组织后才显示页面。

最佳答案

那你就应该作弊。

<强> jCarousel 有一个init回调函数。让您的 ul 容器用一点 css 隐藏:

display: block;

jCarousel 完成初始化,只是通过init回调函数显示ul容器。

工作 HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <link rel="stylesheet" href="http://sorgalla.com/projects/jcarousel/skins/tango/skin.css" />    
        <style>
            .hidden {
                display: none;
            }
        </style>
        <script src="http://sorgalla.com/projects/jcarousel/lib/jquery-1.9.1.min.js"></script>
        <script src="http://sorgalla.com/projects/jcarousel/lib/jquery.jcarousel.min.js"></script>                    
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
        <script>
            $(document).on('pagebeforeshow', '#index', function(){ 
                $('#mycarousel').jcarousel({        
                    scroll: 1,
                    initCallback:   carouselInit
                });
            });

            function carouselInit() {
                $('#mycarousel').show();
            }
        </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content">
                <ul id="mycarousel" class="jcarousel-skin-tango hidden">
                    <li><img src="http://static.flickr.com/66/199481236_dc98b5abb3_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/75/199481072_b4a0d09597_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/57/199481087_33ae73a8de_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/77/199481108_4359e6b971_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/58/199481143_3c148d9dd3_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/72/199481203_ad4cdcf109_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/58/199481218_264ce20da0_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/69/199481255_fdfe885f87_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/60/199480111_87d4cb3e38_s.jpg" width="75" height="75" alt=""/></li>
                    <li><img src="http://static.flickr.com/70/229228324_08223b70fa_s.jpg" width="75" height="75" alt=""/></li>
                </ul>
            </div>
        </div>    
    </body>
</html>   

关于javascript - 在移动设备上的 pageshow() 事件上使用 jcarousel 框架时延迟几秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16580984/

相关文章:

javascript - 为什么 jQuery resize 事件会触发两次?

html - 页脚最后一页只有 html + css

html - 为什么一个部门在另一个部门内部,尽管它们的代码指示相反?

javascript - 如何修复错误 : listen EADDRINUSE while using NodeJS?

javascript - 使用数据表呈现多维嵌套 JSON 响应

javascript - Div 不在另一个 div 中居中

javascript - Angular 2 ngIf 和 ngFor 组合

javascript - 带有 Webstorm 的 Node.js -

javascript - 如何在 React 中实现输入更改时的去抖自动保存?

javascript - $.ajax 相当于 $.load 内容过滤器