Home > Web Front-end > JS Tutorial > body text

Implementation of jQuery's slideUp and slideDown animation effects

巴扎黑
Release: 2017-06-29 11:43:39
Original
1599 people have browsed it
<span style="color: #006600; font-style: italic; "><br></span>
Copy after login

jQuery 可以通过调用 animate 方法添加动画效果, 而且还提供了一套别名, 使用起来很是方便. 其中 slideDown和 slideUp 两方法的作用是纵向展开和卷起一个页面元素, 被使用的几率很高, 却一直存在一个小问题.

如果目标元素是被外部事件驱动, 当鼠标快速地连续触发外部元素事件, 动画会滞后的反复执行, 相当不美观  演示页面中有一个按钮, 请用鼠标迅速地来回划过...


如果用 jQuery 来实现这样的效果, 该如何处理呢?
其实很简单, 只需在触发元素上的事件设置为延迟处理, 即可避免滞后反复执行的问题. 例如: 当鼠标滑过按钮后 0.2 秒, 菜单才会展开, 如果鼠标离开按钮, 展开的处理将被终止. 也就是说, 想要展开菜单鼠标必须有 0.2 秒的事件停留在按钮上, 那么迅速地划过按钮是无法执行展开事件的. 卷起也是如此.

<span style="color: #006600; font-style: italic; "><br></span>
Copy after login
// 线程 IDs var mouseover_tid = [];
Copy after login
var mouseout_tid = [];
Copy after login
  jQuery(document).ready(function(){
Copy after login
	jQuery('#menus > li').each(function(index){ 	
Copy after login
	jQuery(this).hover(   			
Copy after login
// 鼠标进入 		
Copy after login
	function(){ 			
Copy after login
	var _self = this; 		
Copy after login
		// 停止卷起事件 		
Copy after login
		clearTimeout(mouseout_tid[index]); 			
Copy after login
	// 当鼠标进入超过 0.2 秒, 展开菜单, 并记录到线程 ID 中 		
Copy after login
		mouseover_tid[index] = setTimeout(function() { 	
Copy after login
				jQuery(_self).find('ul:eq(0)').slideDown(200); 		
Copy after login
		}, 400); 			},   		
Copy after login
	// 鼠标离开 			function(){ 		
Copy after login
		var _self = this; 			
Copy after login
	// 停止展开事件 			
Copy after login
	clearTimeout(mouseover_tid[index]); 	
Copy after login
	
Copy after login
// 当鼠标离开超过 0.2 秒, 卷起菜单, 并记录到线程 ID 中 			
Copy after login
	mouseout_tid[index] = setTimeout(function() { 			
Copy after login
		jQuery(_self).find('ul:eq(0)').slideUp(200); 		
Copy after login
		}, 400); 		
Copy after login
	}   		
Copy after login
); 	
Copy after login
});
Copy after login
 });
Copy after login

The above is the detailed content of Implementation of jQuery's slideUp and slideDown animation effects. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!