如何将“Flexslider”当前的图像编号变成一个可以在函数中使用的变量?

时间:2022-12-04 07:53:03

I am using flexslider. There is an example under the advanced tap on this page (in the section "The new, robust Callback API" where they use slider.currentSlide to get the number of the current slide.

我用flexslider。在此页面的高级点击下面有一个示例(在“新的、健壮的回调API”中,它们使用slider。获取当前幻灯片的个数。

How can I access this value in a completely different function? I am thinking of a global variable but I don't know if that will work and I don't know if it's possible to declare a variable within a function as global.

如何在一个完全不同的函数中访问这个值?我在考虑一个全局变量,但我不知道它是否可行,也不知道是否有可能在函数中声明一个全局变量。

2 个解决方案

#1


17  

You can declare a global variable and set the value in the after callback function. Here's some pseudocode to help you get the idea:

您可以声明一个全局变量并在after callback函数中设置值。这里有一些伪代码可以帮助你理解:

var curSlide;
$(window).load(function() {
  $('.flexslider').flexslider({
    after: function(slider) {
      window.curSlide = slider.currentSlide;
    }
  });
});

function useCurSlideHere() {
  alert(window.curSlide);
}

Although ideally you'd avoid using a global variable and you could make the useCurSlideHere function into a class which you create an instance of on window.load and use it when you pass the curSlide variable in the after callback function.

虽然理想情况下,您应该避免使用全局变量,但是您可以将useCurSlideHere函数创建为一个在窗口中创建实例的类。在传入回调函数中的curSlide变量时加载并使用它。

$(window).load(function() {
  var customSlider = new useCurSlideHere();
  $('.flexslider').flexslider({
    after: function(slider) {
      customSlider.setCurSlide(slider.currentSlide);
    }
  });      
});

function useCurSlideHere() {
  this.curSlide = 0;

  this.setCurSlide = function(curSlide) {
    this.curSlide = curSlide;
  }

  this.getCurSlide= function() {
    alert(this.curSlide);
  }
}

EDIT: Edited the answer to use slider.currentSlide.

编辑:编辑答案,使用slider.currentSlide。

#2


33  

You can avoid global variables by accessing the flexslider object with:

您可以通过以下方式访问flexslider对象来避免全局变量:

$(element).data('flexslider')

So in your case you'd use:

所以在你的例子中你会用到:

$(element).data('flexslider').currentSlide

#1


17  

You can declare a global variable and set the value in the after callback function. Here's some pseudocode to help you get the idea:

您可以声明一个全局变量并在after callback函数中设置值。这里有一些伪代码可以帮助你理解:

var curSlide;
$(window).load(function() {
  $('.flexslider').flexslider({
    after: function(slider) {
      window.curSlide = slider.currentSlide;
    }
  });
});

function useCurSlideHere() {
  alert(window.curSlide);
}

Although ideally you'd avoid using a global variable and you could make the useCurSlideHere function into a class which you create an instance of on window.load and use it when you pass the curSlide variable in the after callback function.

虽然理想情况下,您应该避免使用全局变量,但是您可以将useCurSlideHere函数创建为一个在窗口中创建实例的类。在传入回调函数中的curSlide变量时加载并使用它。

$(window).load(function() {
  var customSlider = new useCurSlideHere();
  $('.flexslider').flexslider({
    after: function(slider) {
      customSlider.setCurSlide(slider.currentSlide);
    }
  });      
});

function useCurSlideHere() {
  this.curSlide = 0;

  this.setCurSlide = function(curSlide) {
    this.curSlide = curSlide;
  }

  this.getCurSlide= function() {
    alert(this.curSlide);
  }
}

EDIT: Edited the answer to use slider.currentSlide.

编辑:编辑答案,使用slider.currentSlide。

#2


33  

You can avoid global variables by accessing the flexslider object with:

您可以通过以下方式访问flexslider对象来避免全局变量:

$(element).data('flexslider')

So in your case you'd use:

所以在你的例子中你会用到:

$(element).data('flexslider').currentSlide