本篇文章介绍了使用jquery实现网站导航抖动效果的方法,主要用到了each遍历节点和animate自定义动画,希望对学习jquery的朋友有帮助!

使用jQuery实现网站导航抖动效果
知识点
1、each遍历节点
2、 animate()自定义动画
jQuery 1.12.4
jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
下载
代码
html>
<meta>
<title></title>
<style>
* {
padding: 0;
margin: 0;
list-style: none;
}
.box {
width: 350px;
height: 350px;
margin: 100px auto;
cursor: pointer;
}
.box ul li {
float: left;
width: 80px;
height: 80px;
text-align: center;
border: 1px solid #ccc;
box-sizing: border-box;
margin: 2px;
}
.box>ul>li>span {
display: block;
width: 24px;
height: 24px;
background: url("images/bg.png") 0 -24px no-repeat;
margin: 10px auto;
}
</style>
<p>
</p>
<script></script><script>
$(function () {
// 1. 展示图片
var $li = $('.box>ul>li');
$li.each(function (index, value) {
$(this).children('span').css({
'background': ' url("images/bg.png") 0 -' + index * 24 + 'px no-repeat'
})
});
// 2. 抖动动画
$li.hover(function () {
shake(this);
}, function () {
// 停止抖动
stopShake(this);
});
function shake(ele) {
// 1. 设置css
$(ele).css({
'position': 'relative'
});
// 2. 确定走动的值
var animateLeft = $(ele).css('left') === '10px' ? '-10px' : '10px';
$(ele).animate({
left: animateLeft }, 100, function () {
shake(ele);
});
}
function stopShake(ele) {
$(ele).stop(true, false).css({
left: '0'
})
}
});</script>
运行结果
鼠标放上后会不停抖动


// 停止抖动
stopShake(this);
});
function shake(ele) {
// 1. 设置css
$(ele).css({
'position': 'relative'
});
// 2. 确定走动的值
var animateLeft = $(ele).css('left') === '10px' ? '-10px' : '10px';
$(ele).animate({
left: animateLeft }, 100, function () {
shake(ele);
});
}
function stopShake(ele) {
$(ele).stop(true, false).css({
left: '0'
})
}
});