jQuery Cycle 中的 DOM 怪异

发布于 2024-09-26 13:39:28 字数 1728 浏览 3 评论 0原文

我正在从正在构建的 WordPress 网站中的一些幻灯片的目录中动态获取图像。有时它们会出现在页面上,有时则不会。

看着这个,我确信我需要某种预加载器来在循环插件执行其操作之前将我的图像初始化到 DOM 中,但我只是不知道在哪里插入相关代码。我的代码如下:

<?php
        function show_gallery($path){
            if($path == ""){
                return;
            }
            // Fix the path to be usable
            $path = "wp-content/themes/mytheme" . "$path";

            // Ignore files/paths that aren't usable
            $img_list = array_diff(scandir($path), array('.', '..', '.DS_Store'));
            $imgcount = count($img_list);
            if($imgcount > 1){
                echo "<div id=\"overlay-prev\"><img src='/wp-content/themes/grisedale/assets/gallery/prev.png' class='prev'></div>";
                echo "<div id=\"overlay-next\"><img src='/wp-content/themes/grisedale/assets/gallery/next.png' class='next'></div>";
            } else {
                echo "";
            }
            echo "<div id=\"current-image\">";
            // Spit out the images to be controlled by cycle
            foreach ($img_list as $img){
                echo "<img class='gallery-images' src='../../../$path$img'>\n";
            }
            echo "</div>";
        }
    ?>
    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#current-image').cycle({
            fx: 'fade',
            speed:  'fast', 
            timeout: 0,
            prev:    '#overlay-prev',
            next:    '#overlay-next', 
            containerResize: true
        });
    });
    </script>

老实说,图像都很小,如果有某种健全性测试,我可以用 PHP 进行,这可能就足够了。无论如何,我只需要将图像以可预测的方式加载到任何具有图库的页面中,而无需重新加载三到四次才能看到它们。干杯。

I'm getting images from directories dynamically for some slideshows in a WordPress site I'm building. Sometimes they show up on pages, sometimes they don't.

Looking at this I'm sure I need some sort of preloader to initialize my images into the DOM before the cycle plugin does what it does, but I just don't know where to shoehorn the pertinent code. My code is as follows:

<?php
        function show_gallery($path){
            if($path == ""){
                return;
            }
            // Fix the path to be usable
            $path = "wp-content/themes/mytheme" . "$path";

            // Ignore files/paths that aren't usable
            $img_list = array_diff(scandir($path), array('.', '..', '.DS_Store'));
            $imgcount = count($img_list);
            if($imgcount > 1){
                echo "<div id=\"overlay-prev\"><img src='/wp-content/themes/grisedale/assets/gallery/prev.png' class='prev'></div>";
                echo "<div id=\"overlay-next\"><img src='/wp-content/themes/grisedale/assets/gallery/next.png' class='next'></div>";
            } else {
                echo "";
            }
            echo "<div id=\"current-image\">";
            // Spit out the images to be controlled by cycle
            foreach ($img_list as $img){
                echo "<img class='gallery-images' src='../../../$path$img'>\n";
            }
            echo "</div>";
        }
    ?>
    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#current-image').cycle({
            fx: 'fade',
            speed:  'fast', 
            timeout: 0,
            prev:    '#overlay-prev',
            next:    '#overlay-next', 
            containerResize: true
        });
    });
    </script>

Honestly, the images are all small and if there were some kind of sanity test I could do with PHP that would probably suffice. In any case, I just needs the images to load predictably into any page that has a gallery without having to do three or four reloads to see them. Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-10-03 13:39:28

更新

因此,jQuery 循环插件的技巧是为图像设置特定的宽度和高度。我设置了一个宽度,显然对于大多数浏览器来说这还不够。我还设置了最小高度,现在一切都按预期进行。

UPDATE

So, the trick with the jQuery cycle plugin is to set a specific width and height for your images. I was setting a width, and apparently for most browsers that just isn't enough. I set a minimum height as well and now everything works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文