javascript - Intro.js 无法在两个步骤之间触发函数(请检查我的代码)

标签 javascript intro.js

我是 JS 的绝对初学者,请检查我的代码,我无法在 intro.js 的两个步骤之间调用函数。现在我只是想显示警报但失败了。

<script type="text/javascript">
var introguide = introJs();
$(function(){



introguide.setOptions({
steps: [
{
  element: '#chaman',
  intro: 'This is step1',
  position: 'top',
  onchange: function(){
    //do something interesting here...

    alert("Want to call function after this step , no alert showing ");
  },
  onbeforechange: function(){

  }
},
{
  element: '#search',
  intro: 'This is step 2.',
  position: 'bottom',
  onchange: function(){
    //do something interesting here...


  },
  onbeforechange: function(){

  }

}
,
{
  element: '.flyMarker',
  intro: 'This is step 3.',
  position: 'bottom',
  onchange: function(){
    //do something interesting here...
  },
  onbeforechange: function(){
    //do something else interesting here...
  }
}
]
});



setTimeout(function() { introguide.start(); }, 3000);


});


createStepEvents: function( guideObject, eventList ){

//underscore loop used here, foreach would work just as well
_.each( eventList, function( event ){

//for the guid object's <event> attribute...
guideObject[event]( function(){

  //get its steps and current step value
  var steps       = this._options.steps,
      currentStep = this._currentStep;

  //if it's a function, execute the specified <event> type
  if( _.isFunction(steps[currentStep][event]) ){
    steps[currentStep][event]();
  }
});

}, this );
}

//setup the events per step you care about for this guide
createStepEvents( introguide, ['onchange','onbeforechange']);



</script>

我想在每一步之后触发一些函数。

最佳答案

别忘了添加underscore.js .

您的代码错误是:

createStepEvents: function( guideObject, eventList ){

更改为:

function createStepEvents(guideObject, eventList) {

var introguide = introJs();
$(function() {

  introguide.setOptions({
    steps: [{
        element: '#chaman',
        intro: 'This is step1',
        position: 'top',
        onchange: function() {
          alert("Do whatever you want on this callback.");
        },
        onbeforechange: function() {
        }
      },
      {
        element: '#search',
        intro: 'This is step 2.',
        position: 'bottom',
        onchange: function() {
        },
        onbeforechange: function() {
        }
      },
      {
        element: '.flyMarker',
        intro: 'This is step 3.',
        position: 'bottom',
        onchange: function() {
        },
        onbeforechange: function() {
        }
      }
    ]
  });

  setTimeout(function() {
    introguide.start();
  }, 3000);

});

function createStepEvents(guideObject, eventList) {

  //underscore loop used here.
  _.each(eventList, function(event) {

    //for the guid object's <event> attribute.
    guideObject[event](function() {

      //get its steps and current step value
      var steps = this._options.steps,
        currentStep = this._currentStep;

      //if it's a function, execute the specified <event> type
      if (_.isFunction(steps[currentStep][event])) {
        steps[currentStep][event]();
      }
    });

  }, this);
}

//setup the events per step you care about for this guide
createStepEvents(introguide, ['onchange', 'onbeforechange']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intro.js/2.4.0/intro.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

关于javascript - Intro.js 无法在两个步骤之间触发函数(请检查我的代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39179247/

相关文章:

javascript - 忽略 tour.js 中缺少的游览元素

tooltip - 如何自定义 intro.js 的覆盖层

javascript - 将项目动态添加到 Accordion 布局

javascript - 我的 anchor 文本突然无法点击

javascript - 跳过或完成后的 IntroJS 回调函数?

intro.js - 一次显示所有 intro.js 导览步骤

javascript - Bootstrap 4 模态与 intro.js transparant

javascript - 在 bootstrap daterangepicker 中默认选择 'This Month' 范围

javascript - ReactJS 的实时 API 监听器

javascript - 尝试使用 AJAX 和 PHP