search
HomeWeb Front-endJS TutorialMootools 1.2教程 Tooltips_Mootools

We'll also take a closer look at tooltip options and events, as well as some tools for adding and removing tooltips from elements. Finally, we will learn how to have multiple tooltips in different styles on one page.
The Basics
Creating a new tooltip
Creating a new tooltip is easy. First, let’s create a link to which we want to add a tooltip:
Reference code:

Copy the code The code is as follows:

MooTools 1.2 tooltips will display the values ​​of the title and rel attributes in links by default. If there is no rel attribute, the href attribute value will be displayed.
Now create a new default toolbar tip:
Reference code:
Copy code The code is as follows:

var customTips = $$('.tooltipA');
var toolTips = new Tips(customTips);

Since no styles are used, you will see to a tooltip like this:
Tool tip 1
Use styles for your tooltips
MooTools gives you a lot of control over its output - let’s take a look at the html code for the tooltip :
Reference code:
Copy code The code is as follows:

// You can Specify
in options >



Pay attention to the top and bottom divs. You can use them to easily add rounded corners or other styling effects to the top and bottom.
Now, let’s create our first option and add some CSS. The above html code will be rendered with a CSS style named "options.className". By giving our tooltip a CSS class name, we can give it an independent style without affecting other MooTools tooltips on the page. Reference code:



Copy code The code is as follows: var customTipsB = $$( '.tooltipB');
var toolTipsB = new Tips(customTipsB, {
className: 'custom_tip'
});


Finally, we add some more CSS:
Reference code:



Copy code The code is as follows: .custom_tip .tip {
background-color: #333
padding: 5px
}
.custom_tip .tip-title {
color: #fff
background-color: #666
font-size: 20px
padding: 5px
}
.custom_tip .tip-text {
color: #fff
padding: 5px
}


Tool tip 2
Options
There are only five options in total in the Tips category, each of which is very self-explanatory (that is, you can understand what it means at a glance).
showDelay
The default value is 100
An integer in milliseconds that will determine how long the tooltip will wait after the mouse is moved over the element.
hideDelay
The default value is 100
Same as showDelay above, but this value (also in milliseconds) will determine how long it takes to hide the tooltip when the mouse leaves the element.
className
Defaults to null
As you can see in the example above, this allows you to set a CSS class name for the tooltip container.
offsets
The default is x:16, y:16
This will determine the distance of the tooltip from your element, the x value is the distance to the right of the element, and the y value is the distance from the element down ( If the fixed option is specified as false, the distance will be relative to the mouse pointer, otherwise it will be the distance relative to the element).
fixed
Defaults to false
This setting determines whether the tooltip follows the mouse when your mouse moves over the element. If set to true, the tooltip will not move with the movement of the mouse pointer, but will just stay at a fixed position near the element.
Events
Like everything else in this class, tooltip events are still very simple. It has two events: onShow and onHide, which will work as you expect.
onShow
This event will be triggered when the toolbar is displayed. If you set a delay, this event will fire until the tooltip is displayed.
onHide
is the same as the onShow event above, but it is triggered when the tool tip is hidden. If a delay is set, this event will also fire until the tooltip is hidden.
Methods
The Tips class has two methods - attach and detach. Through these two methods, you can add a tool tip to a specified element (of course, these tool tips will have the same settings), Or remove the tooltip from a specific element.
.attach();
To add a tooltip to a new element, you only need to add .attach(); after the Tip object, and finally put the selector of this element in parentheses .
Reference code:
Copy code The code is as follows:

toolTips.attach('# tooltipID3');

.dettach();
This method is the same as the .attach method, but their behavior is completely opposite. First, write the Tip object, then add .dettach(); after the element, and finally put the selector of this element in parentheses.
Reference code:
Copy code The code is as follows:

toolTips.dettach('# tooltipID3');

Code Example
In this example, we will create two different instances of the Tip plugin so that we can have two different styles of tooltips. We will also integrate the options, events and methods we saw above.
Reference code:
Copy code The code is as follows:

var customTips = $$('.tooltip');
var toolTips = new Tips(customTips, {
// This will set the delay time for tooltip display
showDelay: 1000 , // The default is 100
// This will set the delay event for tooltip hiding
hideDelay: 100, // The default is 100
// This will add a CSS style to the tooltip's container div
// This way you can have two different styles of toolbar tips on one page
//
className: 'anything', // The default is null
// This will set x and y Offset values ​​for
offsets: {
'x': 100, // Default is 16
'y': 16 // Default is 16
},
// This will set the tool Prompt whether to follow the mouse
// Set to true and the mouse will not be followed
fixed: false, // The default is false
// If you call this function outside the options
// and put This function stays here
// It flashes and has a smooth gradient effect
onShow: function(toolTipElement){
// Pass in the tooltip object
// You can make them Fade to fully opaque
// Or make them a little transparent
toolTipElement.fade(.8);
$('show').highlight('#FFF504');
},
onHide: function(toolTipElement){
toolTipElement.fade(0);
$('hide').highlight('#FFF504');
}
});
var toolTipsTwo = new Tips('.tooltip2', {
className: 'something_else', // The default is null
});
// You can use the .store(); method to change rel The value of
// thereby changing the value of the tool tip
// You can use the following code
$('tooltipID1').store('tip:text', 'You can replace the href with whatever text you want.');
$('tooltipID1').store('tip:title', 'Here is a new title.');
// The following code will not change the tool tip The text of
$('tooltipID1').set('rel', 'This will not change the tooltips text');
$('tooltipID1').set('title', 'This will not change the tooltips title');
toolTips.detach('#tooltipID2');
toolTips.detach('#tooltipID4');
toolTips.attach('#tooltipID4');

Tool tip 1

Tool tip is detached

Tool tip 3

Tool tip detached then attached again.

A differently styled tool tip

Learn more

Read through the Tips section in the MooTools documentation. In addition, here is a very good article written by David Walsh about Customizing Mootools Tips.

Download a zip package containing everything you need

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.