Bootstrap JS Tooltip


เคล็ดลับเครื่องมือ JS (tooltip.js)

ปลั๊กอินคำแนะนำเครื่องมือคือกล่องป๊อปอัปขนาดเล็กที่ปรากฏขึ้นเมื่อผู้ใช้เลื่อนตัวชี้เมาส์ไปที่องค์ประกอบ

สำหรับคำแนะนำเกี่ยวกับ Tooltips โปรดอ่านBootstrap Tooltip Tutorialของเรา


ผ่าน data-* คุณสมบัติ

เปิดdata-toggle="tooltip"ใช้งานคำแนะนำเครื่องมือ

แอตทริบิวต์titleระบุข้อความที่ควรจะแสดงภายในคำแนะนำเครื่องมือ

ตัวอย่าง

<a href="#" data-toggle="tooltip" title="Hooray!">Hover over me</a>

ผ่าน JavaScript

คำแนะนำเครื่องมือไม่ใช่ปลั๊กอินเฉพาะ CSS และต้องเริ่มต้นด้วย jQuery: เลือกองค์ประกอบที่ระบุและเรียกใช้tooltip()เมธอด

ตัวอย่าง

// Select all elements with data-toggle="tooltips" in the document
$('[data-toggle="tooltip"]').tooltip();

// Select a specified element
$('#myTooltip').tooltip();

ตัวเลือกคำแนะนำเครื่องมือ

ตัวเลือกสามารถส่งผ่านแอตทริบิวต์ข้อมูลหรือ JavaScript สำหรับแอตทริบิวต์ข้อมูล ให้ผนวกชื่อตัวเลือกต่อ data- เช่นเดียวกับใน data-placement=""

Name Type Default Description Try it
animation boolean true

Specifies whether to add a CSS fade transition effect when showing and hiding the tooltip

  • true - Add a fading effect
  • false - Do not add a fading effect
container string, or the boolean false false Appends the tooltip to a specific element.
Example: container: 'body'
delay number, or object 0 Specifies the number of milliseconds it will take to show and hide the tooltip.

To specify a delay for showing and another one for hiding, use the object structure:

delay: {show: 500, hide: 100} - which will take 500 ms to show the tooltip, but only 100 ms to hide it
html boolean  false Specifies whether to accept HTML tags in the tooltip:
 
  • true - Accept HTML tags
  • false - Do not accept HTML tags
Note: The HTML must be inserted in the title attribute (or using the title option).

When set to false (default), jQuery's text() method will be used. Use this if you are worried about XSS attacks
placement string "top" Specifies the tooltip position. Possible values:

  • "top" - Tooltip on top
  • "bottom" - Tooltip on bottom
  • "left" - Tooltip on left
  • "right" - Tooltip on right
  • "auto" - Lets the browser decide the position of the tooltip. For example, if the value is "auto left", the tooltip will display on the left side when possible, otherwise on the right. If the value is "auto bottom", the tooltip will display at the bottom when possible, otherwise on the top
selector string, or the boolean false false Adds the tooltip to a specified selector
template string   Base HTML to use when creating the tooltip.

The tooltip's title will be inserted into the element having the class .tooltip-inner and the element with the class .tooltip-arrow will become the tooltip's arrow.

The outermost wrapper element should have the .tooltip class.
title string "" Specifies the text that should be displayed inside the tooltip
trigger string "hover focus" Specifies how the tooltip is triggered. Possible values:

  • "click" - Trigger the tooltip with a click
  • "hover" - Trigger the tooltip on hover
  • "focus" - Trigger the tooltip when it gets focus (by tabbing or clicking .e.g)
  • "manual" - Trigger the tooltip manually
Tip: To pass multiple values, separate them with a space
viewport string, or object {selector: "body", padding: 0} Keeps the tooltip within the bounds of this element.

Example: viewport: '#viewport' or {selector: '#viewport', padding: 0}


วิธีการแนะนำเครื่องมือ

ตารางต่อไปนี้แสดงรายการคำแนะนำเครื่องมือที่มีอยู่ทั้งหมด

Method Description Try it
.tooltip(options) Activates the tooltip with an option. See options above for valid values
.tooltip("show") Shows the tooltip
.tooltip("hide") Hides the tooltip
.tooltip("toggle") Toggles the tooltip
.tooltip("destroy") Hides and destroys the tooltip

เหตุการณ์เคล็ดลับเครื่องมือ

ตารางต่อไปนี้แสดงรายการเหตุการณ์คำแนะนำเครื่องมือที่มีอยู่ทั้งหมด

Event Description Try it
show.bs.tooltip Occurs when the tooltip is about to be shown
shown.bs.tooltip Occurs when the tooltip is fully shown (after CSS transitions have completed)
hide.bs.tooltip Occurs when the tooltip is about to be hidden
hidden.bs.tooltip Occurs when the tooltip is fully hidden (after CSS transitions have completed)

ตัวอย่างเพิ่มเติม

การออกแบบคำแนะนำเครื่องมือแบบกำหนดเอง

ใช้ CSS เพื่อปรับแต่งรูปลักษณ์ของคำแนะนำเครื่องมือ:

ตัวอย่าง

/* Tooltip */
.test + .tooltip > .tooltip-inner {
  background-color: #73AD21;
  color: #FFFFFF;
  border: 1px solid green;
  padding: 15px;
  font-size: 20px;
}

/* Tooltip on top */
.test + .tooltip.top > .tooltip-arrow {
  border-top: 5px solid green;
}

/* Tooltip on bottom */
.test + .tooltip.bottom > .tooltip-arrow {
  border-bottom: 5px solid blue;
}

/* Tooltip on left */
.test + .tooltip.left > .tooltip-arrow {
  border-left: 5px solid red;
}

/* Tooltip on right */
.test + .tooltip.right > .tooltip-arrow {
  border-right: 5px solid black;
}