简体   繁体   English

如何在同一页面上添加多个jssor实例?

[英]How can I add multiple jssor instances on the same page?

Is it possible to add multiple jssor instances on one page? 是否可以在一页上添加多个jssor实例? 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. jssor_slider1变量必须是动态的。 Means the 1 has to changed to a variable itself. 表示1本身必须更改为变量。 var jssor_slider1+VARIABLE . var jssor_slider1+VARIABLE But I found nothing for this. 但是我对此一无所获。

Maybe I make thinking mistake. 也许我想错了。

Thanks 谢谢

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);

Please initialize multiple instances in following manner, 请按照以下方式初始化多个实例,

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

One setting for all sliders without jQuery: 所有没有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: 所有没有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>

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... 我正在使用php从mysql数据库中获取信息,它将建立几个容器,这些容器的末尾都包含一个滑块(使用文件夹中的图片动态生成-因此,我什至不知道有多少个滑块。但是它们都具有相同的大小/选项...因此,我找到了一种快速,肮脏和有效的方法来进行处理...

I loaded all the original javascript with 1 file, it's called sliders.js 我用1个文件加载了所有原始javascript,这个文件称为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. 它具有所有原始内容,如下所示,唯一的区别是,我在滑块上使用了一个类,并使用了唯一的ID(由PHP / MySQL设置)-请参阅我的评论。

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. 因此,当我用PHP加载HTML时,其所有原始HTML-除了ID是根据我从数据库获得的信息的ID(根据其唯一性来进行动态设置)外,在我的情况下,ID类似于“ GameID-2784”等等

<!-- 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. 如前所述,我知道这样做是一种肮脏的方式,但是如果您使用带有相同选项的许多滑块,并且只想给它们一个“类”,那么这非常简单,并且效果很好。更重要的是,不知道ID(因此您可以将其添加到Java脚本中。

Would be happy to hear if anyone has a simpler solution! 如果有人有更简单的解决方案,我们将非常高兴!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM