代码之家  ›  专栏  ›  技术社区  ›  markratledge

jquery和php图像旋转器拼图

  •  4
  • markratledge  · 技术社区  · 15 年前

    jQuery拼图

    我有一个PHP脚本,它从文件夹中返回随机JPG图像的名称。这很好,因为我根本不需要重命名这些图像;我只是把它们放到文件夹中,随机化器就可以工作了。现在,我这样称呼剧本- http://mydomain.com/images/rotate.php -在一个简单的网页重新加载,它交换图像。

    但我希望它能与jquery一起工作,因为我希望以大约10秒的间隔在新图像中交换图像,并使它们淡入淡出。

    编辑1/23/10:

    这是通过在spacer.gif中交换来实现的。也许有一个更优雅的解决方案,但这对我来说是可行的。芒奇通过午夜闪电的一个想法发现了这一点:

    function swapImage(){
      var time = new Date();
      $('#image').fadeOut(1000)
       .attr('src', 'http://mydomain.com/spacer.gif')
       .attr('src', 'http://mydomain.com/images/rotate.php?'+time.getTime())
       .fadeIn(1000);
    }
    
    var imageInterval = setInterval('swapImage()',10*1000); 
    

    这是rotate.php:

    <?php
    
    $folder = '.';
    
    
        $extList = array();
        $extList['gif'] = 'image/gif';
        $extList['jpg'] = 'image/jpeg';
        $extList['jpeg'] = 'image/jpeg';
        $extList['png'] = 'image/png';
    
    
    $img = null;
    
    if (substr($folder,-1) != '/') {
        $folder = $folder.'/';
    }
    
    if (isset($_GET['img'])) {
        $imageInfo = pathinfo($_GET['img']);
        if (
            isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
            file_exists( $folder.$imageInfo['basename'] )
        ) {
            $img = $folder.$imageInfo['basename'];
        }
    } else {
        $fileList = array();
        $handle = opendir($folder);
        while ( false !== ( $file = readdir($handle) ) ) {
            $file_info = pathinfo($file);
            if (
                isset( $extList[ strtolower( $file_info['extension'] ) ] )
            ) {
                $fileList[] = $file;
            }
        }
        closedir($handle);
    
        if (count($fileList) > 0) {
            $imageNumber = time() % count($fileList);
            $img = $folder.$fileList[$imageNumber];
        }
    }
    
    if ($img!=null) {
        $imageInfo = pathinfo($img);
        $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
        header ($contentType);
        readfile($img);
    } else {
        if ( function_exists('imagecreate') ) {
            header ("Content-type: image/png");
            $im = @imagecreate (100, 100)
                or die ("Cannot initialize new GD image stream");
            $background_color = imagecolorallocate ($im, 255, 255, 255);
            $text_color = imagecolorallocate ($im, 0,0,0);
            imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
            imagepng ($im);
            imagedestroy($im);
        }
    }
    
    ?>
    
    6 回复  |  直到 15 年前
        1
  •  4
  •   munch    15 年前

    我看到这样做的缺点是,新图像将有一个加载周期,因此动画可能有点古怪。如果一个$\u get值等于一个值,则返回路径的文件由两部分组成,如果未设置该$\u get值或该值等于其他值,则返回图像,这可能是有益的。这样可以预加载一系列图像,并且图像之间的动画会更平滑。

    说了这句话,我觉得像这样的事情应该奏效。

    $(document).ready(function(){
       function swapImage(){
          var time = new Date();
          $('#image').fadeOut(1000)
           .attr('src', 'http://mydomain.com/images/rotate.php?'+time.getTime())
           .fadeIn(1000);
       }
    
       var imageInterval = setInterval('swapImage()',10*1000); 
    });
    

    时间到了,所以浏览器认为它得到了一个新的图像。

    太空船

    要使用黑色间隔符进行此操作,我建议将您的图像包装在一个DIV中,并给DIV A_000背景色以匹配间隔符:

    #image-wrap{background-color:#000;}
    

    它会使图像实际上褪色为黑色,而不是褪色为当前的背景色,变为黑色,然后淡入。JS将非常类似于上述内容:

    function swapImage(){
      var time = new Date();
      $('#image').fadeOut(1000)
       .attr('src', 'http://mydomain.com/spacer.gif')
       .attr('src', 'http://mydomain.com/images/rotate.php?'+time.getTime())
       .fadeIn(1000);
    }
    
    var imageInterval = setInterval('swapImage()',10*1000); 
    

    把时间放在那里可能是不必要的,但是,嘿,这是另一种防止浏览器缓存“图像”的安全措施。

        2
  •  2
  •   czarchaic    15 年前

    由于您的PHP脚本正在返回新图像的源代码,因此最好避免使用 load() 并使用一个简单的Ajax调用来交换图像的源代码。

    var img=$('#image');//cache the element
    
    function refreshNotification(){
      $.ajax({
        url: 'http://mydomain.com/images/rotate.php',
        success: function(src){
          img.attr({src: src});
        }
      });
    }
    
    setInterval(refreshNotification, 10000);
    
        3
  •  1
  •   Tom Bartel    15 年前

    类似的事情可能会奏效,假设您的PHP脚本只返回图像的URL:

    $(document).ready(function(){
        window.setInterval(switchImage, 10000);
    
        function switchImage() {
            var rn = Math.floor(Math.random() * 100000)
            $.get('http://mydomain.com/images/rotate.php', 
                  { n: rn }, 
                  receiveNewImage);
        }
    
        function receiveNewImage(src) {
            $('#image').fadeTo(1000, 0.0, function() { switchAndFadeIn(src); } );
        }
        function switchAndFadeIn(newSrc) {
            $('#image').attr('src', newSrc).fadeTo(1000, 1.0);
        }
    });
    

    编辑:增加随机参数。

    编辑:在您的PHP中,这样有帮助吗?

    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Expires data in the past
    
        4
  •  1
  •   robertbasic    15 年前

    如何定义 swapImage() 功能超出 $(document).ready() 街区?

    <script type="text/javascript" scr="path/to/jquery.js"></script>
    <script type="text/javascript">
    function swapImage(){
        var time = new Date();
        $('#rotate').fadeOut(1000)
         .attr('src', 'rotate.php?'+time.getTime())
         .fadeIn(1000);
    }
    $(document).ready(function(){
      var imageInterval = setInterval('swapImage()',5000);
    });
    </script>
    
    <img src="rotate.php" id="rotate" />
    
        5
  •  0
  •   Val    15 年前
    $("#image").each(function({
    $(this).fadeOut(function() { 
    
    $(this).attr("src", "http://mydomain.com/images/image.jpg"); 
    $(this).load(function() { $(this).fadeIn(); }); // this should be on the bottom....
    }); 
    })
    

    检查JQ上的每个功能 Each

    我已经更新了脚本,我认为它应该可以工作,因为您正在等待加载图像,但它没有源代码… 看看这个> <img onerror="alert('there was an error') />" 如果您得到错误,这意味着源不存在。顺便说一句,如果您计划使用多个图像,则不应使用图像,因为HTML上可能有一个唯一的ID,否则您会遇到冲突。

    希望这有帮助

        6
  •  0
  •   MidnightLightning    15 年前

    您的设置缺少告诉jquery不要缓存Ajax请求的步骤。有一个 'cache' parameter 可以添加到Ajax调用中,以强制它获取新副本:

    $.ajax({
      url: 'http://mydomain.com/images/rotate.php',
      cache: false,
      success: function(src){
        img.attr({src: src});
      }
    });