This article introduces the method of using jQuery to achieve the jitter effect of website navigation. It mainly uses each traversal node and animate custom animation. I hope it will be helpful to friends who are learning jQuery!

Using jQuery to achieve website navigation jitter effect
Knowledge points
1. each traverses nodes
2, animate() custom animation
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <!DOCTYPE html><html lang= "en" ><head>
<meta charset= "UTF-8" >
<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></head><body>
<p class = "box" >
<ul>
<li><span></span>百度</li>
<li><span></span>淘宝</li>
<li><span></span>新浪</li>
<li><span></span>网易</li>
<li><span></span>搜狐</li>
<li><span></span>腾讯</li>
<li><span></span>优酷</li>
<li><span></span>京东</li>
</ul>
</p><script type= "text/javascript" src= "lib/jquery-3.3.1.js" ></script><script type= "text/javascript" >
$( function () {
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'
})
});
$li .hover( function () {
shake(this);
}, function () {
stopShake(this);
});
function shake(ele) {
$(ele).css({
'position': 'relative'
});
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></body></html>
|
Copy after login
Run result
When the mouse is placed, it will Constant shaking


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
stopShake(this);
});
function shake(ele) {
$(ele).css({
'position': 'relative'
});
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></body></html>
|
Copy after login
Run result
The mouse will keep shaking after placing it


This article comes from the js tutorial column, welcome to learn!
The above is the detailed content of Use jQuery to achieve website navigation jitter effect. For more information, please follow other related articles on the PHP Chinese website!