gpt4 book ai didi

javascript - 如何创建一个启动按钮 "Bootstrap Tour"?

转载 作者:行者123 更新时间:2023-11-28 03:47:32 28 4
gpt4 key购买 nike

我在我的网站“Drupal 8”上安装了下一个模块“D8:Bootstrap Tour”,它使用主题“Bootstrap 3”。

https://www.drupal.org/project/bs_tour

http://bootstraptour.com/api/

这是模块的文件 bs-tour.js 的内容:

(function ($, _, Drupal, drupalSettings) {
'use strict';

Drupal.behaviors.bsTour = {
attach: function (context, settings) {
$(window).on('load', function (event) {
try
{
var tourOptions = $(drupalSettings.bs_tour.tour)[0];
var tips = tourOptions.steps;
var keyboard = tourOptions.keyboard;
var debug = tourOptions.debug;
var steps = [];

for (var i = 0; i < tips.length; i++) {
if ($(tips[i].element).length > 0) {
tips[i].backdropPadding.top = parseInt(tips[i].backdropPadding.top);
tips[i].backdropPadding.right = parseInt(tips[i].backdropPadding.right);
tips[i].backdropPadding.bottom = parseInt(tips[i].backdropPadding.bottom);
tips[i].backdropPadding.left = parseInt(tips[i].backdropPadding.left);

switch (tips[i].backdrop) {
case "0":
tips[i].backdrop = false;
break;

case "1":
tips[i].backdrop = true;
break;
}

steps.push(tips[i]);
}
}

if (steps.length) {
var tour = new Tour({
debug: debug,
keyboard: keyboard,
template: "<div class='popover tour'>\
<div class='arrow'></div>\
<h3 class='popover-title'></h3>\
<div class='popover-content'></div>\
<div class='popover-navigation'>\
<button class='btn btn-default' data-role='prev'>« " + Drupal.t('Prev') + "</button>\
<span data-role='separator'>|</span>\
<button class='btn btn-default' data-role='next'>" + Drupal.t('Next') + " »</button>\
<button class='btn btn-default' data-role='end'>" + Drupal.t('Skip tour') + "</button>\
</div>\
</div>",
});

// Add steps to the tour
tour.addSteps(steps);

// Initialize the tour
tour.init();

// Start the tour
tour.start();

// Add tour object to drupalSettings to allow manipulating tour from other modules.
// Example: drupalSettings.bs_tour.currentTour.end();
drupalSettings.bs_tour.currentTour = tour;
}

} catch (e) {
// catch any fitvids errors
window.console && console.warn('Bootstrap tour stopped with the following exception');
window.console && console.error(e);
}
});
}
};

})(window.jQuery, window._, window.Drupal, window.drupalSettings);

首次访问该网站时,访问会自动开始。我的问题是,当用户单击“结束访问”时,重新启动访问的唯一方法是从浏览器中删除 cookie。该模块没有手动启动访问的按钮。

我使用以下代码在 Drupal 中创建了一个自定义 block :

<button class="btn btn-success btn-sm" data-target="#start"><i class="fa fa-compass fa-lg"></i> Commencer la visite</button>

我在子主题的 js 文件夹中创建了一个文件tour.js:

/**
* @file
* Bootstrap Tour.
*/

var tourObject = drupalSettings.bs_tour.currentTour;
if (tourObject && tourObject._options.steps.length) {
tourObject.start();
}

我修改了子主题的 bootstrap_subtheme_front_office.libraries.yml 文件,如下所示:

global-styling:
css:
theme:
fonts/font-awesome/css/font-awesome.css: {}
# bootstrap/dist/css/bootstrap.css: {}
# css/bootstrap-cosmo.css: {}
css/style.css: {}
# css/style-noel.css: {}
# css/style-nouvel-an.css: {}

