jssor滑块 - 一页上有许多滑块

时间:2015-05-06 22:20:16

标签: javascript jssor

我看到很多帖子用于在页面上放置jssor滑块的多个实例。但是,我想动态添加多个滑块,具体取决于PHP查询和CMS中的内容。因此,页面必须准备好处理2或20个滑块......或更多。

stackoverflow上有很多帖子显示我如何添加两个滑块如下...

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

...然后在我参考滑块时参考每个ID(即slider2_container)。但是,如果我有20个滑块(或更多),我不希望有20个不同的滑块线(或者如果超过20个则担心更多)。我更喜欢为普通滑块提供一些东西...我不写javascript,但是类似的东西(我知道这是错的,但*下面指的是任何#)......

var jssor_slider* = new $JssorSlider$("slider*_container", options);

我看过这篇帖子...... Jssor non-jquery slider dynamic amount multiple slider on one page ......但是没有答案。我将使用CMS中的信息(我使用图库自定义)填充页面和滑块。

而且,正如我所提到的,我已经看过很多其他2或3个滑块的帖子......但没有什么可以处理未知数字。请告诉我。

<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/js/jssor.js"></script>
<script type="text/javascript" src="/js/jssor.slider.js"></script>
<script>
        jQuery(document).ready(function ($) {

            var _CaptionTransitions = [];
            _CaptionTransitions["L"] = { $Duration: 900, x: 0.6, $Easing: { $Left: $JssorEasing$.$EaseInOutSine }, $Opacity: 2 };
            _CaptionTransitions["R"] = { $Duration: 900, x: -0.6, $Easing: { $Left: $JssorEasing$.$EaseInOutSine }, $Opacity: 2 };
            _CaptionTransitions["T"] = { $Duration: 900, y: 0.6, $Easing: { $Top: $JssorEasing$.$EaseInOutSine }, $Opacity: 2 };
            _CaptionTransitions["B"] = { $Duration: 900, y: -0.6, $Easing: { $Top: $JssorEasing$.$EaseInOutSine }, $Opacity: 2 };
            _CaptionTransitions["ZMF|10"] = { $Duration: 900, $Zoom: 11, $Easing: { $Zoom: $JssorEasing$.$EaseOutQuad, $Opacity: $JssorEasing$.$EaseLinear }, $Opacity: 2 };
            _CaptionTransitions["RTT|10"] = { $Duration: 900, $Zoom: 11, $Rotate: 1, $Easing: { $Zoom: $JssorEasing$.$EaseOutQuad, $Opacity: $JssorEasing$.$EaseLinear, $Rotate: $JssorEasing$.$EaseInExpo }, $Opacity: 2, $Round: { $Rotate: 0.8} };
            _CaptionTransitions["RTT|2"] = { $Duration: 900, $Zoom: 3, $Rotate: 1, $Easing: { $Zoom: $JssorEasing$.$EaseInQuad, $Opacity: $JssorEasing$.$EaseLinear, $Rotate: $JssorEasing$.$EaseInQuad }, $Opacity: 2, $Round: { $Rotate: 0.5} };
            _CaptionTransitions["RTTL|BR"] = { $Duration: 900, x: -0.6, y: -0.6, $Zoom: 11, $Rotate: 1, $Easing: { $Left: $JssorEasing$.$EaseInCubic, $Top: $JssorEasing$.$EaseInCubic, $Zoom: $JssorEasing$.$EaseInCubic, $Opacity: $JssorEasing$.$EaseLinear, $Rotate: $JssorEasing$.$EaseInCubic }, $Opacity: 2, $Round: { $Rotate: 0.8} };
            _CaptionTransitions["CLIP|LR"] = { $Duration: 900, $Clip: 15, $Easing: { $Clip: $JssorEasing$.$EaseInOutCubic }, $Opacity: 2 };
            _CaptionTransitions["MCLIP|L"] = { $Duration: 900, $Clip: 1, $Move: true, $Easing: { $Clip: $JssorEasing$.$EaseInOutCubic} };
            _CaptionTransitions["MCLIP|R"] = { $Duration: 900, $Clip: 2, $Move: true, $Easing: { $Clip: $JssorEasing$.$EaseInOutCubic} };

            var options = {
                $FillMode: 2, //[Opt] The way to fill image in slide, 0 stretch, 1 contain (keep aspect ratio and put all inside slide), 2 cover (keep aspect ratio and cover whole slide), 4 actual size, 5 contain for large image, actual size for small image, default=0
                $AutoPlay: true, //[Opt] Whether to auto play, this option must be set to true, default=false
                $AutoPlayInterval: 4000, //[Opt] Interval (in milliseconds) to go for next slide since the previous stopped if the slider is auto playing, default=3000
                $PauseOnHover: 1, //[Opt] 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=1

                $ArrowKeyNavigation: true, //[Opt] Allows keyboard (arrow key) navigation or not, default value is false
                $SlideEasing: $JssorEasing$.$EaseOutQuint, //[Opt] Specifies easing for right to left animation, default value is $JssorEasing$.$EaseOutQuad
                $SlideDuration: 1200, //[Opt] Duration (swipe) for slide in milliseconds, default value is 500
                $MinDragOffsetToSlide: 20, //[Opt] Minimum drag offset to trigger slide , default value is 20
                //$SlideWidth: 600, //[Opt] Width of every slide in pixels, default is width of 'slides' container
                //$SlideHeight: 300, //[Opt] Height of every slide in pixels, default is height of 'slides' container
                $SlideSpacing: 0, //[Opt] Space between each slide in pixels, default=0
                $DisplayPieces: 1, //[Opt] Number of pieces to display (the slideshow would be disabled if the value is set to greater than 1), the default=1
                $ParkingPosition: 0, //[Opt] The offset position to park slide (this options applys only when slideshow disabled), default=0.
                $UISearchMode: 1, //[Opt] 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: 1, //[Opt] Orientation to play slide (for auto play, navigation), 1 horizental, 2 vertical, 5 horizental reverse, 6 vertical reverse, default value is 1
                $DragOrientation: 1, //[Opt] 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 $DisplayPieces is greater than 1, or parking position is not 0)

                $CaptionSliderOptions: { //[Opt] Options which specifies how to animate caption
                    $Class: $JssorCaptionSlider$, //[Req] Class to create instance to animate caption
                    $CaptionTransitions: _CaptionTransitions, //[Req] An array of caption transitions to play caption, see caption transition section at jssor slideshow transition builder
                    $PlayInMode: 1, //[Opt] 0 None (no play), 1 Chain (goes after main slide), 3 Chain Flatten (goes after main slide and flatten all caption animations), default=1
                    $PlayOutMode: 3 //[Opt] 0 None (no play), 1 Chain (goes before main slide), 3 Chain Flatten (goes before main slide and flatten all caption animations), default=1
                },

                $BulletNavigatorOptions: { //[Opt] Options to specify and enable navigator or not
                    $Class: $JssorBulletNavigator$, //[Req] Class to create navigator instance
                    $ChanceToShow: 2, //[Req] 0 Never, 1 Mouse Over, 2 Always
                    $AutoCenter: 1, //[Opt] Auto center navigator, 0 None, 1 Horizontal, 2 Vertical, 3 Both, default=0
                    $Steps: 1, //[Opt] Steps to go for each nav request, default=1
                    $Lanes: 1, //[Opt] lanes to arrange items, default=1
                    $SpacingX: 8, //[Opt] Horizontal space between each item in pixel, default=0
                    $SpacingY: 8, //[Opt] Vertical space between each item in pixel, default=0
                    $Orientation: 1 //[Opt] Orientation of the nav, 1 horizontal, 2 vertical, default=1
                },

                $ArrowNavigatorOptions: { //[Opt] Options to specify and enable arrow navigator or not
                    $Class: $JssorArrowNavigator$, //[Requried] Class to create arrow navigator instance
                    $ChanceToShow: 1, //[Req] 0 Never, 1 Mouse Over, 2 Always
                    $AutoCenter: 2, //[Opt] Auto center, 0 No, 1 Horizontal, 2 Vertical, 3 Both, default=0
                    $Steps: 1 //[Opt] Steps to go for each navigation request, default=1
                }
            };

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

            //responsive code begin
            //you can remove responsive code if you don't want the slider scales while window resizes
            function ScaleSlider() {
                var bodyWidth = document.body.clientWidth;
                if (bodyWidth)
                    jssor_slider1.$ScaleWidth(Math.min(bodyWidth, 1280));
                else
                    window.setTimeout(ScaleSlider, 30);
            }
            ScaleSlider();

            $(window).bind("load", ScaleSlider);
            $(window).bind("resize", ScaleSlider);
            $(window).bind("orientationchange", ScaleSlider);
            //responsive code end
        });
    </script>

2 个答案:

答案 0 :(得分:0)

除了将id作为变量传递给初始jssor滑块之外,您还可以将元素作为变量传递。因此,以下两种方法都是合法的。

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

var containerElement = document.getElementById("slider1_container");
var jssor_slider1 = new $JssorSlider$(containerElement , options);

通过这种方式,如果你有20个滑块元素,你可以按如下方式初始化它,

var containerElements = [...];   //slider container elements in an array
for(var i=0; i<containerElements.length; i++) {
    new $JssorSlider(containerElements[i], options);
}

答案 1 :(得分:0)

由于没有任何帮助(并且人们停止回答),我决定从jssor滑块切换到bxslider,我发现它更容易配置和自定义。