abstrait:1、jQuery動(dòng)畫知識(shí)點(diǎn)總結(jié)<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0&q
1、jQuery動(dòng)畫知識(shí)點(diǎn)總結(jié)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<title>jQuery的動(dòng)畫效果——自定義動(dòng)畫</title>
<style type="text/css">
div {
width: 200px;
height: 200px;
background: blue;
position: absolute;
color:#fff;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('.but1').click(function() {
$('p').animate({
fontSize: '40px'
}, 1500) //屬性名稱font-size一律改成駝峰寫法:fontsize
})
//同時(shí)操作多個(gè)css屬性
//使用預(yù)定義的值:show hide toggle
$('.but2').click(function() {
$('div').animate({
// left: '200px',//如果想對(duì)元素位置進(jìn)行一個(gè)位移,那么我們需要給當(dāng)前元素設(shè)置一個(gè)position屬性,例如:position: absolute;
// opacity:'0.3',
// height:'400px',
// width:'400px'
width:'toggle'
}, 1500) //屬性名稱font-size一律改成駝峰寫法:fontsize
})
$('#right').click(function(){
$('.box1').animate({left:'+500px'},3000)
$('.box1').animate({fontSize:'50px'},3000)
})
$('#stop').click(function(){
$('.box1').stop(true,true)
})
})
</script>
</head>
<body>
<!-- jQuery中我們使用animate()方法創(chuàng)建自定義的動(dòng)畫
語(yǔ)法:$(selector).animate({param},speed,fn)
必需的params參數(shù)定義形成動(dòng)畫的CSS屬性
可選的speed參數(shù)規(guī)定效果的時(shí)長(zhǎng),它可以取以下值:"slow","fast",或毫秒
可選的fn 是動(dòng)畫完成后所執(zhí)行的函數(shù)
stop()方法用于同志動(dòng)畫或效果,在它們完成之前,該方法適用于所有jQuery效果函數(shù),包括滑動(dòng)淡入淡出和自定義動(dòng)畫
語(yǔ)法:$(selector).stop(stopAll,goToEnd)
可選的參數(shù)stopAll規(guī)定是否應(yīng)該清除動(dòng)畫隊(duì)列,默認(rèn)是false僅停止活動(dòng)的動(dòng)畫,允許任何排入隊(duì)列的動(dòng)畫向后執(zhí)行
可選的參數(shù)goToEnd規(guī)定是否立即完成當(dāng)前動(dòng)畫。默認(rèn)是false
默認(rèn)情況下stop()會(huì)清除在被選元素上指定的當(dāng)前動(dòng)畫 -->
<!-- <button>點(diǎn)擊字體放大</button>
<p>jQuery中我們使用animate()方法創(chuàng)建自定義的動(dòng)畫</p>
<button>點(diǎn)擊移動(dòng)div</button>
<div></div> -->
點(diǎn)擊右移按鈕,div右移,點(diǎn)擊停止按鈕當(dāng)前效果停止
<br>
<button id="right">右移</button>
<button id="stop">停止</button>
<div>php中文網(wǎng)</div>
</body>
</html>
2、動(dòng)畫效果的導(dǎo)航(jQuery下劃線動(dòng)畫導(dǎo)航特效)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery下劃線動(dòng)畫導(dǎo)航特效</title>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
background: #1A1E23;
font-family: "Lato", sans-serif;
-webkit-font-smoothing: antialiased;
}
nav {
position: relative;
padding-bottom: 12px;
}
nav .line {
height: 2px;
position: absolute;
bottom: 0;
margin: 10px 0 0 0;
background: #FF1847;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
display: flex;
}
nav ul li {
margin: 0 40px 0 0;
opacity: 0.4;
transition: all 0.4s ease;
}
nav ul li:hover {
opacity: 0.7;
}
nav ul li.active {
opacity: 1;
}
nav ul li:last-child {
margin-right: 0;
}
nav ul li a {
text-decoration: none;
color: #fff;
text-transform: uppercase;
display: block;
font-weight: 600;
letter-spacing: 0.2em;
font-size: 14px;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 50vh;
}
</style>
</head>
<body>
<nav>
<ul>
<li class="active"><a href="">網(wǎng)站首頁(yè)</a></li>
<li><a href="">關(guān)于我們</a></li>
<li><a href="">產(chǎn)品介紹</a></li>
</ul>
</nav>
<script>
var nav = $('nav');
var line = $('<div />').addClass('line');
line.appendTo(nav);
var active = nav.find('.active');
var pos = 0;
var wid = 0;
if(active.length) {
pos = active.position().left;
wid = active.width();
line.css({
left: pos,
width: wid
});
}
nav.find('ul li a').click(function(e) {
if(!$(this).parent().hasClass('active')) {
e.preventDefault();
var _this = $(this);
nav.find('ul li').removeClass('active');
var position = _this.parent().position();
var width = _this.parent().width();
if(position.left >= pos) {
line.animate({
width: ((position.left - pos) + width)
}, 300, function() {
line.animate({
width: width,
left: position.left
}, 150);
_this.parent().addClass('active');
});
} else {
line.animate({
left: position.left,
width: ((pos - position.left) + wid)
}, 300, function() {
line.animate({
width: width
}, 150);
_this.parent().addClass('active');
});
}
pos = position.left;
wid = width;
}
});
</script>
</body>
</html>
Professeur correcteur:滅絕師太Temps de correction:2019-01-23 09:09:29
Résumé du professeur:練習(xí)的比較全面,知識(shí)點(diǎn)歸納的很好,繼續(xù)保持哦