简体   繁体   English

jssor滑块不响应导航箭头

[英]jssor slider not responding to nav arrows

I am building a jssor slider dynamically using javascript. 我正在使用javascript动态构建jssor滑块。 The slider is based on the jssor 'carousel' demo. 滑块基于jssor“轮播”演示。 Everything looks correct when displayed, including the navigation arrows at either end of the slider. 显示时一切看起来正确,包括滑块任一端的导航箭头。 It will also respond correctly when I swipe left or right with the mouse. 当我用鼠标向左或向右滑动时,它也会正确响应。 The slider, however, does not respond to any clicks on the nav arrows. 但是,滑块不响应导航箭头上的任何单击。

One problem I have seen mentioned in other postings is improper nesting of the nav arrows. 我在其他帖子中提到的一个问题是导航箭头的嵌套不正确。 I don't think that is the problem here. 我认为这不是问题所在。 Any help would be most appreciated. 非常感激任何的帮助。

Here is the javascript that builds the slider: 这是构建滑块的javascript:

var CpxRowSlider = function(callback) {
var callback;
var indexedImgEl = new Array();
var DEFAULT_H = 110;
var DEFAULT_W = 260;
var outerDivEl = document.createElement('div');
var title = true;
var addNavArrows = true;

var sliderDivEl = null;
var sliderId = "slider_SOLO";

/*
 * image set should be an array of key-value pairs where the value is the
 * URL of an image and the key will be used as a tag that identifies the
 * image in any callback functions.
 */
function create(imageSet) {
    /* Slides Container -- */
    var rowDivEl = document.createElement('div');
    $(rowDivEl).attr("u", "slides");
    $(rowDivEl).css({
        "cursor" : "move",
        "position" : "absolute",
        "left" : "0px",
        "top" : "0px",
        "width" : "780px",
        "height" : "110px",
        "overflow" : "hidden"
    });
    for ( var key in imageSet) {
        var imgUrl = imageSet[key];
        var imgDivEl = document.createElement('div');
        var imgEl = document.createElement('img');
        imgEl.src = imgUrl;
        $(imgEl).attr("u", "image");
        imgDivEl.appendChild(imgEl);
        // add to row
        rowDivEl.appendChild(imgDivEl);
        $(imgEl).data("cpxKey", key);
        /*
         * actions associated with the img....
         */
        /*
         * click event gets handed off via callback
         */
        if ((callback != undefined) && (callback != null)) {
            imgEl.onclick = function(evt) {
                var targetImg = evt.target;
                trace("Click on " + $(targetImg).data("cpxKey"));
                callback($(targetImg).data("cpxKey"));
            };
        }
    }
    // add slides to a slider...
    sliderDivEl = document.createElement('div');
    sliderDivEl.id = sliderId

    $(sliderDivEl).css({
        "position" : "relative",
        "left" : "0px",
        "top" : "0px",
        "width" : "780px",
        "height" : "110px"
    });
    // add to DOM
    sliderDivEl.appendChild(rowDivEl);
    if (addNavArrows) {
        sliderDivEl.appendChild(createNavArrow(true));
        sliderDivEl.appendChild(createNavArrow(false));
    }
    outerDivEl.appendChild(sliderDivEl);
}

function createNavArrow(toLeft) {
    var arrowSpan = document.createElement('span');
    $(arrowSpan).attr("u", "image");
    $(arrowSpan).css({
        "top" : "30px",
        "width" : "55px",
        "height" : "55px"
    });
    if (toLeft) {
        $(arrowSpan).css({
            "left" : "5px"
        });

        $(arrowSpan).addClass("jssora03l");
    } else {
        $(arrowSpan).css({
            "right" : "5px"
        });
        $(arrowSpan).addClass("jssora03r");
    }
    return arrowSpan;
}

/*
 * Invoked only AFTER the slider has been added to the DOM
 */
function finalize() {
    // add to sliders being controlled by jssor
    var sliderOptions = getSliderOptions();
    var jssor_sliderh = new $JssorSlider$(sliderId, sliderOptions);
}

function getSliderOptions() {
    var sliderhOptions = {
        /*
         * $AutoPlay [Optional] Whether to auto play, to enable slideshow,
         * this option must be set to true, default value is false
         */
        $AutoPlay : false,
        /*
         * $PauseOnHover [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, default
         * value is 1
         */
        $PauseOnHover : 1,
        /*
         * $AutoPlaySteps [Optional] Steps to go for each navigation request
         * (this options applys only when slideshow disabled), the default
         * value is 1
         */
        $AutoPlaySteps : 2,
        /*
         * $ArrowKeyNavigation [Optional] Allows keyboard (arrow key)
         * navigation or not, default value is false
         */
        // $ArrowKeyNavigation : true,
        /*
         * [Optional] Specifies default duration (swipe) for slide in
         * milliseconds, default value is 500
         */
        $SlideDuration : 300,
        /*
         * [Optional] Minimum drag offset to trigger slide , default value
         * is 20
         */
        $MinDragOffsetToSlide : 20,
        /*
         * [Optional] Width of every slide in pixels, default value is width
         * of 'slides' container
         */
        $SlideWidth : DEFAULT_W,
        /*
         * [Optional] Height of every slide in pixels, default value is
         * height of 'slides' container
         */
        // $SlideHeight: 150,
        /*
         * [Optional] Space between each slide in pixels, default value is 0
         */
        $SlideSpacing : 3,
        /*
         * [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
         */
        $DisplayPieces : 3,
        /*
         * [Optional] The offset position to park slide (this options applys
         * only when slideshow disabled), default value is 0.
         */
        $ParkingPosition : 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).
         */
        $UISearchMode : 0,
        // ...................................
        // [Optional] Options to specify and enable navigator or not
        $BulletNavigatorOptions : {
            $Class : $JssorBulletNavigator$, // [Required] Class to
            // create navigator instance
            $ChanceToShow : 1, // [Required] 0 Never, 1 Mouse Over, 2
            // Always
            $AutoCenter : 0, // [Optional] Auto center navigator in
            // parent container, 0 None, 1 Horizontal, 2
            // Vertical, 3 Both, default value is 0
            $Steps : 1, // [Optional] Steps to go for each navigation
            // request, default value is 1
            $Lanes : 1, // [Optional] Specify lanes to arrange items,
            // default value is 1
            $SpacingX : 0, // [Optional] Horizontal space between each item
            // in pixel, default value is 0
            $SpacingY : 0, // [Optional] Vertical space between each item
            // in pixel, default value is 0
            $Orientation : 1
        // [Optional] The orientation of the navigator, 1 horizontal, 2
        // vertical, default value is 1
        }
    }
    return sliderhOptions;
}

