为什么Flex滑块不适用于最小 - 最大动态范围?

时间:2017-03-13 12:24:48

标签: javascript jquery html flexslider

我想在移动设备中显示3个缩略图,在弹性滑块中显示4个桌面缩略图。为此我使用Carousel with dynamic min/max ranges版本的flex滑块。如果您调整该页面的大小,您会看到根据浏览器窗口宽度更改的thmubanils数量。但是当我使用相同的代码在我的网站或jsfiddle或任何地方它不起作用。这是jsfiddle proof和相同的代码:

(function() {

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

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


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

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

    flexslider.vars.minItems = gridSize;
    flexslider.vars.maxItems = gridSize;
  });
}());
.container {
  width: 70%;
  max-width: 1100px;
  margin: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.3/jquery.flexslider.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.3/flexslider.min.css" rel="stylesheet" />

<div class="container">
  <div class="flexslider">
    <ul class="slides">
      <li>
        <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg" />
      </li>
      <li>
        <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_lemon.jpg" />
      </li>
      <li>
        <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_donut.jpg" />
      </li>
      <li>
        <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_caramel.jpg" />
      </li>
      <!-- items mirrored twice, total of 12 -->
    </ul>
  </div>

我想在github上报告这是一个问题,但我很担心为什么它会在实际的flex滑块网站上运行。

2 个答案:

答案 0 :(得分:5)

奇怪的是,这个滑块的官方文档遗漏了这个重要信息。在启动或加载时,您必须将滑块实例设置为flexslider全局变量。

工作示例。

&#13;
&#13;
 (function() {

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

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

       
        $window.ready(function() {
            $('.flexslider').flexslider({
                animation: "slide",
                animationLoop: false,
                itemWidth: 210,
                itemMargin: 5,
                minItems: getGridSize(), // use function to pull in initial value
                maxItems: getGridSize(),
          			start: function(slider){
                //set slider instance to flexslider variable
            			flexslider = slider;
          	}
            });
        });

        // check grid size on resize event
        $window.resize(function() {
            var gridSize = getGridSize();
            flexslider.vars.minItems = gridSize;
            flexslider.vars.maxItems = gridSize;
        });
        
    }());
&#13;
.container {
  width: 70%;
  max-width: 1100px;
  margin: auto;
}
&#13;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="container">
<div id="carousel" class="flexslider">
  <ul class="slides">
    <li>
      <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg" />
    </li>
    <li>
      <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_lemon.jpg" />
    </li>
    <li>
      <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_donut.jpg" />
    </li>
    <li>
      <img src="http://flexslider.woothemes.com/images/kitchen_adventurer_caramel.jpg" />
    </li>
    <!-- items mirrored twice, total of 12 -->
  </ul>
</div>
	</div>
	
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.3/jquery.flexslider.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.3/flexslider.min.css" rel="stylesheet" />

</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下是最小-最大动态范围的工作示例。

    var $window = jQuery(window),
    flexslider = {
        vars: {}
    };
    
    function getGridSize() {
            return (window.innerWidth <= 767) ? 2 : (window.innerWidth <= 900) ? 2 : (window.innerWidth <= 1024) ? 3 : (window.innerWidth > 1025) ? 5 : 5;
}

$window.ready(function() {
        jQuery('.flexslider').flexslider({
            animation: "slide",
            slideshow: false,
            itemWidth: 248,
            itemMargin: 5,      
            minItems: 2,
            maxItems: getGridSize(),
            controlNav: false
        });