javascript - PHP 图片库与延迟加载

标签 javascript php jquery html jquery-lazyload

我正在尝试使用 php 和lazyload 构建一个图片库。我有大约 8000 张图像,不应立即加载。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
  <div class="gallery">
    <?php
                $dir = "img/";
                $i = 0;
                if (is_dir($dir)) {
                    if ($dh = opendir($dir)) {
                    while (($file = readdir($dh)) !== false) {
                        if(is_file($dir . $file)){
                             echo "<img class='lazyload' data-src='img/$file' src='img/$file' width='auto' height='600px'>";
                        }
                      }
                    closedir($dh);
                    }
                } ?>

<script src="https://afarkas.github.io/lazysizes/lazysizes.min.js"></script>

  </div>
</body>
</html>

我可以一次加载 img 文件夹中的每张图片,但向下滚动时无法加载更多图片。我不想一次加载所有图像。

最佳答案

您只需删除 src='img/$file' 即可正常工作。 该问题可能是由于图像 src 路径错误造成的,这里是静态图像工作片段:

<script src="https://afarkas.github.io/lazysizes/lazysizes.min.js"></script>
<div class="gallery">
   
<img class="lazyload" data-src='https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' width='auto' height='600px'><br><br>

<img class="lazyload" data-src='https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' width='auto' height='600px'><br><br>

<img class="lazyload" data-src='https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' width='auto' height='600px'><br><br>

<img class="lazyload" data-src='https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' width='auto' height='600px'><br><br>

<img class="lazyload" data-src='https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' width='auto' height='600px'><br><br>

<img class="lazyload" data-src='https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg' width='auto' height='600px'><br><br>
<img class='lazyload' data-src='https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' width='auto' height='600px'><br><br>
                
</div>

关于javascript - PHP 图片库与延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149551/

相关文章:

php - 我们如何在不对 MYSQL 施加任何负载的情况下创建 XML?

javascript - 在 JavaScript 中向整数添加尾随数字

javascript - 将 `this` 参数发送到函数

php - 不小心用 PHP 设置了错误的 chmod

PHP preg_match 与 < 和 >

c# - 使用 jQuery 将 HTML 表格导出为 MS Excel 格式

jquery - 展开和折叠 jQuery UI Accordion 的所有部分

javascript - Rails 表单输入字段充满了 Bootstrap 3 模式对话框

javascript - 我是否已达到浏览器中 JavaScript 可以处理的对象大小的限制?

javascript - 如何在弹出模式中仅显示外部 html 文件中的一个 div id?