动画效果

改变元素的显示和隐藏

DOM : 运动插件的封装
move(‘width’,’500’,1000) 动画效果

  • show() 显示 通过设置display属性改变
  • hide() 隐藏 通过设置display属性改变
  • toggle() 切换

同时改变元素的宽高和透明度来隐藏和显示元素

  1. 参数1:时间
  2. 参数2 :函数
  3. 参数3: 运动速度
  • fadeIn() 淡入 让元素显示
  • fadeOut() 淡出 让元素隐藏

通过元素的透明度让元素显示和隐藏,同时也会设置元素的display属性

  • fadeTo(3000,0.5,fn) 设置元素变化透明度的目标值 0-1
  • fadeToggle() 切换 将元素的透明度从0切换到1 / 从1-0
  • slideDown() 卷入
  • slideUp() 卷出
  • slideToggle() 切换
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="./jquery-1.12.4.js"></script>
  9. <style>
  10. * {
  11. margin: 0px;
  12. padding: 0px;
  13. }
  14. div {
  15. width: 200px;
  16. height: 150px;
  17. overflow: hidden;
  18. }
  19. img {
  20. width: 200px;
  21. height: 150px;
  22. display: block;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div>
  28. <!-- <img src="./IMG_20210819_182021.jpg" alt=""> -->
  29. <img src="./IMG_20210802_192614.jpg" alt="">
  30. <img src="./IMG_20210804_191912_1.jpg" alt="">
  31. <img src="./IMG_20210804_191919.jpg" alt="">
  32. <img src="./IMG_20210819_182021.jpg" alt="">
  33. </div>
  34. <button>点我开始滑动</button>
  35. <script>
  36. // 定位:top
  37. // slideUp down 改变img的位置
  38. $('button').click(function() {
  39. setInterval(function() {
  40. $('div img:first').slideUp(function() {
  41. $('div img:last').after($(this));
  42. $('div img:last').css("display", "block")
  43. })
  44. }, 2000)
  45. })
  46. // $('button').click(function() {
  47. // $('div').s();
  48. // console.log($('div'));
  49. // })
  50. </script>
  51. </body>
  52. </html>

通过改变元素高度来让元素显示和隐藏的

animate()

集合了以上的所有效果

通过改变元素的属性来达到动画效果

参数1: 属性对象
参数2:时间
参数3: 速度
参数4:回调函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="js/jquery.js"></script>
  9. <style>
  10. div{
  11. width: 100px;
  12. height: 100px;
  13. background: hotpink;
  14. position: absolute;
  15. top: 0;
  16. left: 0;
  17. }
  18. button{
  19. position: absolute;
  20. top:100px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div></div>
  26. <button>点我</button>
  27. <script>
  28. // animate()
  29. // div 宽度 定位:0 0 -》 100 100
  30. // $('button').click(function(){
  31. // $('div').animate({
  32. // 'width':500,
  33. // 'top':100,
  34. // 'left':100
  35. // },1000,'linear',function(){
  36. // console.log('over');
  37. // })
  38. // })
  39. // 在当前属性的值上 添加100px 直接在属性值上去进行运算 += 100
  40. // $('button').click(function(){
  41. // $('div').animate({
  42. // 'width':'+=100',
  43. // 'top':'+=100',
  44. // 'left':'+=100'
  45. // },1000,'linear',function(){
  46. // console.log('over');
  47. // })
  48. // })
  49. // 回调函数:函数中,当函数的功能执行完成后,然后执行传递的参数(函数)
  50. // 递归: 在函数内容 只有执行完函数中的代码 才能结束函数
  51. // 事情1 干完 -》 执行事件2 -》 处理事件3
  52. // 处理事件1中 得到的结果是错误的 promise 回调地狱
  53. $('button').click(function(){
  54. $('div').animate({
  55. 'width':500,
  56. 'top':100,
  57. 'left':100
  58. },1000,'linear',function(){
  59. $('button').animate({
  60. 'width':100,
  61. 'height':100,
  62. },2000,'swing',function(){
  63. })
  64. })
  65. })
  66. // var one = new Promise();
  67. // one.then(function(){
  68. // // 成功后的要执行的事件
  69. // },function(){
  70. // //
  71. // })
  72. // 面试重点 : promise
  73. </script>
  74. </body>
  75. </html>
  • stop() 停止动画
  • finish() 完成动画
  • delay() 延迟动画
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="js/jquery.js"></script>
  9. <style>
  10. div{
  11. width: 100px;
  12. height: 100px;
  13. background: hotpink;
  14. position: absolute;
  15. top: 50px;
  16. left: 0;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div></div>
  22. <button>stop</button>
  23. <button>finish</button>
  24. <button>delay</button>
  25. <script>
  26. $('div').click(function(){
  27. $(this).animate({
  28. 'opacity':0.2,
  29. // 'background':'red',
  30. 'top':200,
  31. },2000).delay(2000).animate({
  32. 'width':500,
  33. 'height':500
  34. })
  35. })
  36. $('button:eq(0)').click(function(){
  37. // stop 停止当前正在进行的动画 直接完成最后一个动画
  38. // 传参true 停止当前所有的动画
  39. // true true 直接完成当前动画 然后停止后面所有的动画
  40. $('div').stop(true,true);
  41. })
  42. $('button:eq(1)').click(function(){
  43. // 直接到达所有动画的目标 但是没有执行的过程
  44. $('div').finish();
  45. })
  46. // 将需要延迟的动画前调用delay
  47. $('button:eq(2)').click(function(){
  48. // $('div').;
  49. })
  50. </script>
  51. </body>
  52. </html>

工具类

静态方法

  • $.each(data,function(){}) 遍历方法 forEach()
    $(data).each(function(){})
  • index() 获取元素在这一类元素中的下标
  • type() 类似 typeof
    typeof 数据是什么类型的 array date regExp -》 Object
    type 是什么数据
  • $.isArray()
  • $.isFunction()
  • $.isWindow()
    jquery插件内置的内部使用的方法
  • trim() 消除空格
  • proxy() 改变this的指向 类似bind
  1. function show(){}
  2. var newShow = show.bind(obj,1,2);
  3. var newShow = $.proxy(show,obj);
  4. // 单例模式:实现功能 this的执行可能发生改变
  • $.noconflict() 防止冲突 变量冲突 $
  1. 引入jq $
  2. 引入wq $
  3. 引入sq $
  4. $
  5. var $c = $.noconflict();
  6. $c('div)
  • $.parseJson() 将字符串转换为对象
    json数据 字符串 -》 对象
    JSON.parse(str) 原生
    json.stringify() 对象转换成json格式的字符串
  1. "{"name":123}" 从前端将json数据传递到后端
  • makeArray() 制作数组
  1. form()
  • $.extend()
  • jq.extend()