FlexSlider未捕获的TypeError和ReferenceError

时间:2014-11-01 14:40:46

标签: javascript jquery flexslider

我正在使用FlexSlider 2.2.2并且他的代码片段生成了两个jQuery错误

  

未捕获的TypeError:无法读取未定义的未捕获属性'vars'   ReferenceError:未定义SyntaxHighlighter

        jQuery(document).ready(function(){

  // store the slider in a local variable
  var jQuerywindow = jQuery(window),
      flexslider;

  // tiny helper function to add breakpoints
  function getGridSize() {
    return (window.innerWidth < 600) ? 1 :
           (window.innerWidth < 900) ? 3 : 3;
  }

  jQuery(function() {
    SyntaxHighlighter.all();
  });

  jQuery('.flexslider').flexslider({
    animation: "slide",
    animationLoop: false,
    itemWidth: 290,
    itemMargin: 0,
    prevText: " ",
    nextText: " ",
 minItems: getGridSize(), // use function to pull in initial value
      maxItems: getGridSize() // use function to pull in initial value
    });
 // check grid size on resize event
  jQuery(window).resize(function() {
    var gridSize = getGridSize();

    flexslider.vars.minItems = gridSize;
    flexslider.vars.maxItems = gridSize;
  });
});

编辑:高度错误以提高可见性。

2 个答案:

答案 0 :(得分:3)

我无法重现syntaxhighlighter错误。但是flexslider错误是因为你没有初始化flexslider变量。

工作演示:http://jsfiddle.net/lotusgodkk/nwjra/23/

    jQuery('.flexslider').flexslider({
    animation: "slide",
    animationLoop: false,
    itemWidth: 290,
    itemMargin: 0,
    prevText: " ",
    nextText: " ",
    minItems: getGridSize(),
    maxItems: getGridSize(),
    start: function (slider) {
        flexslider = slider; //Initializing flexslider here.
    }
});

您还可以看到此处没有出现syntaxhighlight错误。

答案 1 :(得分:0)

start: function(slider){
        flexslider = slider;
       }

var $window = $(window),
    flexslider = { vars:{} };

我的诀窍。

完整代码:

jQuery(document).ready(function() {
    // Carousel with dynamic min/max ranges
    (function() {

        // store the slider in a local variable
        var $window = $(window),
            flexslider = { vars:{} };

        // tiny helper function to add breakpoints
        function getGridSize() {
            return (window.innerWidth < 480) ? 1 :
                (window.innerWidth < 900) ? 2 : 3;
        }

        $window.load(function() {
            $('.flexslider').flexslider({
                animation: "slide",
                animationLoop: true,
                itemWidth: 210,
                itemMargin: 0,
                controlNav: false,
                minItems: getGridSize(), // use function to pull in initial value
                maxItems: getGridSize(), // use function to pull in initial value
                start: function(slider){
                    flexslider = slider;
                }
            });
        });

        // check grid size on resize event
        $window.resize(function() {
            var gridSize = getGridSize();

            flexslider.vars.minItems = gridSize;
            flexslider.vars.maxItems = gridSize;
        });
    }());
});