【问题标题】:jQuery Next-Previous Gallery Buttons Not Stopping at EndjQuery Next-Previous Gallery 按钮不会在末尾停止
【发布时间】:2014-07-31 15:32:03
【问题描述】:

我构建了这个 div,它允许用户浏览相册中的图像。 它首先使用 php-mysql 获取图像的文件路径,然后将行数分配给一个名为 array_size 的 javascript 变量。然后它使用 array_size 变量创建图片列表数组,并为数组的每个元素分配一个文件路径。然后它会显示弹出 div,其中包含图像以及下一个、后退、关闭一组按钮。目前,后退按钮将在到达数组的第 0 个元素时停止处理照片,这没关系,但是当它到达最大元素时,下一个按钮将继续滚动数组,没有为以下图像分配任何值。我希望下一个和后退按钮都可以在数组中旋转,目前我不知道如何实现这一点。我一直在努力解决这个问题,距离发布这个野兽还有一周的时间,所以我真的不能再坐在我的手上了。

HTML 页面:

<html>
<head>
<style>
@import "album_style.css";
</style>
<script type="text/javascript" src="jquery-1.10.2.min.js" ></script>
<script>

 $(document).ready(function(){
var array_size = 0 ;
var active_index ;
var source_image ;
$('#largeImage').hide() ;
$('#imageBackground').hide() ;
$('#gallery img').click(function(){
    $('#largeImage img').attr('src',$(this).attr('src').replace('gallery','large'));
    $('#largeImage').show() ;
    $('#imageBackground').show() ;
    active_index = parseInt( $(this).attr('value') ) ;

});
$('.next').click(function( event ){
    active_index = active_index + 1 ;
    max_index = array_size - 1 ;
    if( active_index == array_size )
    {
        active_index = 0 ;
    }
    source_image = $('#largeImage img').attr('src') ;
    var newSrc = $('#largeImage img').attr('src').replace( source_image, picture_list[ active_index ] ) ;
    $('#largeImage img').attr('src', newSrc ) ;


});
$('.back').click(function( event ){
    active_index = active_index - 1 ;

    if( active_index < 0 )
    {
        active_index = array_size ;
    }
    source_image = $('#largeImage img').attr('src') ;
    var newSrc = $('#largeImage img').attr('src').replace( source_image, picture_list[ active_index ] ) ;
    $('#largeImage img').attr('src', newSrc ) ;

}) ;
$('.close').click(function(){
    $('#largeImage').hide();
    $('#imageBackground').hide() ;
});



});
</script>
</head>
<body>
<?php
require( 'albums_functions.php' );

draw_masthead();
gallery();



?>
</body>

</html>

PHP:

function gallery()  //displays only the first 80 photos in the database.  
{
try
{
    $album_id = $_GET['id'] ;
    $db = honneyconnect( ) ;
    if( mysqli_connect_error() )
    {
        throw new Exception( "Could not connect to the database") ;
    }
    $query = 'select * from albums where album_id="'.$album_id.'"' ;
    $albums = $db->query( $query) ;
    $album_name = $albums->fetch_row();
    $default_pic = $album_name[1].'/'.$album_name[2] ;
    $albums->free();
    $query = 'select * from photos where album_id="'.$album_id.'"' ;
    $photos = $db->query( $query ) ;
    if( !$photos )
    {
        throw new Exception( "Query returned zero results." ) ;
    }
    else
    {
        $number_of_photos = $photos->num_rows ;
        echo "<script type='text/javascript'> array_size = parseInt( ".$number_of_photos." )  ;</script>" ; //figure out the size of the javascript array of photo sources 
        echo "<script type='text/javascript'> var picture_list = new Array( array_size ) ;</script>" ; //define the array
        echo "<div id='imageBackground'></div>" ;
        echo "<div id='largeImage'><input type='button' class='close' value='X'><input type='button' class='back' value='<<'><img src='".$default_pic."' ><input type='button' class='next' value='>>'></div>";
        echo "<div id='gallery'>" ;
        echo "<div id='thumbpanel'>" ;

        if( $number_of_photos > 80 )
        {
            $stop = 80 ;
        }
        else
        {
            $stop = $number_of_photos ;
        }
        for( $i = 0; $i < $stop ; $i++ )
        {
            $row = $photos->fetch_row() ;
            if( !$row )
            {
                $i = 80 ;
            }
            else
            {
                $file_path = $album_name[1]."/".$row[2] ;    //provides the path for the image source
                echo "<img value='".$i."' src='".$file_path."'>" ; //assigns the value for use in the jQuery scripts
                echo "<script type='text/javascript'> picture_list[ ".$i." ] = '".$file_path."'</script>" ;  //sends the values to the jQuery script
            }
        }
        echo "</div></div>" ;
    }
        $photos->free();
        $db->close();
}
catch( Exception $error )
{
    echo $error ;
}
}

【问题讨论】:

  • 你能设置一个小提琴,复制问题吗?

标签: javascript php jquery html arrays


【解决方案1】:

我想这对您来说已经晚了,但也可能对其他人有所帮助。您可以通过 if 语句将其删除,如果图像在那里,则保留按钮,否则将其删除。 首先,为.next.back 添加属性style="display:none;",当您在php 中输出它们时。然后在 jquery 中,执行此操作。

  if (newSRC!=='') {
      $(".next,.back").show();
  }else {
     $(".next").hide();
     $(".back").hide();
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2013-12-12
    • 2017-04-28
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    相关资源
    最近更新 更多