function trace(msg) {
    console.log("CpxRowSlider: " + msg);
}

return {
    create : create,
    finalize : finalize,
    getContainer : function() {
        return outerDivEl;
    }
};
};

UPDATE: Well, some digging around turned up the "where" but the "why" is still a mystery. 更新:好吧,有些挖掘发现了“哪里”,但是“为什么”仍然是个谜。 The generated HTML should look something like this (minus the "styles" for clarity): 生成的HTML应该看起来像这样(为清楚起见,减去“样式”):

<div id="slider_SOLO" >      
        <div u="slides">
            <div>
                <img u="image" src="../foo1.jpg" />
            </div>
            <div>
                <img u="image" src="../foo2.jpg" />
            </div>
        <div>
        <span u="arrowleft" class="jssora03l" ></span>
        <span u="arrowright" class="jssora03r"></span>
</div>

The problem is that in the actual HTML page there are TWO elements with the attribute u="slides". 问题在于,在实际的HTML页面中,有两个元素的属性为u =“ slides”。 The 2nd is the correct one but just before it is another empty div. 第二个是正确的,但在它之前是另一个空div。 In other words, the DOM I see when I examine the displayed HTML is more like: 换句话说,当我检查显示的HTML时看到的DOM更像是:

<div id="slider_SOLO" >     
        <div u="slides"></div>   
        <div u="slides">
            <div>
                <img u="image" src="../foo1.jpg" />
            </div>
            <div>
                <img u="image" src="../foo2.jpg" />
            </div>
        <div>
        <span u="arrowleft" class="jssora03l" ></span>
        <span u="arrowright" class="jssora03r"></span>
</div>

If I add a similar div to the jssor carousel demo HTML, I get the same behavior (ie, navigation arrows no longer work). 如果将类似的div添加到jssor轮播演示HTML中,则会得到相同的行为(即,导航箭头不再起作用)。

The problem is that the error (ie, the extra DIV) is not inserted by my javascript. 问题是我的JavaScript没有插入错误(即额外的DIV)。 Rather it seems to be linked to the call var jssor_sliderh = new $JssorSlider$(sliderId, sliderOptions); 相反,它似乎与调用var jssor_sliderh = new $JssorSlider$(sliderId, sliderOptions);

I am wondering if the cause is something in the options I pass in. 我想知道原因是否在我传递的选项中。

Found it! 找到了! The root cause was indeed my options but not in the way I thought. 根本原因确实是我的选择,而不是我的想法。 I was missing the $ArrowNavigatorOptions. 我错过了$ ArrowNavigatorOptions。 The mystery DIV is still being inserted but everything now works correctly. 神秘的DIV仍在插入,但现在一切正常。

The take-away lesson for me is that jssor seems to fail quietly in the sense that rather than generate a console message about the missing option it simply did nothing. 对我来说,重要的一点是,jssor似乎悄无声息地失败了,因为它没有生成有关缺少选项的控制台消息,而是什么也没做。

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

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