【问题标题】:Ajax slideshow does not show anythingAjax 幻灯片不显示任何内容
【发布时间】:2012-04-25 11:49:51
【问题描述】:

您好,我正在使用此代码从文件夹中获取新图像以填充幻灯片。

每次循环ajax后应该检查文件夹的内容,看看是否有任何

更改(添加或删除图片)然后在下一个循环中对

进行新的更改

幻灯片

<html>
<meta http-equiv="refresh" content="1000"/> 
<head>
<title>Slideshow</title>
<style type="text/css">
    #slideshow
    #slideshow img { padding: 15px; border: 1px solid #ccc; background-color: #eee; }
    #slide {width: 370px; height: 220px; padding: 0;  margin:  0 auto; }

    #myslides {
    width: 370px;
    height: 220px;
    padding: 0;  
    margin:  0 auto;  
} 

#myslides img {  
    padding: 10px;  
    border:  1px solid rgb(100,100,100);  
    background-color: rgb(230,230,230);
    width: 350px;
    height: 200px;
    top:  0; 
    left: 0 
}

</style>
</head>
<!-- include jQuery library -->

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<!-- include Cycle plugin -->
<script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script>

<script type="text/javascript">
$(document).ready(function(){
$('#slideshow').cycle({
               fx: 'fade',
                speed: 700,
                timeout: 8000
        });
});
</script>

<body>

<div id="slideshow">

</body>



</html>

<script>
    $(function(){
        window.onload("fetchImages()", 2);

        function fetchImages() {
            $.ajax({
                type: "GET",
                url: "load.php"
            }).done(function(response){
                var curImgCount = $('#slideshow img').length;
                if (response.length > curImgCount) {
                    for (var i = curImgCount; i < response.length; i++) {
                        $('#slideshow').append('<img src="images/' + response[i] + '"');    
                    }
                }
            });
        }
    });
</script>

和php的内容:

<?php

    function returnimages($dirname="./images") {
         $pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";     
         $files = array();
         if($handle = opendir($dirname)) {
            while(false !== ($file = readdir($handle))){
                if(preg_match($pattern, $file)){ //if this file is a valid image 
                    $files[] = $file;
                } 
            }

            closedir($handle);
        }
        //sort($files);         
        natcasesort($files);   

        return($files);
    }

   $images = returnimages();
    foreach($images as $img)
    {
      echo json_encode($images);
    }

   ?>

【问题讨论】:

  • 也许这条线$('#slideshow').append('&lt;img src="images/' + response[i] + '"');应该像$('#slideshow').append('&lt;img src="images/' + response[i] + '" alt="" /&gt;');一样正确关闭

标签: php jquery ajax slideshow


【解决方案1】:

我正在测试这段代码,它工作正常,但为什么刷新元标记? AJAX 的目的不是防止这种情况发生并使其动态化吗?此外,最大的问题是您需要设置一个不太长的元刷新时间,以便图像在添加或删除时不会花费太长时间来反映,并且不会太短以防止某些图像完全显示(不可避免在一点)。

【讨论】:

    【解决方案2】:

    将您的 PHP 更改为:

    <?php
    
    function returnimages($dirname="./images") {
      $pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";
      $files = array();
      if($handle = opendir($dirname)) {
        while(false !== ($file = readdir($handle))){
          if(preg_match($pattern, $file)){ //if this file is a valid image
            $files[] = $file;
          }
        }
        closedir($handle);
      }
      //sort($files);
      natcasesort($files);
    
      return($files);
    }
    
    $images = returnimages();
    echo json_encode($images);
    
    ?>
    

    和你的 HTML 到:

    <html>
      <head>
        <meta http-equiv="refresh" content="1000"/>
        <title>Slideshow</title>
        <style type="text/css">
          #slideshow {
            position: relative;
          }
          #slideshow,
          #slideshow img {
            position: absolute;
            top: 0px;
            left: 0px;
            padding: 15px;
            border: 1px solid #ccc;
            background-color: #eee;
          }
          #slide {
            width: 370px;
            height: 220px;
            padding: 0;
            margin: 0 auto;
          }
          #myslides {
            width: 370px;
            height: 220px;
            padding: 0;
            margin: 0 auto;
          }
    
          #myslides img {
            padding: 10px;
            border:  1px solid rgb(100,100,100);
            background-color: rgb(230,230,230);
            width: 350px;
            height: 200px;
            top:  0;
            left: 0
          }
    
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script>
    
        <script type="text/javascript">
          function loadSlides() {
            $.ajax({
              type: "GET",
              url: "load.php"
            }).done(function(response) {
              var temp_images = eval("(" + response + ")");
              for(ti in temp_images)
              {
                //alert(temp_images[ti]);
                $('#slideshow').append('<img src="images/' + temp_images[ti] + '" alt="">');
              }
              startSlideshow();
            });
          }
    
          function startSlideshow()
          {
            $('#slideshow').cycle({
              fx: 'fade',
              speed: 700,
              timeout: 800
            });
          }
    
          $(document).ready(function(){
            loadSlides();
          });
        </script>
      </head>
      <body>
        <div id="slideshow" />
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      相关资源
      最近更新 更多