0

I'm fairly new to AngularJS, and working on a project that is very restrictive towards external dependencies; in fact, Bootstrap is ruled out, and yet, I have to implement a "tooltip" directive.

I'm trying to do it using mouseenter and mouseleave events, but I wonder how can I use the directive's own attribute as the tooltip's content?

It'd be used like this:

<a id='someLink' my-tooltip='The content that I want to show'>Trigger</a>

Being quite green on UI development, can anyone tell me how I could add the necessary HTML/CSS to make this work through the directive?

2
  • There is an Angularjs version of bootstrap here that is written by the angular team. Commented Aug 13, 2014 at 18:22
  • Sorry, I should have been more clear in the question; angular-ui is also ruled out. AFAIK the person responsible wants to keep the client as thin as it can possibly be. Commented Aug 13, 2014 at 23:01

2 Answers 2

1

DEMO

app.directive("tooltip", function () {
  return {
    link: function (scope, element, attrs) {

        $(element).on("mouseover", function () {
            $(this).append("<span>"+ attrs.tooltip +"</span>");
        });

        $(element).on("mouseout", function () {
            $(this).find("span").remove();
        });

        scope.$on("$destroy", function () {
            $(element).off("mouseover");
            $(element).off("mouseout");                
        });
    }
  };
});

CSS Source

a.tooltips {
  position: relative;
  display: inline;
}
a.tooltips span {
  position: absolute;
  width:140px;
  color: #FFFFFF;
  background: #000000;
  height: 30px;
  line-height: 30px;
  text-align: center;
  visibility: hidden;
  border-radius: 6px;
}
a.tooltips span:after {
  content: '';
  position: absolute;
  top: 50%;
  right: 100%;
  margin-top: -8px;
  width: 0; height: 0;
  border-right: 8px solid #000000;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
}
a:hover.tooltips span {
  visibility: visible;
  opacity: 0.8;
  left: 100%;
  top: 50%;
  margin-top: -15px;
  margin-left: 15px;
  z-index: 999;
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is a shorter version that I use:

PIAPP.directive('piTooltip', function(){
    return function($scope, element, attrs){
        element.append('<u>'+attrs.piTooltip+'</u>');
    };
});

CSS:

*[pi-tooltip]>u{
    visibility: hidden;
    opacity: 0;
    font-size: 12px;
    padding: 0 5px;
    background: #f6e6c2;
    border: 1px solid #000;
    color: #000;
    z-index: 1000;
    white-space: nowrap;
    text-decoration: none;
    position: absolute;
    bottom: -2em;
    left: 0;
    transition: opacity 0.5s linear 0.5s, visibility 0s;
}
*[pi-tooltip]:hover>u{
    visibility: visible;
    opacity: 1;
}

You can use it in any container (a, span, div, etc) like this:

<div pi-tooltip="The content that I want to show">...</div>

Show the demo no Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.