javascript - Close tooltip on click outside of it -
i trying make tooltip notification text able close on click anywhere outside on 150x150 box because right manage close tooltip if box clicked again.
here code until now. ideas ?
this answer @junaid cannot me because when implement code, tooltip not show on click. may receive accurate answer problem ?
var hastooltip = $(".inner"); hastooltip.on("click", function(e) { e.preventdefault(); var isshowing = $(this).data("isshowing"); hastooltip.removedata("isshowing"); if (isshowing != "true") { hastooltip.not(this).tooltip("hide"); $(this).data("isshowing", "true"); $(this).tooltip("show"); } else { $(this).tooltip("hide"); } }).tooltip({ animation: true, trigger: "manual", placement: "auto" });
.container { width: 960px; margin: 0 auto; } .outer { width: 150px; height: 150px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="outer"> <div class="inner" data-toggle="tooltip" data-placement="bottom" data-original-title="tooltip text" aria-describedby="tooltip"> <img src="http://via.placeholder.com/150x150"> </div> </div> </div>
js fiddle example fiddle
look @ example below (removed hide/show onclick simplify example). shows how can handle mouseout method remove popover, whilst cleaning event after self.
var $hastooltip = $(".inner"); $hastooltip.tooltip({ animation: true, trigger: "manual", placement: "auto" }); $hastooltip.on("click", function(e) { var $this = $(this); e.preventdefault(); // create event handler need remove event when done function onwindowclick() { $this.tooltip("hide"); // remove event $('.fakebox').off('mouseout', onwindowclick); $('.fakebox').css({display:'none'}); } // add event $('.fakebox').on('click', onwindowclick); $('.fakebox').css({display:'block'}); $(this).tooltip("show"); })
.container { width: 960px; margin: 0 auto; } .outer { width: 150px; height: 150px; } .fakebox {display:none; width:100%; height: 100%; position:fixed;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="fakebox"></div> <div class="container"> <div class="outer"> <div class="inner" tab-index="0" data-toggle="tooltip" data-placement="bottom" data-original-title="tooltip text" aria-describedby="tooltip"> <img src="http://via.placeholder.com/150x150"> </div> </div> </div>
Comments
Post a Comment