Position in EJ2 JavaScript Tooltip control
8 May 202315 minutes to read
Tooltips can be attached to 12 static locations around the target. On initializing the Tooltip, you can set the position property with any one of the following values:
TopLeft
TopCenter
TopRight
BottomLeft
BottomCenter
BottomRight
LeftTop
LeftCenter
LeftBottom
RightTop
RightCenter
RightBottom
By default, Tooltip is placed at the
TopCenter
of the target element.
var button = new ej.buttons.Button({content: 'Show Tooltip'});
button.appendTo('#tooltip');
var tooltip = new ej.popups.Tooltip({
position: 'TopCenter',
content: 'Tooltip Content'
});
tooltip.appendTo('#tooltip');
var dropDownListObject = new ej.dropdowns.DropDownList({
change: (args) => {
tooltip.close();
tooltip.position = args.value;
}
});
dropDownListObject.appendTo('#positions');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Tooltip</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="tooltip"></div>
<div class="ddl">
<select id="positions" class="form-control" style="width:150px">
<option value="TopLeft">Top Left</option>
<option value="TopCenter" selected="">Top Center</option>
<option value="TopRight">Top Right</option>
<option value="BottomLeft">Bottom Left</option>
<option value="BottomCenter">Bottom Center</option>
<option value="BottomRight">Bottom Right</option>
<option value="LeftTop">Left Top</option>
<option value="LeftCenter">Left Center</option>
<option value="LeftBottom">Left Bottom</option>
<option value="RightTop">Right Top</option>
<option value="RightCenter">Right Center</option>
<option value="RightBottom">Right Bottom</option>
</select>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Tip pointer positioning
The Tooltip pointer can be attached or detached from the Tooltip by using the showTipPointer
property. Pointer positions can be adjusted using the tipPointerPosition
property that can be assigned to one of the following values:
Auto
Start
Middle
End
The following code example illustrates how to set the pointer to the start position of the Tooltip.
var tooltip = new ej.popups.Tooltip({
tipPointerPosition: "Start"
});
tooltip.appendTo('#target');
var button = new ej.buttons.Button({content: 'Show Tooltip'});
button.appendTo('#target');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Tooltip</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="target" title="Tooltip content">
<span>Show tooltip</span>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
By default, tip pointers are auto adjusted so that the arrow does not point outside the target element.
Dynamic positioning
The Tooltip and its tip pointer can be positioned dynamically based on the target’s location. This can be achieved by using the refresh
method, which auto adjusts the Tooltip over the target.
var tooltip = new ej.popups.Tooltip({
content: 'Drag me !!!',
target: '#demoSmart',
animation: { open: { effect: 'None' }, close: { effect: 'None' } }
}, '#targetContainer');
var ele = document.getElementById('demoSmart');
var drag = new ej.base.Draggable(ele, {
clone: false,
dragArea: '#targetContainer',
drag: (args) => {
tooltip.refresh(args.element);
},
dragStart: (args) => {
tooltip.open(args.element);
},
dragStop: (args) => {
tooltip.close();
}
});
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Tooltip</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="targetContainer">
<div id="demoSmart">
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Mouse trailing
Tooltips can be positioned relative to the mouse pointer. This behavior can be enabled or disabled by using the mouseTrail
property. By default, it is set to false
.
var tooltip = new ej.popups.Tooltip({
mouseTrail: true,
content: 'Tooltip content'
});
tooltip.appendTo('#target');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Tooltip</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="target">
Show tooltip
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
When mouse trailing option is enabled, the tip pointer position gets auto adjusted based on the target, and
other position values like start, end, and middle are not applied (to prevent the pointer from moving out of target).
Setting offset values
Offset values are set to specify the distance between the target and tooltip element. offsetX
and offsetY
properties are used to specify the offset left and top values.
-
offsetX
specifies the distance between the target and Tooltip element in X axis. -
offsetY
specifies the distance between the target and Tooltip element in Y axis.
The following code example illustrates how to set offset values.
var tooltip = new ej.popups.Tooltip({
offsetX: 30,
offsetY: 30,
mouseTrail: true,
showTipPointer: false,
content: 'Tooltip content'
});
tooltip.appendTo('#target');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Tooltip</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="target">
Show tooltip
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
By default, collision is handled automatically and therefore when collision is detected the Tooltip fits horizontally and flips vertically.