search
HomeWeb Front-endJS TutorialjQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery

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 id="图片放大显示的jQuery插件演示页面">图片放大显示的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="/static/imghwm/default1.png"  data-src="http://image.jb51.net/image/study/s/s128/mm1.jpg"  class="lazy" / alt="jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery" >
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm2.jpg" title="某不知名美女">
    	<img  src="/static/imghwm/default1.png"  data-src="http://image.jb51.net/image/study/s/s128/mm2.jpg"  class="lazy" / alt="jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery" >
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm3.jpg" title="某不知名美女">
    	<img  src="/static/imghwm/default1.png"  data-src="http://image.jb51.net/image/study/s/s128/mm3.jpg"  class="lazy" / alt="jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery" >
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm4.jpg" title="某不知名美女">
    	<img  src="/static/imghwm/default1.png"  data-src="http://image.jb51.net/image/study/s/s128/mm4.jpg"  class="lazy" / alt="jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery" >
    </a>
    <a class="preview" href="http://image.jb51.net/image/study/s/s256/mm5.jpg" title="某不知名美女">
    	<img  src="/static/imghwm/default1.png"  data-src="http://image.jb51.net/image/study/s/s128/mm5.jpg"  class="lazy" / alt="jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery" >
    </a>
   </div>   
  </div>

 </div>
</div>
</body>
</html>

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("

jQuery realizes the special effect of popping up the picture when the mouse slides over it_jquery"+ c +" p>"), 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 .

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: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.