How to make tooltip with pure javascript (onclick

2019-09-20 00:24发布

I need to use some JS no JQuery plugins to make a simple tooltip.

When I click on image I want to display div with triangle corner bellow that image with some text inside.

I google and google and didn't find anything.

Simple click on image to show info and click on same image to dismiss it but can't figure out how to do this.
enter image description here

1条回答
仙女界的扛把子
2楼-- · 2019-09-20 00:49

If you're willing to use jquery, but not a plugin, then this can be done pretty simply.

http://jsfiddle.net/GQE4k/

var h = false;
$("#container").hover(function(){
    if (h == false){
        $("#popUp").fadeIn();
        $("#popUpText").fadeIn();
        h = true;
    }
},function(){
    if (h == true){
        $("#popUp").fadeOut();
        $("#popUpText").fadeOut(function(){h=false});
    }
});

For click instead of hover:

http://jsfiddle.net/GQE4k/1/

var h = false;
$("#container").click(function(){
    if (h == false){
        $("#popUp").fadeIn();
        $("#popUpText").fadeIn(function(){h = true;});
    }
    if (h == true){
        $("#popUp").fadeOut();
        $("#popUpText").fadeOut(function(){h=false});
    }
});
查看更多
登录 后发表回答