Home > Web Front-end > JS Tutorial > JQuery method to achieve hyperlink mouse prompt effect_jquery

JQuery method to achieve hyperlink mouse prompt effect_jquery

WBOY
Release: 2016-05-16 15:55:51
Original
1434 people have browsed it

The example in this article describes how JQuery implements the mouse prompt effect of hyperlinks. Share it with everyone for your reference. The specific analysis is as follows:

The browser actually already comes with a hyperlink prompt. You only need to add the title attribute to the hyperlink. However, the response speed of this prompt effect is very slow, with a delay of about 1 second. What we need now is to have a prompt appear the moment the mouse moves to the hyperlink. At this time, you need to remove the title prompt effect in the a tag and make a prompt with similar functions yourself.

HTML design is as follows:

Copy code The code is as follows:

Then add mouseover and mouseout events for the hyperlink with class tooltip:

$("a.tooltip").mouseover(function (){  
    //显示 title  
}).mouseout(function (){  
    //隐藏 title  
});

Copy after login

The specific ideas to achieve this effect are as follows:

1. When the mouse slides into the hyperlink, create a div element, and the content of the div element is the value of the title attribute. The created element is then appended to the document. Set the x and y coordinates for it so that it displays next to the mouse position.
2. When the mouse slides out of the hyperlink, remove the div element.

According to the analysis ideas, write the following JQuery code:

$(function(){  
  var x = 10;   
  var y = 20;  
  $("a.tooltip").mouseover(function(e){  
    this.myTitle = this.title;  
    this.title = "";    
    var tooltip = "<div id='tooltip'>"+ this.myTitle +"<\/div>";
    //创建 div 元素  
    $("body").append(tooltip); //把它追加到文档中  
    $("#tooltip")  
      .css({  
        "top": (e.pageY + y) + "px",  
        "left": (e.pageX + x) + "px" 
      }).show("fast"); //设置x坐标和y坐标,并且显示  
  }).mouseout(function(){    
    this.title = this.myTitle;  
    $("#tooltip").remove(); //移除  
  });  
});
Copy after login

There are two problems with the effect at this time: firstly, when the mouse slides over, the prompt of the title attribute in the a tag will also appear; secondly, the problem of setting the x coordinate and y coordinate, due to the distance between the self-made prompt and the mouse Too close may sometimes cause problems such as being unable to prompt (the mouse focus change causes a mouseout event).

In order to remove the title prompt function in the a tag, you need to perform the following steps:

1. When the mouse slides in, add a new attribute to the object, pass the value of title to this attribute, and then clear the value of the attribute title.

this.myTitle = this.title;  
s.title = "";   
var tooltip = "<div id='tooltip'>"+ this.myTitle +"<\/div>";
//创建 div 元素

Copy after login

2. When the mouse slides out, the value of the myTitle attribute of the object is assigned to the attribute title.

Copy code The code is as follows:
this.title = this.myTitle;

Why should the attribute value be assigned to the attribute title when the mouse slides out? Because when the mouse slides out, the value of the attribute title when sliding in again needs to be considered. If the value of myTitle is not reassigned to the title attribute, when the mouse slides in again, the value of title will be empty.

In order to solve the second problem, you need to reset the top and left values ​​​​of the prompt element. The code is as follows, adding 10px to top and 20px to left:

var x = lO;  
var y = 20;  
$("#tooltip").css({  
    "top": (e.pageY+y) + "px",  
    "left": (e.pageX+x) + "px" 
});

Copy after login

OK, the problem is solved here, and the mouse hyperlink prompt effect is realized.

I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template