This time I will bring you JS to realize the sliding door dynamic effect. What are the precautions for JS to realize the sliding door dynamic effect. The following is a practical case, let's take a look.
"Sliding door" implementation method one: change the picture width
html css code
<p> </p>
jQuery implementation
<script></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>
"Sliding door" implementation method two: change the offset value of the image
html css code
<p> </p>
- 
- 
- 
- 
- 
jQuery implementation
<script></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 1, pay attention to the settings of width and image naming.
Tip: 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, which can be directly introduced and used) .
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The above is the detailed content of JS realizes sliding door animation effect. For more information, please follow other related articles on the PHP Chinese website!

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


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

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

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

Atom editor mac version download
The most popular open source editor
