Tooltips for mobile browsers

后端 未结 9 1946
后悔当初
后悔当初 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:

      <span class="more_info">Main Text<div class="popup">Pop-up text can use <b>HTML</b><div></span>
    

    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/

    0 讨论(0)
  • 2020-12-07 13:55

    Depending on how much information you want to give the user, a modal dialogue box might be an elegant solution.

    Specifically, you could try the qTip jQuery plugin, which has a modal mode fired on $.click():

    qTip Modal tooltip

    0 讨论(0)
  • 2020-12-07 13:59

    The title attribute is not supported in any mobile browsers **in a way that it would show the tooltip the same as to desktop mouse users** *(the attribute itself is ofcourse supported in the markup)*.
    It's only basically for desktop users with a mouse, keyboard only users can't use it either, or screenreaders.

    You can achieve almost similar with javascript as you said.

    0 讨论(0)
提交回复
热议问题