Home > Web Front-end > JS Tutorial > jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery

jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery

WBOY
Release: 2016-05-16 15:21:08
Original
1373 people have browsed it

This chapter introduces a commonly used effect, that is, when the mouse slides over a link, a layer that follows the movement of the mouse pointer can appear. In practical applications, it is usually some explanatory text or pictures for the link, etc. Wait,

Let’s take a look at a demo picture first

The following is a code example:

<link rel="stylesheet" href="../css/common.css" type="text/css" />
<script type="text/javascript" src="../js/jquery-1.2.6.pack.js"></script>
<script type="text/javascript" src="../js/jquery.imagePreview.1.0.js"></script>
<script type="text/javascript">
$(function(){
	$("a.preview").preview();	 
});
</script>
<style type="text/css">
html{overflow-y:scroll;}
a.preview,a.preview:hover{text-decoration:none;}
a.preview img{margin:20px 10px;}
</style>
</head>

<body>
<div class="zxx_out_box">
 <div class="zxx_in_box">
  <h3 class="zxx_title">图片放大显示的jQuery插件演示页面</h3>
  <div class="zxx_main_con">
			<div class="zxx_test_list">
   	<a class="preview" href="http://image.jb51.net/image/study/s/s256/mm1.jpg" title="张含韵">
    	<img src="http://image.jb51.net/image/study/s/s128/mm1.jpg" />
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm2.jpg" title="某不知名美女">
    	<img src="http://image.jb51.net/image/study/s/s128/mm2.jpg" />
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm3.jpg" title="某不知名美女">
    	<img src="http://image.jb51.net/image/study/s/s128/mm3.jpg" />
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm4.jpg" title="某不知名美女">
    	<img src="http://image.jb51.net/image/study/s/s128/mm4.jpg" />
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm5.jpg" title="某不知名美女">
    	<img src="http://image.jb51.net/image/study/s/s128/mm5.jpg" />
    </a>
   </div>   
  </div>

 </div>
</div>
</body>
</html>
Copy after login

The above code has achieved our requirements, what do you guys think?

Next let’s take a look at a brief description of how to use it:

1. You need to use the href attribute of the a tag. The principle of this jQuery plug-in is that when the mouse moves to the thumbnail (or link text), a large image html fragment containing the href pointing path will be loaded. This fragment is based on the mouse Position is absolutely positioned. This produces the effect of moving the mouse over the thumbnail to display the larger image. The address of the large image is the content of the href attribute of the a tag. For example: Thumbnail If the a tag contains a method to display a large image, the page will display the image "xx.jpg" pointed to by the href.

2. The method used is: target selector.preview(); For example, for the above thumbnail, you can use $("a"). preview(); This code implements the effect of displaying the image xx.jpg when the mouse is moved to the text link "Thumbnail".

3. Only four formats of images, png, gif, jpg, and bmp, are supported. You can modify the regular expression of the plug-in code to expand the supported image format types.

The following is a brief introduction to the implementation process:

1. Code comments:

1.this.screenshotPreview=function(){ }, declares a function to implement the following effect. In this effect, this can actually be omitted, it points to the window.

2.xOffset=10, declare a variable to specify the horizontal distance of the mouse pointer from the pop-up image.

3.yOffset=30, declare a variable to specify the vertical distance of the mouse pointer from the pop-up image.

4.$("a.screenshot").hover(function(e){}, function(e){}) specifies the function to be executed when the mouse moves to the link and leaves the link.

5.this.t = this.title, assign the title attribute value of the link to the t attribute, where this is the link object pointing to the current mouse hover.

6.var c = (this.t != "") ? "
" + this.t : "", if this.t is not empty, that is, the title attribute value exists, then Inserts a newline character and concatenates the current header content, otherwise sets c to empty.

7.$("body").append("

"+ c +""), add the image and related description to the body.

8.$("#screenshot").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px") .fadeIn("fast"), sets the top and left attribute values ​​​​of the p element, and displays it with a fade-in effect.

9.this.title=this.t, assign the title content to this.title. In fact, there is no problem without this sentence, it is a bit redundant.

10.$("#screenshot").remove(), remove the p element.

11.$("a.screenshot").mousemove(function(e){}), used to set the picture to follow when the mouse pointer moves.

12.$("#screenshot").css("top",(e.pageY-xOffset)+"px") .css("left",(e.pageX+yOffset)+"px") , setting the top and left attribute values ​​​​of the p element can achieve the following effect.

2. Related reading:

1. The hover() function can be found in the chapter jQuery’s hover event .
2. For the append() function, please refer to the chapter jQuery’s append() method .
3. For the css() function, please refer to the chapter jQuery’s css() method .
4. For the pageY attribute, please refer to the chapter of jQuery’s event.pageY attribute.
5. For the fadeIn() function, please refer to the chapter jQuery’s fadeIn() method.
6. For the remove() function, please refer to the chapter jQuery’s remove() method .
7. For the mousemove event, please refer to the chapter jQuery’s mousemove event .

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