jquery click text content to enlarge and center the example sharing
This article mainly introduces jquery to you in detail. Click on text or picture content to enlarge and display it in the center. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
The effect we want to achieve is:
Click a small picture and a large picture will be displayed in the center of the page.
The animate animation function is used, and there is a trajectory from the small picture to the large picture, and from the small picture position to the center position.
Supports IE7 and above browsers, Firefox, and Google Chrome.
To get the large picture to be centered, I mainly used the following code:
var width=$('.alert').find('img').width();//大图得宽高 var height=$('.alert').find('img').height(); var lwidth=$(window).width();//屏幕中页面可见区域的宽高 var lheight=$(window).height(); var x2=lwidth/2-width/2+$(window).scrollLeft();//在屏幕居中的坐标 var y2=lheight/2-height/2+$(window).scrollTop();
The width and height of the scroll bar are added here, so that the width and height of the scroll bar can be Even if there is a scroll bar, it will be displayed in the center.
The main code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>单击文字或图片内容放大显示</title> <script src="../jquery-1.8.3.min.js"></script> <style> *{margin:0;padding:0;} ul{overflow:hidden;list-style:none;margin:1000px auto;} ul li{float:left;height:150px;width:130px;margin:0 10px;} .bigpic{position:absolute;display:none;} .alert{background:#fff;border:solid #ccc 1px;padding:10px;} .alert a.close{position:absolute;top:0;right:0;} </style> </head> <body> <ul> <li><img src="mm1.jpg"></li> <li><img src="mm2.jpg"></li> <li><img src="mm3.jpg"></li> <li><img src="mm4.jpg"></li> </ul> <p class="bigpic" style="display:none;"> <p class="pic-one"><img src="m1.jpg"></p> <p class="pic-two"><img src="m2.jpg"></p> <p class="pic-three"><img src="m3.jpg"></p> <p class="pic-four"><img src="m4.jpg"></p> </p> <p class="alert" style="display:none;"> <a class="close" href="javascript:;" rel="external nofollow" >关闭</a> </p> <script> var x=0; var y=0; $('ul li').click(function(e){ var index=$(this).index(); x= e.pageX|| e.clientX+$(window).scrollLeft();//鼠标点击的坐标 y= e.pageY|| e.clientY+$(window).scrollTop(); $('.alert').css({position:'absolute',top:y,left:x,width:'1px',height:'1px',overflow:'hidden'}); var bigpic=$('.bigpic').find('p').eq(index).find('img').attr('src');//找到相对应的大图片 $('.alert').find('img').remove(); $('.alert').append("<img src="+bigpic+">");//添加大图 $('.alert').show(); var width=$('.alert').find('img').width();//大图得宽高 var height=$('.alert').find('img').height(); var lwidth=$(window).width();//屏幕页面可见区域的宽高 var lheight=$(window).height(); var x2=lwidth/2-width/2+$(window).scrollLeft();//在屏幕居中的坐标 var y2=lheight/2-height/2+$(window).scrollTop(); $('.alert img').css({width:'100%'}); $('.alert').animate({left:x2,top:y2,width:width,height:height},300); }) //这出现一个问题,当alert宽度和高度都为15px时或以下,如果不加padding,img是100%,就会造成图片不是从左上角开始的,上面就会有空白,这是因为父元素是块状元素,有自己的行间距,二他的子元素是行内元素,这样就会有空隙,此时解决方法有两个, // 给img加上display:block属性,形成块状元素; // 或者img还是内联元素,此时使用vertical-align:top可以向上对齐。 //把父元素的间距设置为0,或者父元素的font-size设置为0yekeyi $('.alert a.close').click(function(){ //console.log(x+'"'+y); $('.alert').animate({left:x,top:y,width:'1px',height:'1px'},300); //全局变量 $('.alert').fadeOut(100); }) </script> </body> </html>
The effect can be copied and viewed on the page.
Related recommendations:
JS implementation example sharing of image centering and floating effect
CSS image centering method with non-fixed size
The above is the detailed content of jquery click text content to enlarge and center the example sharing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to change the page that opens the Microsoft Edge browser to 360 navigation? It is actually very simple, so now I will share with you the method of changing the page that opens the Microsoft Edge browser to 360 navigation. Friends in need can take a look. I hope Can help everyone. Open the Microsoft Edge browser. We see a page like the one below. Click the three-dot icon in the upper right corner. Click "Settings." Click "On startup" in the left column of the settings page. Click on the three points shown in the picture in the right column (do not click "Open New Tab"), then click Edit and change the URL to "0" (or other meaningless numbers). Then click "Save". Next, select "

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

CheatEngine is a game editor that can edit and modify the game's memory. However, its default language is non-Chinese, which is inconvenient for many friends. So how to set Chinese in CheatEngine? Today, the editor will give you a detailed introduction to how to set up Chinese in CheatEngine. I hope it can help you. Setting method one: 1. Double-click to open the software and click "edit" in the upper left corner. 2. Then click “settings” in the option list below. 3. In the opened window interface, click "languages" in the left column

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Do you know where to set the download button to display in Microsoft Edge? Below, the editor will bring you the method to set the download button to display in Microsoft Edge. I hope it will be helpful to you. Let’s follow the editor to learn it! Step 1: First open Microsoft Edge Browser, click the [...] logo in the upper right corner, as shown in the figure below. Step 2: Then click [Settings] in the pop-up menu, as shown in the figure below. Step 3: Then click [Appearance] on the left side of the interface, as shown in the figure below. Step 4: Finally, click the button on the right side of [Show Download Button] and it will change from gray to blue, as shown in the figure below. The above is where the editor brings you how to set up the download button in Microsoft Edge.

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
