Example of Smart Positioning in ASP.NET MVC Tooltip Control
This sample demonstrates the smart positioning functionalities of the Tooltip which will open by dragging the picture.
@Html.UiTooltip(new UiTooltipOptions { Id = "targetTooltip", Target = "#demoSmart", Content = "Drag me anywhere, to start walking with me !!!", ShowTipPointer = true, Position = UiTooltipPosition.TopCenter })
<style>
#demoSmart { width: 100px; height: 100px; background-image: url('@Url.Content("~/Content/tooltip/smartposition.png")'); background-size: 100px 100px; position: absolute; left: calc(50% - 50px); top: calc(50% - 50px); cursor: grab; touch-action: none; }
#targetContainer { position: relative; margin: 10px; min-height: 320px; width: 100%; border: 1px solid #dddddd; border-radius: 3px; }
.control-section { padding: 3px; margin-right: 20px; }
</style>
<div class="control-section">
<div id="targetContainer">
<div id="demoSmart" role="img" aria-label="A walking figure you can drag"></div>
</div>
@Html.UiTooltip(new UiTooltipOptions { Id = "targetTooltip", Target = "#demoSmart", Content = "Drag me anywhere, to start walking with me !!!", ShowTipPointer = true, Position = UiTooltipPosition.TopCenter })
</div>
<script>
// Syncfusion: the element is dragged inside its container and the Tooltip follows it, moving to the side that has room
(function () {
var box = document.getElementById('targetContainer');
var ele = document.getElementById('demoSmart');
var grab = null;
function tip() { return window.Lumen.get('targetTooltip'); }
ele.addEventListener('pointerdown', function (e) {
var r = ele.getBoundingClientRect();
grab = { x: e.clientX - r.left, y: e.clientY - r.top };
ele.setPointerCapture(e.pointerId);
tip().open(ele);
});
ele.addEventListener('pointermove', function (e) {
if (!grab) { return; }
var area = box.getBoundingClientRect();
var left = Math.max(0, Math.min(area.width - ele.offsetWidth, e.clientX - area.left - grab.x));
var top = Math.max(0, Math.min(area.height - ele.offsetHeight, e.clientY - area.top - grab.y));
ele.style.left = left + 'px';
ele.style.top = top + 'px';
tip().refresh();
});
ele.addEventListener('pointerup', function () { grab = null; });
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TooltipController : Controller
{
// The page is Views/Tooltip/SmartPositioning.cshtml; it draws what ViewData holds.
public ActionResult SmartPositioning()
{
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
This sample shows the dynamic adjustment of the tooltip position within the specified Viewport. Start dragging the ant image, so that the tooltip opens up immediately and keeps moving along with the target image. When the image reaches the corners of the sample container on dragging, the tooltip and its arrow position will be auto adjusted to make it look fit within the sample container area.
In this sample, the tooltip is opened manually by using its open method on drag start of the target image. On further dragging, the refresh method of the tooltip needs to be called to reposition it continuously and on drag stop, the tooltip will be hidden by using it’s close method.