Tooltips for mobile browsers

后端 未结 9 1962
后悔当初
后悔当初 2020-12-07 13:16

I currently set the title attribute of some HTML if I want to provide more information:

An

9条回答
  •  抹茶落季
    2020-12-07 13:54

    Slightly more elaborated version of flavaflo's answer:

    • Uses pre-defined div as pop-up that can hold HTML, rather than reading from a title attribute
    • Opens/closes on rollover if mouse is used
    • Opens on click (touch screen) and closes on click on the open pop-up or anywhere else on the document.

    HTML:

      Main Text

    CSS:

    .more_info {
        border-bottom: 1px dotted #000;
      position: relative;
        cursor: pointer;
    }
    
    .more_info .popup {
        position: absolute;
        top: 15px; /*must overlap parent element otherwise pop-up doesn't stay open when rolloing over '*/
        background: #fff;
        border: 1px solid #ccc;
        padding: 8px;
        left: 0;
        max-width: 240px;
        min-width: 180px;
        z-index: 100;
        display: none;
    }
    

    JavaScript / jQuery:

    $(document).ready(function () {
    
        //init pop-ups
        $(".popup").attr("data-close", false);
    
        //click on pop-up opener
        //pop-up is expected to be a child of opener
        $(".more_info").click(function () {
            var $title = $(this).find(".popup");
            //open if not marked for closing
            if ($title.attr("data-close") === "false") {
                $title.show();
            }
            //reset popup         
            $title.attr("data-close", false);
        });
    
        //mark pop-up for closing if clicked on
        //close is initiated by document.mouseup, 
        //marker will stop opener from re-opening it
        $(".popup").click(function () {
            $(this).attr("data-close",true);
        });
    
        //hide all pop-ups
        $(document).mouseup(function () {
            $(".popup").hide();
    
        });
    
        //show on rollover if mouse is used
        $(".more_info").mouseenter(function () {
            var $title = $(this).find(".popup");
            $title.show();
        });
    
        //hide on roll-out
        $(".more_info").mouseleave(function () {
            var $title = $(this).find(".popup");
            $title.hide();
        });  
    
    });
    

    Demo here https://jsfiddle.net/bgxC/yvs1awzk/

提交回复
热议问题