How can I add multiple jssor instances on the same

2019-08-15 17:47发布

问题:

Is it possible to add multiple jssor instances on one page? Is it possible to create new class instances dynamically like:

var jssor_slider1 = new $JssorSlider$("slider1_container", options);

The jssor_slider1 variable has to be dynamic. Means the 1 has to changed to a variable itself. var jssor_slider1+VARIABLE. But I found nothing for this.

Maybe I make thinking mistake.

Thanks

回答1:

Removing the number(s) after "options" & changing the double quotes to single should allow you to initialize multiple sliders on the same page:

var jssor_slider1 = new $JssorSlider$('slider1_container', options);
var jssor_slider2 = new $JssorSlider$('slider2_container', options);


回答2:

Please initialize multiple instances in following manner,

var jssor_slider1 = new $JssorSlider$("slider1_container", options1);
var jssor_slider2 = new $JssorSlider$("slider2_container", options2);


回答3:

One setting for all sliders without jQuery:

    <script>
    jssor_slider1_starter = function (containerId) {
        ...
    };
</script>

<div id="slider1_container" style="position:relative;top:0px;left:0px;width:600px;height:300px">
    ...
    <script>
        jssor_slider1_starter('slider1_container');
    </script>
</div>

<div id="slider2_container" style="position:relative;top:0px;left:0px;width:600px;height:300px">
    ...
    <script>
        jssor_slider1_starter('slider2_container');
    </script>
</div>

Different settings for all sliders without jQuery:

    <script>
    jssor_slider1_starter = function (containerId) {
        ...
    };
    jssor_slider2_starter = function (containerId) {
        ...
    };
</script>

<div id="slider1_container" style="position:relative;top:0px;left:0px;width:600px;height:300px">
    ...
    <script>
        jssor_slider1_starter('slider1_container');
    </script>
</div>

<div id="slider2_container" style="position:relative;top:0px;left:0px;width:600px;height:300px">
    ...
    <script>
        jssor_slider2_starter('slider2_container');
    </script>
</div>


回答4:

I am using php to get information from a mysql database, and it will build several containers which at the end of them all include a slider (dynamically built with images from a folder - So, I don't even know how many sliders. But they will all have the same size/options ... Hence I found a quick, dirty and effective way to work with that...

I loaded all the original javascript with 1 file, it's called sliders.js

It has all the original content as seen below, only difference is I use a class on my sliders and a unique ID (set by PHP/MySQL) - See where I commented.

jQuery(document).ready(function ($){

     //--------------------HERE I ADD AN EACH FUNCTION-------------------
     //------------------------------------------------------------------

     $(".jsslider").each(function(index){
            var SliderID = $(this).attr('id');

     // ---- Just for my own reference while troubleshooting ----
     // console.log("Index: " + index + " | Created Slider with ID: " + SliderID );     

            var jssor_1_SlideshowTransitions = [
              {$Duration:1200,x:0.3,$During:{$Left:[0.3,0.7]},$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:-0.3,$SlideOut:true,$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:-0.3,$During:{$Left:[0.3,0.7]},$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:0.3,$SlideOut:true,$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:0.3,$During:{$Top:[0.3,0.7]},$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:-0.3,$SlideOut:true,$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:-0.3,$During:{$Top:[0.3,0.7]},$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:0.3,$SlideOut:true,$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:0.3,$Cols:2,$During:{$Left:[0.3,0.7]},$ChessMode:{$Column:3},$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:0.3,$Cols:2,$SlideOut:true,$ChessMode:{$Column:3},$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:0.3,$Rows:2,$During:{$Top:[0.3,0.7]},$ChessMode:{$Row:12},$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:0.3,$Rows:2,$SlideOut:true,$ChessMode:{$Row:12},$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:0.3,$Cols:2,$During:{$Top:[0.3,0.7]},$ChessMode:{$Column:12},$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,y:-0.3,$Cols:2,$SlideOut:true,$ChessMode:{$Column:12},$Easing:{$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:0.3,$Rows:2,$During:{$Left:[0.3,0.7]},$ChessMode:{$Row:3},$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:-0.3,$Rows:2,$SlideOut:true,$ChessMode:{$Row:3},$Easing:{$Left:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:0.3,y:0.3,$Cols:2,$Rows:2,$During:{$Left:[0.3,0.7],$Top:[0.3,0.7]},$ChessMode:{$Column:3,$Row:12},$Easing:{$Left:$Jease$.$InCubic,$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,x:0.3,y:0.3,$Cols:2,$Rows:2,$During:{$Left:[0.3,0.7],$Top:[0.3,0.7]},$SlideOut:true,$ChessMode:{$Column:3,$Row:12},$Easing:{$Left:$Jease$.$InCubic,$Top:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,$Delay:20,$Clip:3,$Assembly:260,$Easing:{$Clip:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,$Delay:20,$Clip:3,$SlideOut:true,$Assembly:260,$Easing:{$Clip:$Jease$.$OutCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,$Delay:20,$Clip:12,$Assembly:260,$Easing:{$Clip:$Jease$.$InCubic,$Opacity:$Jease$.$Linear},$Opacity:2},
              {$Duration:1200,$Delay:20,$Clip:12,$SlideOut:true,$Assembly:260,$Easing:{$Clip:$Jease$.$OutCubic,$Opacity:$Jease$.$Linear},$Opacity:2}
            ];

            var jssor_1_options = {
              $FillMode: 5,
              $AutoPlay: false,
              $SlideshowOptions: {
                $Class: $JssorSlideshowRunner$,
                $Transitions: jssor_1_SlideshowTransitions,
                $TransitionsOrder: 1
              },
              $ArrowNavigatorOptions: {
                $Class: $JssorArrowNavigator$
              },
              $ThumbnailNavigatorOptions: {
                $Class: $JssorThumbnailNavigator$,
                $Cols: 10,
                $SpacingX: 8,
                $SpacingY: 8,
                $Align: 360
              }
            };

     //-----------------HERE IS WHERE THE MAGIC HAPPENS--------------
        var jssor_1_slider = new $JssorSlider$(SliderID, jssor_1_options);
     //--------------------------------------------------------------   

        function ScaleSlider() {
            var refSize = jssor_1_slider.$Elmt.parentNode.clientWidth;
            if (refSize) {
                refSize = Math.min(refSize, 800);
                jssor_1_slider.$ScaleWidth(refSize);
            }
            else {
                window.setTimeout(ScaleSlider, 30);
            }
        }
        ScaleSlider();
        $(window).bind("load", ScaleSlider);
        $(window).bind("resize", ScaleSlider);
        $(window).bind("orientationchange", ScaleSlider);
    }); 
});

So, when I load my HTML with PHP, its all original HTML - except the ID is dyanmiacally set depending on the ID of the information I get from database which keeps it unique, in my case the ID's are something like "GameID-2784" etc.

<!-- SLIDER -->
<div class="jsslider" id="'.$g_id.'" style="position: relative; margin: 0 auto; top: 0px; left: 0px; width: 608px; height: 456px; overflow: hidden; visibility: hidden; background-color: #000;">
   <div data-u="loading" style="position: absolute; top: 0px; left: 0px;">
   <div style="filter: alpha(opacity=70); opacity: 0.7; position: absolute; display: block; top: 0px; left: 0px; width: 100%; height: 100%;"></div>

..........

As mentioned, I know it's a dirty way of doing it, But it's very easy and works real good if you're using a lot of sliders with the same options, and you simply just want to give them a 'class' --- and more importantly, do not know the ID's (so you can add them to the java-script.

Would be happy to hear if anyone has a simpler solution!