bootstrap-scripts:
js:
bootstrap/js/affix.js: {}
bootstrap/js/alert.js: {}
bootstrap/js/button.js: {}
bootstrap/js/carousel.js: {}
bootstrap/js/collapse.js: {}
# bootstrap/js/dropdown.js: {}
bootstrap/js/modal.js: {}
bootstrap/js/tooltip.js: {}
bootstrap/js/popover.js: {}
bootstrap/js/scrollspy.js: {}
bootstrap/js/tab.js: {}
bootstrap/js/transition.js: {}
js/tour.js: {}

我清理了缓存,但按钮不起作用。这是我网站的页面,按钮位于左侧菜单中。

enter image description here

我找到了解决方案:

我将模块的文件“bs-tour.js”复制到子主题的文件夹 js 中,并在其中插入以下代码:

    $('#bs-tour-restart').click(function () {
// Restart the tour
tour.restart();
});

bs-tour.js 文件现在如下所示:

(function ($, _, Drupal, drupalSettings) {
'use strict';

Drupal.behaviors.bsTour = {
attach: function (context, settings) {
$(window).on('load', function (event) {
try
{
var tourOptions = $(drupalSettings.bs_tour.tour)[0];
var tips = tourOptions.steps;
var keyboard = tourOptions.keyboard;
var debug = tourOptions.debug;
var steps = [];

for (var i = 0; i < tips.length; i++) {
if ($(tips[i].element).length > 0) {
tips[i].backdropPadding.top = parseInt(tips[i].backdropPadding.top);
tips[i].backdropPadding.right = parseInt(tips[i].backdropPadding.right);
tips[i].backdropPadding.bottom = parseInt(tips[i].backdropPadding.bottom);
tips[i].backdropPadding.left = parseInt(tips[i].backdropPadding.left);

switch (tips[i].backdrop) {
case "0":
tips[i].backdrop = false;
break;

case "1":
tips[i].backdrop = true;
break;
}

steps.push(tips[i]);
}
}

if (steps.length) {
var tour = new Tour({
debug: debug,
keyboard: keyboard,
template: "<div class='popover tour'>\
<div class='arrow'></div>\
<h3 class='popover-title'></h3>\
<div class='popover-content'></div>\
<div class='popover-navigation'>\
<button class='btn btn-default' data-role='prev'>« " + Drupal.t('Prev') + "</button>\
<span data-role='separator'>|</span>\
<button class='btn btn-default' data-role='next'>" + Drupal.t('Next') + " »</button>\
<button class='btn btn-default' data-role='end'>" + Drupal.t('Skip tour') + "</button>\
</div>\
</div>",
});

// Add steps to the tour
tour.addSteps(steps);

// Initialize the tour
tour.init();

// Start the tour
tour.start();

$('#bs-tour-restart').click(function () {
// Restart the tour
tour.restart();
});

// Add tour object to drupalSettings to allow manipulating tour from other modules.
// Example: drupalSettings.bs_tour.currentTour.end();
drupalSettings.bs_tour.currentTour = tour;
}

} catch (e) {
// catch any fitvids errors
window.console && console.warn('Bootstrap tour stopped with the following exception');
window.console && console.error(e);
}
});
}
};

})(window.jQuery, window._, window.Drupal, window.drupalSettings);

我使用以下代码创建了一个自定义 block :

<button class="btn btn-success btn-sm" id="bs-tour-restart"><i class="fa fa-compass fa-lg"></i> Commencer la visite</button>

我不知道这是否是一个好的做法,但是开始游览的按钮有效;-)

最佳答案

我认为你需要delete/expire首先使用一些 javascript 来处理 cookie,然后再次开始游览。

function eraseCookie(name) {   
document.cookie = name+'=; Max-Age=-99999999;';
}
<小时/>

更新:如果您收到“不是函数”错误,请尝试将代码包装在:

$(document).ready(function() { [code here] });

关于javascript - 如何创建一个启动按钮 "Bootstrap Tour"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48313564/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com