mirror of
https://github.com/kamranahmedse/driver.js.git
synced 2026-10-31 03:52:13 +08:00
Add functionality to position the popover on mid center
This commit is contained in:
@@ -10,6 +10,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
title: 'Before we start',
|
||||
description: 'This is just one use-case, make sure to check out the rest of the docs below.',
|
||||
nextBtnText: 'Okay, Start!',
|
||||
position: 'mid-center',
|
||||
},
|
||||
}, {
|
||||
element: '#logo_img',
|
||||
|
||||
@@ -167,6 +167,9 @@ export default class Popover extends Element {
|
||||
case 'bottom-right':
|
||||
this.positionOnBottomRight(position);
|
||||
break;
|
||||
case 'mid-center':
|
||||
this.positionOnMidCenter(position);
|
||||
break;
|
||||
case 'auto':
|
||||
default:
|
||||
this.autoPosition(position);
|
||||
@@ -434,6 +437,30 @@ export default class Popover extends Element {
|
||||
this.tipNode.classList.add('top', 'position-right');
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the popover on the mid-center of the given position
|
||||
* @param {Position} elementPosition
|
||||
* @private
|
||||
*/
|
||||
positionOnMidCenter(elementPosition) {
|
||||
const popoverDimensions = this.getSize();
|
||||
const popoverHeight = popoverDimensions.height;
|
||||
const popoverWidth = popoverDimensions.width / 2;
|
||||
const popoverCenter = popoverHeight / 2;
|
||||
|
||||
const elementCenter = (elementPosition.bottom - elementPosition.top) / 2;
|
||||
const topCenterPosition = (elementPosition.top - popoverCenter) + elementCenter + this.options.offset;
|
||||
const nodeCenter = this.options.offset + elementPosition.left + ((elementPosition.right - elementPosition.left) / 2);
|
||||
|
||||
this.node.style.top = `${topCenterPosition}px`;
|
||||
this.node.style.left = `${nodeCenter - popoverWidth - this.options.padding}px`;
|
||||
this.node.style.right = '';
|
||||
this.node.style.bottom = '';
|
||||
|
||||
// Add the tip at the top center
|
||||
this.tipNode.classList.add('mid-center');
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically positions the popover around the given position
|
||||
* such that the element and popover remain in view
|
||||
|
||||
@@ -99,6 +99,10 @@ div#driver-popover-item {
|
||||
right: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
&.mid-center {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.driver-popover-footer {
|
||||
|
||||
Vendored
+6
@@ -555,6 +555,12 @@ declare module 'driver.js' {
|
||||
*/
|
||||
private positionOnBottomRight(position: Driver.Position): void;
|
||||
|
||||
/**
|
||||
* Positions the popover to the middle center of the given element position
|
||||
* @param {Driver.Position} position
|
||||
*/
|
||||
private positionOnMidCenter(position: Driver.Position): void;
|
||||
|
||||
/**
|
||||
* Positions the popover automatically around the element position
|
||||
* @param {Driver.Position} position
|
||||
|
||||
Reference in New Issue
Block a user