1. css方法
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < style> div { width : 200px; height : 200px; background-color : red; } </ style> < script src = " ../jQuery.min.js" > </ script>
</ head>
< body> < div> 123</ div> < script> $ ( function ( ) { console. log ( $ ( 'div' ) . css ( 'width' ) ) ; $ ( 'div' ) . css ( { width : 400 , height : 400 , backgroundColor : 'blue' } ) ; } ) ; </ script>
</ body>
</ html>
2. 类操作样式
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < style> div { width : 100px; height : 100px; background-color : red; } .current { background-color : blue; } .one { color : purple; font-size : 20px; background-color : pink; } .two { color : blue; } </ style> < script src = " ../jQuery.min.js" > </ script> r
</ head>
< body> < div class = " current" > </ div> < span class = " one" > HelloWorld</ span> < script> $ ( function ( ) { $ ( 'div' ) . click ( function ( ) { $ ( this ) . toggleClass ( 'current' ) ; } ) ; } ) ; </ script> < script> var span = document. querySelector ( 'span' ) ; span. className = 'two' ; </ script>
</ body>
</ html>
3. 显示隐藏
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../jQuery.min.js" > </ script> < style> div { width : 200px; height : 200px; background-color : red; } </ style>
</ head>
< body> < button> 显示</ button> < button> 隐藏</ button> < button> 切换</ button> < div> </ div> < script> $ ( function ( ) { $ ( 'button' ) . eq ( 1 ) . click ( function ( ) { $ ( 'div' ) . hide ( 'slow' , function ( ) { alert ( 'callback,last execute!' ) ; } ) ; } ) ; $ ( 'button' ) . eq ( 0 ) . click ( function ( ) { $ ( 'div' ) . show ( 1000 ) ; } ) ; $ ( 'button' ) . eq ( 2 ) . click ( function ( ) { $ ( 'div' ) . toggle ( 1000 ) ; } ) ; } ) ; </ script>
</ body>
</ html>
4. 上拉下滑
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../jQuery.min.js" > </ script> < style> div { display : none; width : 200px; height : 200px; background-color : red; } </ style>
</ head> < body> < button> 下拉滑动</ button> < button> 上拉滑动</ button> < button> 切换滑动</ button> < div> </ div> < script> $ ( function ( ) { $ ( 'button' ) . hover ( function ( ) { $ ( 'div' ) . slideToggle ( 'slow' ) ; } ) ; } ) ; </ script>
</ body> </ html>
5. 停止动画队列
<! DOCTYPE html >
< html lang = " en" > < 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" > < title> Document</ title> < style> * { margin : 0; padding : 0; } li { list-style-type : none; } a { text-decoration : none; font-size : 14px; } .nav { margin : 100px; } .nav>li { position : relative; float : left; width : 80px; height : 41px; text-align : center; } .nav li a { display : block; width : 100%; height : 100%; line-height : 41px; color : #333; } .nav>li>a:hover { background-color : #eee; } .nav ul { display : none; position : absolute; top : 41px; left : 0; width : 100%; border-left : 1px solid #FECC5B; border-right : 1px solid #FECC5B; } .nav ul li { border-bottom : 1px solid #FECC5B; } .nav ul li a:hover { background-color : #FFF5DA; } </ style> < script src = " ../jQuery.min.js" > </ script>
</ head> < body> < ul class = " nav" > < li> < a href = " #" > 微博</ a> < ul> < li> < a href = " " > 私信</ a> </ li> < li> < a href = " " > 评论</ a> </ li> < li> < a href = " " > @我</ a> </ li> </ ul> </ li> < li> < a href = " #" > 微博</ a> < ul> < li> < a href = " " > 私信</ a> </ li> < li> < a href = " " > 评论</ a> </ li> < li> < a href = " " > @我</ a> </ li> </ ul> </ li> < li> < a href = " #" > 微博</ a> < ul> < li> < a href = " " > 私信</ a> </ li> < li> < a href = " " > 评论</ a> </ li> < li> < a href = " " > @我</ a> </ li> </ ul> </ li> < li> < a href = " #" > 微博</ a> < ul> < li> < a href = " " > 私信</ a> </ li> < li> < a href = " " > 评论</ a> </ li> < li> < a href = " " > @我</ a> </ li> </ ul> </ li> </ ul> < script> $ ( function ( ) { $ ( ".nav>li" ) . hover ( function ( ) { $ ( this ) . children ( "ul" ) . stop ( ) . slideToggle ( ) ; } ) ; } ) </ script>
</ body> </ html>
6. 淡入淡出效果
<! DOCTYPE html >
< html lang = " en" > < 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" > < title> Document</ title> < style> div { width : 150px; height : 300px; background-color : red; display : none; } </ style> < script src = " ../jQuery.min.js" > </ script>
</ head> < body> < button> 淡入效果</ button> < button> 淡出效果</ button> < button> 淡入淡出切换</ button> < button> 修改透明度</ button> < div> </ div> < script> $ ( function ( ) { $ ( "button" ) . eq ( 0 ) . click ( function ( ) { $ ( "div" ) . fadeIn ( 1000 ) ; } ) ; $ ( "button" ) . eq ( 1 ) . click ( function ( ) { $ ( "div" ) . fadeOut ( 1000 ) ; } ) ; $ ( "button" ) . eq ( 2 ) . click ( function ( ) { $ ( "div" ) . fadeToggle ( 1000 ) ; } ) ; $ ( "button" ) . eq ( 3 ) . click ( function ( ) { $ ( "div" ) . fadeTo ( 1000 , 0.66 ) ; } ) ; } ) ; </ script>
</ body> </ html>
7. 自定义动画
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta http-equiv = " X-UA-Compatible" content = " IE=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < title> Document</ title> < script src = " ../jQuery.min.js" > </ script> < style> div { position : absolute; width : 200px; height : 200px; background-color : red; } </ style>
</ head> < body> < button> 动起来</ button> < div> </ div> < script> $ ( function ( ) { $ ( "button" ) . click ( function ( ) { $ ( "div" ) . animate ( { left : 200 , top : 300 } , 1000 ) ; } ) ; } ) ; </ script>
</ body> </ html>