LoginSignup
4
6

More than 5 years have passed since last update.

【JavaScript】jQueryでツールチップ表示

Posted at

画像にマウスを重ねると、alt属性のテキストをツールチップに表示する。
捨てるのもなんなのでチェック用にでも使ってください。

jquery.tooltip.js
;(function($,undefined){
  $("img").each(function(){
    $(this)
      .off(".tooltip")
      .on("mouseenter.tooltip", function(e){
        var alt = $(this).attr("alt") || "...";
        var tip = $("<span>"+alt+"</span>");
        tip.css({
          position: "absolute",
          top : e.pageY-24,
          left: e.pageX,
          background: "white",
          color: "black",
          border: "1px outset #333"
        });
        $("body").append(tip);
        $(this).on("mouseleave.tooltip", function(e){
          tip.remove();
        });
      });
  });
})(jQuery);
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6