


Sharing two ways to realize the animation effect of picture sliding door with jQuery
''Sliding door'' dynamic effect can also be called the "accordion" effect. The idea of realizing most effects is basically the same. Two methods are introduced below. One is to move the picture by changing the offset position. The other is to achieve transformation by traversing the background image and then changing the width of the image. The two methods in this article achieve the "sliding door" animation effect, which can also be called the accordion effect. Let's learn the specific implementation methods through this article. I hope it can help everyone.
Implementation method one: change the image width
html+css code
<body> <p class="box"> <ul> <!-- <li></li> --> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </p> </body> <style> *{ padding: 0; margin: 0; } .box{ /*收缩状态:缩放时每个图片的大小240px 所以总大小1200px*/ /*展开状态:当前图片宽度800px 其他图片宽度100px*/ width: 1200px; height: 500px; border:1px solid red; margin: 50px auto; } .box ul{ list-style: none; width: 1210px; } /*设置每一张图片的大小和float: left*/ .box ul li{ width: 240px; height: 500px; /*background: url(images/slidepic2.jpg);*/ float: left; } </style>
jQuery implementation
<script src = 'jquery-3.2.1.js'></script> <script> $(function(){ //1遍历每一张li 获取每个元素设置对应的图片 var lis = $('li'); lis.each(function(index, element){ //通过设置背景图片名称改变图片的显示 var imgName = "images/slidepic" + (index + 2) +".jpg "; $(element).css('background', "url('"+ imgName +"')") }); //2.展开状态 //鼠标滑入改变对应图片宽度800 其他图片(兄弟)改为100 lis.mouseenter(function(){ // console.log(this); 当前的li DOM元素 //当前的图片的宽度变为800 $(this).stop().animate({width: 800}); //其他图片的宽度变为100 $(this).siblings('li').stop().animate({width: 100}); }); //3鼠标滑出是全部显示为收缩状态 lis.mouseout(function(){ lis.stop().animate({width: 240}); }); }) </script>
jQuery streamlined code
//精简代码 $(function(){ $('li').each(function(index, element){ $(element).css('backgroud',"url('images/slidepic"+(index + 2)+.jpg')"); }).mouseenter(function(){ $(this).stop().animate({width: 800}).siblings().stop().aniamte(width: 100}); }).mouseout(function(){ $('li').stop().animate({width: 240}); }); })
Implementation method two: change the offset value of the image
html+css code
<body> <p class="picList"> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </p> </body> <style> *{ background-color: #aaa; padding: 0; margin: 0; } ul{list-style: none;} .picList{ width: 1000px; height: 400px; /*border:1px solid #eee;*/ margin:100px auto; position: relative; overflow: hidden; } /*设置定位属性 所有图片覆盖在起始位置*/ .picList ul li{ position: absolute; width: 1000px; height: 400px; top: 0; } img{ width: 100%; height: 400px; cursor: pointer; } </style>
jQuery implementation
<script src = 'jquery-3.2.1.js'></script> <script > $(function(){ //1获取所有的图片 设置初始的收缩状态left:i*200 var lis = $('li'); for(var i = 0; i < lis.length; i++){ lis.eq(i).css({left:i*200 + 'px' }); } //2.设置hover内置函数,实现鼠标滑入展开滑出收缩效果 lis.hover(function(){ var index = $(this).index(); //DOM对象转换jQuery对象 //2.1鼠标滑入后,当前图片的前面图片偏移位置减小到 j*100位置 for(var j = 0; j <= index; j++){ lis.eq(j).stop().animate({left: j*100 + 'px'},300); } //2.2鼠标滑入后,当前图片的后面图片偏移位置扩大到 500+j*100位置 for(var j = index + 1; j < lis.length; j++){ lis.eq(j).stop().animate({left: 500+j*100 + 'px'},300); } },function(){ //2.3鼠标滑出后,所有图片恢复到原来的位置 i*200 for(var i = 0; i < lis.length; i++){ lis.eq(i).stop().animate({left: i*200 + 'px'},300); } }); }) </script>
Note: During the implementation of method one, pay attention to the width and image Named settings.
Tips: The jQuery code is used here, and the javaScript code can be implemented in the same way. Just modify the traversal process and built-in function methods, and rewrite the animation function (the previous notes have an encapsulated animate function , can be directly introduced and used).
Related recommendations:
Detailed examples of how to realize the picture sliding door animation effect based on jQuery
How to create a web page with opening animation effect in Dreamweaver
The above is the detailed content of Sharing two ways to realize the animation effect of picture sliding door with jQuery. 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

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

We often use ppt in our daily work, so are you familiar with every operating function in ppt? For example: How to set animation effects in ppt, how to set switching effects, and what is the effect duration of each animation? Can each slide play automatically, enter and then exit the ppt animation, etc. In this issue, I will first share with you the specific steps of entering and then exiting the ppt animation. It is below. Friends, come and take a look. Look! 1. First, we open ppt on the computer, click outside the text box to select the text box (as shown in the red circle in the figure below). 2. Then, click [Animation] in the menu bar and select the [Erase] effect (as shown in the red circle in the figure). 3. Next, click [

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

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: <

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

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:

Sometimes we encounter the need to add animation to a ppt. For example, if we want to make a thunderstorm ppt and add some animated thunderstorm effects to it, what should we do? Today, the editor will introduce to you how to make an animated thunderstorm in thunderstorm ppt. It is actually very simple, come and learn it! 1. First we open a PPT page, "Insert" - "Shape" - "Basic Shape" - "Lightning Shape", as shown in the picture. 2. In the "Fill and Line" tab on the right, select "Fill": white; "Select" "Line": black, as shown in the figure. 3. Click "Animation" - "Custom Animation" - "Add Effects" - "Emphasis" - "Subtle" - "Flickering",

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
