简体   繁体   中英

FlexSlider Uncaught TypeError and ReferenceError

I'm using FlexSlider 2.2.2 and he following snippet is generating two jQuery errors

Uncaught TypeError: Cannot read property 'vars' of undefined Uncaught ReferenceError: SyntaxHighlighter is not defined

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

Edit : highight error for better visibility.

I couldn't reproduce the syntaxhighlighter error. but flexslider error was because you were not initializing the flexslider variable.

Working Demo: 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.
    }
});

You can also see that the syntaxhighlight error does not appear here.

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

and

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

Does the trick for me.

Complete code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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