我正在尝试创建一个动态jssor嵌套滑块,它将显示来自mySQL数据库的相册和相关图像。现在,它只与一张专辑完美配合,但当数据库中存在两张或更多张专辑时,它无法正确显示。我是JavaScript和JQuery的新手。以下是我的代码。
<div id="slider1_container" style="position: relative; top: 0px; left: 0px; width: 810px; height: 600px; background: #24262e; overflow: hidden; ">
<!-- Loading Screen -->
<div u="loading" style="position: absolute; top: 0px; left: 0px;">
<div style="filter: alpha(opacity=70); opacity:0.7; position: absolute; display: block;
background-color: #000; top: 0px; left: 0px;width: 100%;height:100%;">
</div>
<div style="position: absolute; display: block; background: url(img/loading.gif) no-repeat center center;
top: 0px; left: 0px;width: 100%;height:100%;">
</div>
</div>
<!-- Slides Container -->
<div u="slides" style="cursor: move; position: absolute; left: 210px; top: 0px; width: 600px; height: 600px;
overflow: hidden;">
<!-- beginning of PHP Loop -->
<?php
$q = mysql_query("select * from album");
while($albumRes = mysql_fetch_array($q))
{
$i = 1;
$divId = "sliderh" . $i . "_container";
$divClass = "sliderh" . $i;
?>
<div>
<div id="<?php echo $divId ?>" class="<?php echo $divClass ?>" style="position: relative; top: 0px; left: 0px; width: 600px;
height: 600px;">
<!-- Slides Container -->
<div u="slides" style="cursor: move; position: absolute; left: 0px; top: 0px; width: 600px; height: 500px;
overflow: hidden;">
<!-- Begin images loop -->
<?php
$img_query = mysql_query("select * from gallery_images where IMG_AB_ID='$albumRes[0]'");
while($imageRes = mysql_fetch_array($img_query))
{
?>
<div>
<img u="image" src="<?php echo fix_directory($imageRes[2]) ?>" />
<img u="thumb" src="<?php echo fix_directory($imageRes[2]) ?>" />
</div>
<?php } ?>
<!-- End images loop -->
</div>
<!-- thumbnail navigator container (bottom row thumbnails)-->
<div u="thumbnavigator" class="jssort05" style="left: 0px; bottom: 0px;">
<!-- Thumbnail Item Skin Begin -->
<div u="slides" style="cursor: default;">
<div u="prototype" class="p">
<div class="o">
<div u="thumbnailtemplate" class="b"></div>
<div class="i"></div>
<div u="thumbnailtemplate" class="f"></div>
</div>
</div>
</div>
<!-- Thumbnail Item Skin End -->
</div>
<!--#endregion Bullet Navigator Skin End -->
</div>
<div u="thumb">
<img src="<?php echo fix_directory($albumRes[3]) ?>" />
<div class="title_back"></div>
<div class="title">
<?php echo $albumRes[1]?>
</div>
</div>
</div>
<!-- End of Album Loop -->
<?php
$i = $i + 1;
}
?>
</div> <!--#region Thumbnail Navigator Skin Begin -->
<!-- Help: http://www.jssor.com/development/slider-with-thumbnail-navigator-jquery.html -->
<!-- thumbnail navigator container -->
<div u="thumbnavigator" class="jssort16" style="left: 0px; top: 0px;">
<!-- Thumbnail Item Skin Begin -->
<div u="slides" style="cursor: default;">
<div u="prototype" class=p>
<div u="thumbnailtemplate" class="t"></div>
</div>
</div>
<!-- Thumbnail Item Skin End -->
</div>
<!--#endregion Thumbnail Navigator Skin End
<a style="display: none" href="http://www.jssor.com">Bootstrap Slider</a>-->
<!-- Trigger -->
<script>
jssor_slider1_starter('slider1_container');
</script>
</div>
</div>
<!-- Jssor Slider End -->
<?php
} //end else statement for page 2 (gallery page)
?>
这是脚本代码:
<script>
// get number of albums from DB and PHP
var div = document.getElementById("dom-target");
var numAlbums = div.textContent;
// generate array for each function based on
var sliderArray = [];
for(var i = 0; i < numAlbums; i++)
{
// add first container
if(i === 0)
{
sliderArray.push("sliderh1_container");
}
else // add any existing containers for albums after that
{
var num = i + 1;
var n = num.toString();
sliderArray.push("sliderh" + n + "_container");
}
// output to console for testing purposes
console.log(sliderArray);
}
jssor_slider1_starter = function (containerId) {
var nestedSliders = [];
$Jssor$.$Each(sliderArray, function (containerId) {
var nestedSliderOptions = {
$PauseOnHover: 1, //[Optional] Whether to pause when mouse over if a slider is auto playing, 0 no pause, 1 pause for desktop, 2 pause for touch device, 3 pause for desktop and touch device, 4 freeze for desktop, 8 freeze for touch device, 12 freeze for desktop and touch device, default value is 1
$SlideDuration: 500, //[Optional] Specifies default duration (swipe) for slide in milliseconds, default value is 500
$MinDragOffsetToSlide: 20, //[Optional] Minimum drag offset to trigger slide , default value is 20
//$SlideWidth: 200, //[Optional] Width of every slide in pixels, default value is width of 'slides' container
//$SlideHeight: 150, //[Optional] Height of every slide in pixels, default value is height of 'slides' container
$SlideSpacing: 3, //[Optional] Space between each slide in pixels, default value is 0
$Cols: 1, //[Optional] Number of pieces to display (the slideshow would be disabled if the value is set to greater than 1), the default value is 1
$Align: 0, //[Optional] The offset position to park slide (this options applys only when slideshow disabled), default value is 0.
$UISearchMode: 0, //[Optional] The way (0 parellel, 1 recursive, default value is 1) to search UI components (slides container, loading screen, navigator container, arrow navigator container, thumbnail navigator container etc).
$ThumbnailNavigatorOptions: { //[Optional] Options to specify and enable thumbnail navigator or not
$Class: $JssorThumbnailNavigator$, //[Required] Class to create thumbnail navigator instance
$ChanceToShow: 2, //[Required] 0 Never, 1 Mouse Over, 2 Always
$ActionMode: 1, //[Optional] 0 None, 1 act by click, 2 act by mouse hover, 3 both, default value is 1
$SpacingX: 8, //[Optional] Horizontal space between each thumbnail in pixel, default value is 0
$Cols: 10, //[Optional] Number of pieces to display, default value is 1
$Align: 360 //[Optional] The offset position to park thumbnail
}
};
nestedSliders.push(new $JssorSlider$(containerId, nestedSliderOptions));
});
var options = {
$AutoPlay: false, //[Optional] Whether to auto play, to enable slideshow, this option must be set to true, default value is false
$AutoPlaySteps: 1, //[Optional] Steps to go for each navigation request (this options applys only when slideshow disabled), the default value is 1
$AutoPlayInterval: 2000, //[Optional] Interval (in milliseconds) to go for next slide since the previous stopped if the slider is auto playing, default value is 3000
$PauseOnHover: 1, //[Optional] Whether to pause when mouse over if a slider is auto playing, 0 no pause, 1 pause for desktop, 2 pause for touch device, 3 pause for desktop and touch device, 4 freeze for desktop, 8 freeze for touch device, 12 freeze for desktop and touch device, default value is 1
$ArrowKeyNavigation: true, //[Optional] Allows keyboard (arrow key) navigation or not, default value is false
$SlideDuration: 300, //[Optional] Specifies default duration (swipe) for slide in milliseconds, default value is 500
$MinDragOffsetToSlide: 80, //[Optional] Minimum drag offset to trigger slide , default value is 20
//$SlideWidth: 600, //[Optional] Width of every slide in pixels, default value is width of 'slides' container
//$SlideHeight: 150, //[Optional] Height of every slide in pixels, default value is height of 'slides' container
$SlideSpacing: 3, //[Optional] Space between each slide in pixels, default value is 0
$Cols: 1, //[Optional] Number of pieces to display (the slideshow would be disabled if the value is set to greater than 1), the default value is 1
$Align: 0, //[Optional] The offset position to park slide (this options applys only when slideshow disabled), default value is 0.
$UISearchMode: 0, //[Optional] The way (0 parellel, 1 recursive, default value is 1) to search UI components (slides container, loading screen, navigator container, arrow navigator container, thumbnail navigator container etc).
$PlayOrientation: 2, //[Optional] Orientation to play slide (for auto play, navigation), 1 horizental, 2 vertical, 5 horizental reverse, 6 vertical reverse, default value is 1
$DragOrientation: 0, //[Optional] Orientation to drag slide, 0 no drag, 1 horizental, 2 vertical, 3 either, default value is 1 (Note that the $DragOrientation should be the same as $PlayOrientation when $Cols is greater than 1, or parking position is not 0),
$ThumbnailNavigatorOptions: {
$Class: $JssorThumbnailNavigator$, //[Required] Class to create thumbnail navigator instance
$ChanceToShow: 2, //[Required] 0 Never, 1 Mouse Over, 2 Always
$ActionMode: 1, //[Optional] 0 None, 1 act by click, 2 act by mouse hover, 3 both, default value is 1
$AutoCenter: 3, //[Optional] Auto center thumbnail items in the thumbnail navigator container, 0 None, 1 Horizontal, 2 Vertical, 3 Both, default value is 3
$Rows: 1, //[Optional] Specify lanes to arrange thumbnails, default value is 1
$SpacingX: 10, //[Optional] Horizontal space between each thumbnail in pixel, default value is 0
$SpacingY: 10, //[Optional] Vertical space between each thumbnail in pixel, default value is 0
$Cols: 3, //[Optional] Number of pieces to display, default value is 1
$Align: 0, //[Optional] The offset position to park thumbnail
$Orientation: 2, //[Optional] Orientation to arrange thumbnails, 1 horizental, 2 vertical, default value is 1
$NoDrag: false //[Optional] Disable drag or not, default value is false
}
};
var jssor_slider1 = new $JssorSlider$(containerId, options);
function OnMainSliderPark(currentIndex, fromIndex) {
$Jssor$.$Each(nestedSliders, function (nestedSlider) {
nestedSlider.$Pause();
});
setTimeout(function () {
nestedSliders[currentIndex].$Play();
}, 2000);
}
jssor_slider1.$On($JssorSlider$.$EVT_PARK, OnMainSliderPark);
OnMainSliderPark(0, 0);
//responsive code begin
//you can remove responsive code if you don't want the slider scales while window resizes
function ScaleSlider() {
var parentWidth = jssor_slider1.$Elmt.parentNode.clientWidth;
if (parentWidth)
jssor_slider1.$ScaleWidth(Math.min(parentWidth, 800));
else
$Jssor$.$Delay(ScaleSlider, 30);
}
ScaleSlider();
$Jssor$.$AddEvent(window, "load", ScaleSlider);
$Jssor$.$AddEvent(window, "resize", $Jssor$.$WindowResizeFilter(window, ScaleSlider));
$Jssor$.$AddEvent(window, "orientationchange", ScaleSlider);
//responsive code end
};
</script>
答案 0 :(得分:0)
嗯......我想这是业余时间,但我发现也许我不应该在while循环中声明计数器变量。案件结案。