Tooltips for mobile browsers

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

    I know this is an old question, but i have found a CSS solution that works on mobile too, it doesn't use title at all and it's easy to implement, explained here:

    https://www.w3schools.com/css/css_tooltip.asp Explanation:

    On mobile, with the touchscreen,the first input acts as css hover, so it works like a toggle tooltip when you press on it.

    Code example:

    .tooltip {
      position: relative;
      display: inline-block;
      border-bottom: 2px dotted #666;
    }
    
    .tooltip .tooltiptext {
      visibility: hidden;
      width: 15em;
      background-color: #555;
      color: #fff;
      text-align: center;
      border-radius: 6px;
      position: absolute;
      z-index: 1;
      bottom: 125%;
      left: 50%;
      margin-left: -8em;
      opacity: 0;
      transition: opacity 0.3s;
      padding: 0.5em;
    }
    
    .tooltip .tooltiptext::after {
      content: "";
      position: absolute;
      top: 100%;
      left: 50%;
      margin-left: -5px;
      border-width: 5px;
      border-style: solid;
      border-color: #555 transparent transparent transparent;
    }
    
    .tooltip:hover .tooltiptext {
      visibility: visible;
      opacity: 1;
    }
    
    <div class="tooltip">Hover over me
      <span class="tooltiptext">Tooltip text</span>
    </div> 
    
    0 讨论(0)
  • 2020-12-07 13:45

    Given that a lot of people nowadays (2015) use mobile browsers, and title still hasn't found a form of exposure in mobile browsers, maybe it's time to deprecate reliance upon title for meaningful information.

    It should never be used for critical information, but it is now becoming dubious for useful information, because if that information is useful and cannot be shown to half the users, then another way of showing it to almost all users needs to be found.

    For static pages, perhaps some visible text near to the relevant control, even as fine print. For server-generated pages, browser sniffing could provide that only for mobile browsers. On the client side, javascript could be used to trap the focus event, via bubbling, to show the extra text next to the currently focussed element. That would minimise the screen space taken up, but would not necessarily be of much use, since, in a lot of instances, bringing focus to a control can only be done in a way that immediately activates its action, bypassing the ability to find out about it before using it!

    Over all though, it appears that the difficulties of showing the title attribute on mobile devices, may lead to its demise, mostly due to needing an alternative that is more universal. That is a pity, because mobiles could use a way to show such extra info on-demand, without taking up the limited screen space.

    It seems strange that the w3c and mobile browser makers did not do anything about this issue a long time ago. At least they could have displayed the title text on top of the menu that appears when a long press on a control is made.

    Personally, I wish it was placed at the top of a right-click/long-touch menu, as it won't timeout, and would be available on all browsers.

    The other alternative is to construct footnotes, so an [n] type superscript is put next to the element/text needing more info, linking to explanatory text in a list at the bottom of the page. Each of those can have a similar [n] type link back to the original text/element. That way, it keeps the display uncluttered, but provides easy bidirectional swapping in a simple way. Sometimes, old print media ways, with a little hyperlink help, are best.

    The title attribute has been hijacked by some browsers to provide help text for the pattern attribute, in that its text pops up if the pattern doesn't match the text in the input element. Typically, it is to provide examples of the right format.

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

    Thanks to @flavaflo for their answer. This works in most cases but if there is more than one title to lookup in the same paragraph, and one opens over the link to another, the unopened link shows through the first. This can be solved by dynamically changing the z-index of the title that has "popped up":

    $("span[title]").click(function () {
      var $title = $(this).find(".title");
      if (!$title.length) {
        $(this).append('<span class="title">' + $(this).attr("title") + '</span>');
        $(this).css('z-index', 2);
      } else {
        $title.remove();
        $(this).css('z-index', 0);
      }
    });​
    

    Also, you can make both the hover over display and the click display multiline by adding &#10; (linefeed) to the title='' attribute, and then convert that to <br /> for the html click display:

    $(this).append('<span class="title">' + $(this).attr("title").replace(/\\n/g, '<br />') + '</span>');
    
    0 讨论(0)
  • 2020-12-07 13:46

    As @cimmanon mentioned: span[title]:hover:after { content: attr(title) } gives you a rudimentary tooltip on touch screen devices. Unfortunately this has problems where the default ui behavior on touch screen devices is to select the text when any non-link/uicontrol is pressed.

    To solve the selection problem you can add span[title] > * { user-select: none} span[title]:hover > * { user-select: auto }

    A full solution may use some other techniques:

    • Add position: absolute background, border, box-shadow etc to make it look like a tooltip.
    • Add the class touched to body (via js) when the user uses any touch event. Then you can do body.touched [title]:hover ... without affecting desktop users

    document.body.addEventListener('touchstart', function() {
      document.body.classList.add('touched');
    });
    [title] {
    	border-bottom: 1px dashed rgba(0, 0, 0, 0.2);
    	border-radius:2px;
    	position: relative;
    }
    body.touched [title] > * {
    	user-select: none;
    }
    body.touched [title]:hover > * {
    	user-select: auto
    }
    body.touched [title]:hover:after {
    	position: absolute;
    	top: 100%;
    	right: -10%;
    	content: attr(title);
    	border: 1px solid rgba(0, 0, 0, 0.2);
    	background-color: white;
    	box-shadow: 1px 1px 3px;
    	padding: 0.3em;
    	z-index: 1;
    }
    <div>Some text where a portion has a <span title="here's your tooltip">tooltip</span></div>

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

    You can fake the title tooltip behavior with Javascript. When you click/tab on an element with a title attribute, a child element with the title text will be appended. Click again and it gets removed.

    Javascript (done with jQuery):

    $("span[title]").click(function () {
      var $title = $(this).find(".title");
      if (!$title.length) {
        $(this).append('<span class="title">' + $(this).attr("title") + '</span>');
      } else {
        $title.remove();
      }
    });​
    

    CSS:

    .more_info {
      border-bottom: 1px dotted;
      position: relative;
    }
    
    .more_info .title {
      position: absolute;
      top: 20px;
      background: silver;
      padding: 4px;
      left: 0;
      white-space: nowrap;
    }
    

    Demo: http://jsfiddle.net/xaAN3/

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

    Here is a CSS only solution. (Similar to @Jamie Pate 's answer, but without the JavaScript.)

    We can use the pseudo class :hover, but I'm not sure all mobile browsers apply these styles when the element is tapped. I'm using pseudo class :focus because I'm guessing it's safer. However, when using pseudo class :focus we need to add tabindex="0" to elements that don't have a focus state intrinsically.

    I'm using 2 @media queries to ensure all mobile devices are targeted. The (pointer: coarse) query will target any device that the primary input method is something "coarse", like a finger. And the (hover: none) query will target any device that the primary pointing system can't hover.

    This snippet is all that's needed:

    @media (pointer: coarse), (hover: none) {
          [title] {
            position: relative;
            display: inline-flex;
            justify-content: center;
          }
          [title]:focus::after {
            content: attr(title);
            position: absolute;
            top: 90%;
            color: #000;
            background-color: #fff;
            border: 1px solid;
            width: fit-content;
            padding: 3px;
          }
        }
    

    /*Semantic Styling*/
    body {
      display: grid;
      place-items: center;
      text-align: center;
      height: 100vh;
    }
    
    a {
      height: 40px;
      width: 200px;
      background: #fa4766;
      color: #fff;
      text-decoration: none;
      padding: 10px;
      box-sizing: border-box;
      border-radius: 10px;
    }
    
    /*Functional Styling*/
    @media (pointer: coarse), (hover: none) {
      [title] {
        position: relative;
        display: flex;
        justify-content: center;
      }
      [title]:focus::after {
        content: attr(title);
        position: absolute;
        top: 90%;
        color: #000;
        background-color: #fff;
        border: 1px solid;
        width: fit-content;
        padding: 3px;
      }
    
    }
    <a title="this is the Title text" tabindex="0">Tag with Title</a>

    Obviously, you'll need to open this on a mobile device to test it. Here is a Pen with the same code.

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