产品导览并非 Driver.js 的唯一用例。你可以使用它来高亮页面上的任何元素,并显示带有描述的弹出窗口。这对于向用户提供上下文帮助很有用,例如帮助用户填写表单或解释某个功能。
¥Product tours is not the only usecase for Driver.js. You can use it to highlight any element on the page and show a popover with a description. This is useful for providing contextual help to the user e.g. help the user fill a form or explain a feature.
以下示例展示了如何高亮元素并简单地显示弹出窗口。
¥Example below shows how to highlight an element and simply show a popover.
以下是上述示例的代码:
¥Here is the code for above example:
const driverObj = driver({
popoverClass: "driverjs-theme",
stagePadding: 4,
});
driverObj.highlight({
element: "#highlight-me",
popover: {
side: "bottom",
title: "This is a title",
description: "This is a description",
}
})
你还可以使用它来显示一个简单的模态框,而不高亮任何元素。
¥You can also use it to show a simple modal without highlighting any element.
以下是上述示例的代码:
¥Here is the code for above example:
const driverObj = driver();
driverObj.highlight({
popover: {
description: "<img src='https://i.imgur.com/EAQhHu5.gif' style='height: 202.5px; width: 270px;' /><span style='font-size: 15px; display: block; margin-top: 10px; text-align: center;'>Yet another highlight example.</span>",
}
})
关注下面的输入,看看弹出窗口是如何显示的。
¥Focus on the input below and see how the popover is shown.
以下是上述示例的代码。
¥Here is the code for the above example.
const driverObj = driver({
popoverClass: "driverjs-theme",
stagePadding: 0,
onDestroyed: () => {
document?.activeElement?.blur();
}
});
const nameEl = document.getElementById("name");
const educationEl = document.getElementById("education");
const ageEl = document.getElementById("age");
const addressEl = document.getElementById("address");
const formEl = document.querySelector("form");
nameEl.addEventListener("focus", () => {
driverObj.highlight({
element: nameEl,
popover: {
title: "Name",
description: "Enter your name here",
},
});
});
educationEl.addEventListener("focus", () => {
driverObj.highlight({
element: educationEl,
popover: {
title: "Education",
description: "Enter your education here",
},
});
});
ageEl.addEventListener("focus", () => {
driverObj.highlight({
element: ageEl,
popover: {
title: "Age",
description: "Enter your age here",
},
});
});
addressEl.addEventListener("focus", () => {
driverObj.highlight({
element: addressEl,
popover: {
title: "Address",
description: "Enter your address here",
},
});
});
formEl.addEventListener("blur", () => {
driverObj.destroy();
});