【问题标题】:Preloader: How can I set a timeout before the script adds the loaded class?预加载器:如何在脚本添加加载的类之前设置超时?
【发布时间】:2017-01-05 09:25:22
【问题描述】:

我不得不问你们,这段代码有什么问题,我为我的网站创建了一个预加载器,但我在 javascript 上失败了:

    <!-- script for preloader -->
    <script type="text/javascript/jquery">
        $(document).ready(function() {
            setTimeout(function() {
                $('body').addClass('loaded')
            }, 1500);
        });
    </script>

    <!-- preloader* -->
    <div id="loader-wrapper">
        <div id="loader"></div>
        <div class="loader-section section-one"></div>
        <div class="loader-section section-two"></div>
    </div>

因此,一旦&lt;body&gt; 标记获得class="loaded",预加载器就会停止。但由于某些原因,&lt;body&gt; 没有收到课程。

我不知道它是否有帮助,但这是整个文件:

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= $view->render('head') ?>
    <link href="packages/pagekit/theme-hello/css/theme.css" rel="stylesheet">
    <?php $view->script('theme', 'theme:js/theme.js') ?>
    <?php $url = $_SERVER['REQUEST_URI']; if($url == "/arn_architekten/bilderbuch"): ?>
        <style type="text/css">
            html {
                background-color: #000 !important;
                background-image: none !important;
            }
        </style>
    <? endif ?>
</head>    

<body>

    <!-- Render logo or title with site URL -->
    <a href="<?= $view->url()->get() ?>">
        <?php if ($logo = $params['logo']) : ?>
            <img src="<?= $this->escape($logo) ?>" alt="">
        <?php else : ?>
            <?= $params['title'] ?>
        <?php endif ?>
    </a>

    <!-- Render widget position -->
    <?php if ($view->position()->exists('sidebar')) : ?>
        <?= $view->position('sidebar') ?>
    <?php endif; ?>

    <!-- script for preloader -->
    <script type="text/javascript/jquery">
        $(document).ready(function() {
            setTimeout(function() {
                $('body').addClass('loaded')
            }, 1500);
        });
    </script>

    <!-- preloader* -->
    <div id="loader-wrapper">
        <div id="loader"></div>
        <div class="loader-section section-left"></div>
        <div class="loader-section section-right"></div>
    </div>        

    <!-- Render content -->
    <?= $view->render('content') ?>

    <!-- Insert code before the closing body tag  -->
    <?= $view->render('footer') ?>

</body>
</html>

希望有人能帮助我,对不起我的英语不好^^。

【问题讨论】:

  • "因此,一旦 标签获得 class="loaded",预加载器就会停止。"想解释一下这部分吗?

标签: javascript html indexing preloader


【解决方案1】:

如下更改脚本可能会导致问题

<script type="text/javascript">

$(document).ready(function() {
  setTimeout(function() {
    $('body').addClass('loaded');
  }, 1500);
});

</script>

更新

添加 jquery min js 如果你不加载 jquery js &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt; 在函数顶部或头部标签内添加此行,则会出现问题

更新 1 如下更新你的 head 标签

<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?= $view->render('head') ?>
        <link href="packages/pagekit/theme-hello/css/theme.css" rel="stylesheet">
//add below line 
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <?php $view->script('theme', 'theme:js/theme.js') ?>
        <?php $url = $_SERVER['REQUEST_URI']; if($url == "/arn_architekten/bilderbuch"): ?>
            <style type="text/css">
                html {
                    background-color: #000 !important;
                    background-image: none !important;
                }
            </style>
        <? endif ?>
    </head> 

更多信息请查看fiddle

希望对你有帮助。

【讨论】:

  • Uncaught ReferenceError: $ is not defined at this point: $(document).ready(function() {...}
  • @Thibs 很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
相关资源
最近更新 更多