Blogger Information
Blog 22
fans 0
comment 1
visits 17871
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第七章 JavaScript动画
刘静的博客
Original
656 people have browsed it

第七章 JavaScript动画

简单运动

如何让物体运动起来

css代码如下:

  1. <style type="text/css">
  2. *{
  3. padding: 0;
  4. margin: 0;
  5. }
  6. button{
  7. width: 80px;
  8. height: 40px;
  9. }
  10. #box{
  11. width: 200px;
  12. height: 200px;
  13. background-color: pink;
  14. position: absolute;
  15. top: 50px;
  16. }
  17. </style>

html代码:

  1. <button id="btn">动画</button>
  2. <div id="box"></div>

javascript代码:

  1. <script type="text/javascript">
  2. // 简单的动画存在的问题: 1.处理边界 2.定时器的管理
  3. // 1.获取事件源
  4. var btn = document.getElementById('btn');
  5. var box = document.getElementById('box');
  6. var timer = null;
  7. // 2.给按钮绑定事件
  8. btn.onclick = function (){
  9. // 先关闭定时器 再开启定时器 防止定时器累加 导致物体加快
  10. clearInterval(timer);
  11. // 3.让物体运动起来(定时器) 如果没有边界,速度会越来越快
  12. timer = setInterval(function (){
  13. if(box.offsetLeft === 500){
  14. clearInterval(timer);
  15. }else{
  16. box.style.left = box.offsetLeft + 10 + 'px';
  17. }
  18. },30);
  19. }
  20. </script>

侧边栏动画效果

css代码如下:

  1. <style type="text/css">
  2. *{margin:0px;padding:0px;}
  3. #box{width:200px;height:200px;background-color:red;position:relative;margin-top:20px;left:-200px;}
  4. span{width: 40px;height: 60px;background-color: pink;color:#fff;position:absolute;right:-40px;top:50%;margin-top:-30px;text-align:center;line- height:60px;border-top-right-radius:10px;border-bottom-right-radius:10px;}
  5. </style>

html代码:

  1. <div id="box">
  2. <span>拉开</span>
  3. </div>

javascript代码:

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var box = document.getElementById('box');
  4. var sp = document.getElementsByTagName('span');
  5. box.onmouseover = function(){
  6. this.style.left = 0 + 'px';
  7. }
  8. box.onmouseout = function (){
  9. this.style.left = -box.offsetWidth + 'px';
  10. // this.style.left = -200 + 'px';
  11. }
  12. }
  13. </script>

[^注意:如果offsetWidth的对象有padding或者border值那么获取的obj.offsetWidth与定义的width不一致]:

侧边栏动画效果(匀速运动)

css代码如下:

  1. <style type="text/css">
  2. *{
  3. padding: 0;
  4. margin: 0;
  5. }
  6. #box{
  7. width: 200px;
  8. height: 200px;
  9. background-color: red;
  10. position: relative;
  11. left: -200px;
  12. }
  13. #box span{
  14. position: absolute;
  15. width: 40px;
  16. height: 60px;
  17. color: #fff;
  18. background-color: #000000;
  19. right: -40px;
  20. top: 50%;
  21. margin-top: -30px;
  22. line-height: 60px;
  23. text-align: center;
  24. }
  25. </style>

html代码如下:

  1. <div id="box">
  2. <span>拉开</span>
  3. </div>

javascript代码如下:

  1. <script type="text/javascript">
  2. window.onload = function (){
  3. var box = document.getElementById('box');
  4. var timer = null;
  5. box.onmouseover = function(){
  6. //先关闭定时器
  7. clearInterval(timer);
  8. timer = setInterval(function(){
  9. if(box.offsetLeft === 0){
  10. clearInterval(timer);
  11. return;
  12. }else{
  13. box.style.left = box.offsetLeft + 5 +'px';
  14. }
  15. },100)
  16. }
  17. box.onmouseout = function(){
  18. clearInterval(timer);
  19. timer = setInterval(function(){
  20. if(box.offsetLeft === -200){
  21. clearInterval(timer);
  22. return;
  23. }else{
  24. box.style.left = box.offsetLeft -5 +'px';
  25. }
  26. })
  27. }
  28. }
  29. </script>

侧边栏封装

javascript代码:

  1. <script type="text/javascript">
  2. window.onload = function (){
  3. var box = document.getElementById('box');
  4. var timer = null,speed = 0;
  5. box.onmouseover = function(){
  6. //先关闭定时器
  7. startAnimation(this,0);
  8. }
  9. box.onmouseout = function(){
  10. startAnimation(this,-200);
  11. }
  12. function startAnimation(obj,end){
  13. clearInterval(timer);
  14. speed = end > obj.offsetLeft ? 5 : -5;
  15. timer = setInterval(function(){
  16. if(obj.offsetLeft === end){
  17. clearInterval(timer);
  18. return;
  19. }else{
  20. obj.style.left = obj.offsetLeft + speed +'px';
  21. }
  22. })
  23. }
  24. }
  25. </script>

缓动运动

javascript代码:

  1. <script type="text/javascript">
  2. //0 —— 200
  3. //缓慢运动公式:加速度 = (结束值 - 起始值)/时间(缓动系数) 加速度是由快到慢
  4. window.onload = function (){
  5. var box = document.getElementById('box');
  6. var timer = null;end = 0;end2 = -200;
  7. box.onmouseover = function(){
  8. clearInterval(timer);
  9. timer = setInterval(function(){
  10. speed = Math.ceil((end - box.offsetLeft)/20);//0.45 数学Math.ceil()向上取整
  11. //处理边界问题
  12. if(box.offsetLeft === end){
  13. clearInterval(timer);
  14. return;
  15. }else{
  16. // console.log(box.offsetLeft,speed);//-9 0.45
  17. box.style.left = box.offsetLeft + speed +'px';//left: -8.55px;
  18. }
  19. },30);
  20. }
  21. box.onmouseout = function(){
  22. clearInterval(timer);
  23. timer = setInterval(function(){
  24. speed = Math.floor((end2 - box.offsetLeft)/20);//0.45 数学Math.ceil()向上取整
  25. //处理边界问题
  26. if(box.offsetLeft === end2){
  27. clearInterval(timer);
  28. return;
  29. }else{
  30. console.log(box.offsetLeft,speed);//-199 -1
  31. box.style.left = box.offsetLeft + speed +'px';//left: -8.55px;
  32. }
  33. },30);
  34. }
  35. }
  36. </script>

缓动运动封装

javascript代码:

  1. <script type="text/javascript">
  2. //缓慢运动公式:加速度 = (结束值 - 起始值)/时间(缓动系数) 加速度是由快到慢
  3. window.onload = function (){
  4. var box = document.getElementById('box');
  5. var timer = null;
  6. box.onmouseover = function(){
  7. slowAnimation(this,0);
  8. }
  9. box.onmouseout = function(){
  10. slowAnimation(this,-200);
  11. }
  12. function slowAnimation(obj,end){
  13. clearInterval(timer);
  14. timer = setInterval(function(){
  15. speed = (end - obj.offsetLeft) / 20;
  16. //如果速度大于0,证明物体往右走,速度小于0,证明物体往左走
  17. speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
  18. //处理边界问题
  19. if(obj.offsetLeft === end){
  20. clearInterval();
  21. return;
  22. }else{
  23. //console.log(obj.offsetLeft,speed)
  24. obj.style.left = obj.offsetLeft + speed + 'px';
  25. }
  26. },30)
  27. }
  28. }
  29. </script>

透明度动画

css代码:

  1. <style type="text/css">
  2. #box{
  3. width:200px;
  4. height:200px;
  5. background-color:pink;
  6. opacity:0.3;//全浏览器支持
  7. filter:alpha(opacity:30);
  8. //支持ie8以下浏览器 filter:alpha(opacity:30)
  9. }
  10. </style>

javascript代码:

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. //1.获取事件源
  4. var box = document.getElementById('box');
  5. //2.监听事件
  6. box.onmouseover = function(){
  7. opacityAnimation(this,100);
  8. }
  9. box.onmouseout = function(){
  10. opacityAnimation(this,30);
  11. }
  12. var timer = null;alpha = 30;speed = 0;
  13. function opacityAnimation(obj,endAlpha){
  14. clearInterval(timer);
  15. timer = setInterval(function(){
  16. //求透明度变化的速度
  17. speed = endAlpha > alpha ? 1 :-1;
  18. //边界处理
  19. if(alpha === endAlpha){
  20. clearInterval(timer);
  21. return;
  22. }
  23. alpha += speed;
  24. obj.style.opacity = alpha/100;
  25. obj.style.filter = `alpha(opacity:${alpha})`;
  26. },30)
  27. }
  28. }
  29. </script>

多物体缓动运动

css代码:

  1. <style type="text/css">
  2. *{
  3. padding:0px;
  4. margin:0px;
  5. }
  6. div{
  7. width:300px;
  8. height:150px;
  9. background-color:#3081BF;
  10. margin:20px 0px;
  11. }
  12. </style>

html代码:

  1. <div></div>
  2. <div></div>
  3. <div></div>

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var allBoxs = document.getElementsByTagName('div');
  4. for(var i = 0;i<allBoxs.length;i++){
  5. allBoxs[i].onmouseover = function(){
  6. startAnimation(this,600);
  7. }
  8. allBoxs[i].onmouseout = function(){
  9. startAnimation(this,300);
  10. }
  11. }
  12. var timer = null,speed = 0;
  13. function startAnimation(obj,endTarget){
  14. //针对于多物体运动,定时器的返回值要绑定在当前的对象上
  15. clearInterval(obj.timer);
  16. obj.timer = setInterval(function(){
  17. //1.求速度
  18. speed = (endTarget - obj.offsetWidth) / 10;
  19. speed = endTarget > obj.offsetWidth ? Math.ceil(speed) : Math.floor(speed);
  20. //2.设置临界值
  21. if(endTarget === obj.offsetWidth){
  22. clearInterval(obj.timer);
  23. return;
  24. }
  25. //3.运动
  26. obj.style.width = obj.offsetWidth + speed +'px';
  27. },30)
  28. }
  29. }
  30. </script>

[^针对于多物体运动,定时器的返回值要绑定在当前的对象上]:

正确获取元素样式属性

css代码

  1. <style type="text/css">
  2. #box{
  3. width:200px;
  4. height:200px;
  5. background-color:red;
  6. border:1px solid #000;
  7. }
  8. </style>

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var box = document.getElementById('box');
  4. move(box);
  5. function move(obj){
  6. setInterval(function(){
  7. // alert(obj.offsetWidth); obj.style.width为获取行内宽度
  8. obj.style.width = parseInt(getStyle(obj,'width')) - 1 + 'px';
  9. // console.log(getStyle(obj,'width'));//string类型
  10. },30);
  11. }
  12. //获取元素属性的函数
  13. // @param {object} obj当前元素对象
  14. // @param {object} attr 当前元素对象的属性
  15. function getStyle(obj,attr){
  16. if(obj.currentStyle){
  17. //判断是否在ie浏览器上 ie11
  18. return obj.currentStyle[attr];
  19. }else{
  20. //兼容主流浏览器 ie11也不支持
  21. return getComputedStyle(obj,null)[attr];
  22. }
  23. }
  24. }
  25. </script>

[^obj.currentStyle[attr]:获取ie浏览器上的的样式属性,包括ie11]:

多物体缓动运动完整版

css代码

  1. <style type="text/css">
  2. *{
  3. padding:0px;
  4. margin:0px;
  5. }
  6. div{
  7. width:300px;
  8. height:150px;
  9. background-color:red;
  10. margin:20px 0px;
  11. border:4px solid #000;
  12. }
  13. </style>

html代码

  1. <div></div>
  2. <div></div>
  3. <div></div>

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var boxs = document.getElementsByTagName('div');
  4. for(var i = 0;i<boxs.length;i++){
  5. boxs[i].onmouseover = function(){
  6. startAnimation(this,600);
  7. }
  8. boxs[i].onmouseout = function(){
  9. startAnimation(this,300);
  10. }
  11. }
  12. var speed = 0,timer = null;
  13. function startAnimation(obj,endTarget){
  14. clearInterval(obj.timer);
  15. obj.timer = setInterval(function(){
  16. //0.获取样式属性
  17. var cur = parseInt(getStyle(obj,'width'));//string
  18. // 1.求速度
  19. speed = (endTarget - cur) / 20;
  20. speed = (endTarget > cur) ? Math.ceil(speed) : Math.floor(speed);
  21. //2.临界处理
  22. if(endTarget === cur){
  23. clearInterval(obj.timer);
  24. return;
  25. }
  26. //3.运动起来
  27. obj.style.width = cur + speed + 'px';
  28. },30)
  29. }
  30. //获取元素属性的函数
  31. // @param {object} obj当前元素对象
  32. // @param {object} attr 当前元素对象的属性
  33. function getStyle(obj,attr){
  34. if(obj.currentStyle){
  35. //判断是否在ie浏览器上 ie11
  36. return obj.currentStyle[attr];
  37. }else{
  38. //兼容主流浏览器 ie11也不支持
  39. return getComputedStyle(obj,null)[attr];
  40. }
  41. }
  42. }
  43. </script>

多值运动

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var boxs = document.getElementsByTagName('div');
  4. // for(var i = 0;i<boxs.length;i++){
  5. // boxs[i].onmouseover = function(){
  6. // startAnimation(this,600);
  7. // }
  8. // boxs[i].onmouseout = function(){
  9. // startAnimation(this,300);
  10. // }
  11. // }
  12. boxs[0].onmouseover = function(){
  13. startAnimation(this,'width',600);
  14. }
  15. boxs[0].onmouseout = function(){
  16. startAnimation(this,'width',300);
  17. }
  18. boxs[1].onmouseover = function(){
  19. startAnimation(this,'height',300);
  20. }
  21. boxs[1].onmouseout = function(){
  22. startAnimation(this,'height',150);
  23. }
  24. boxs[2].onmouseover = function(){
  25. startAnimation(this,'width',600);
  26. }
  27. boxs[2].onmouseout = function(){
  28. startAnimation(this,'width',300);
  29. }
  30. var speed = 0,timer = null;
  31. function startAnimation(obj,attr,endTarget){
  32. clearInterval(obj.timer);
  33. obj.timer = setInterval(function(){
  34. //0.获取样式属性
  35. var cur = parseInt(getStyle(obj,attr));//string
  36. // 1.求速度
  37. speed = (endTarget - cur) / 20;
  38. speed = (endTarget > cur) ? Math.ceil(speed) : Math.floor(speed);
  39. //2.临界处理
  40. if(endTarget === cur){
  41. clearInterval(obj.timer);
  42. return;
  43. }
  44. //3.运动起来
  45. //obj.style.width = cur + speed + 'px';
  46. obj.style[attr] = cur + speed + 'px';
  47. },30)
  48. }
  49. //获取元素属性的函数
  50. // @param {object} obj当前元素对象
  51. // @param {object} attr 当前元素对象的属性
  52. function getStyle(obj,attr){
  53. if(obj.currentStyle){
  54. //判断是否在ie浏览器上 ie11
  55. return obj.currentStyle[attr];
  56. }else{
  57. //兼容主流浏览器 ie11也不支持
  58. return getComputedStyle(obj,null)[attr];
  59. }
  60. }
  61. }
  62. </script>

多值运动-处理透明度

css代码如下

  1. <style type="text/css">
  2. *{
  3. padding:0px;
  4. margin:0px;
  5. }
  6. div{
  7. width:300px;
  8. height:150px;
  9. background-color:pink;
  10. margin:20px 0px;
  11. border:4px solid #000;
  12. opacity:0.3;
  13. filter:alpha(opacity:30);
  14. }
  15. </style>

javascript代码如下

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var boxs = document.getElementsByTagName('div');
  4. boxs[0].onmouseover = function(){
  5. startAnimation(this,'opacity',100);
  6. }
  7. boxs[0].onmouseout = function(){
  8. startAnimation(this,'opacity',30);
  9. }
  10. boxs[1].onmouseover = function(){
  11. startAnimation(this,'height',300);
  12. }
  13. boxs[1].onmouseout = function(){
  14. startAnimation(this,'height',150);
  15. }
  16. boxs[2].onmouseover = function(){
  17. startAnimation(this,'width',600);
  18. }
  19. boxs[2].onmouseout = function(){
  20. startAnimation(this,'width',300);
  21. }
  22. var speed = 0,timer = null;
  23. // alert(Math.round(0.07*100));//四舍五入一下
  24. function startAnimation(obj,attr,endTarget){
  25. clearInterval(obj.timer);
  26. obj.timer = setInterval(function(){
  27. var cur = 0;
  28. //透明度变化处理
  29. //0.获取样式属性
  30. if(attr === 'opacity'){
  31. cur = Math.round(parseFloat(getStyle(obj,attr))*100);//正常的四色五入
  32. }else{
  33. cur = parseInt(getStyle(obj,attr));
  34. }
  35. // console.log(typeof getStyle(obj,attr));//string
  36. // 1.求速度
  37. speed = (endTarget - cur) / 20;
  38. speed = (endTarget > cur) ? Math.ceil(speed) : Math.floor(speed);
  39. //2.临界处理
  40. if(endTarget === cur){
  41. clearInterval(obj.timer);
  42. return;
  43. }
  44. //3.运动起来
  45. //obj.style.width = cur + speed + 'px';
  46. if(attr === 'opacity'){
  47. obj.style[attr] = `alpha(opacity:${cur+speed})`;
  48. obj.style[attr] = (cur + speed)/100;
  49. }else{
  50. obj.style[attr] = cur + speed + 'px';
  51. }
  52. },30)
  53. }
  54. //获取元素属性的函数
  55. // @param {object} obj当前元素对象
  56. // @param {object} attr 当前元素对象的属性
  57. function getStyle(obj,attr){
  58. if(obj.currentStyle){
  59. //判断是否在ie浏览器上 ie11
  60. return obj.currentStyle[attr];
  61. }else{
  62. //兼容主流浏览器 ie11也不支持
  63. return getComputedStyle(obj,null)[attr];
  64. }
  65. }
  66. }
  67. </script>

链式运动

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var box = document.getElementById('box');
  4. box.onmouseover = function(){
  5. startAnimation(box,'width',500,function(){
  6. startAnimation(box,'height',400);
  7. });
  8. }
  9. box.onmouseout = function(){
  10. startAnimation(this,'width',300,function(){
  11. startAnimation(box,'height',150);
  12. });
  13. }
  14. }
  15. </script>

myAnimation3.js代码

  1. var speed = 0,timer = null;
  2. // 动画函数
  3. // @param {object} obj 当前的对象
  4. // @param {object} attr 当前元素对象的属性
  5. // @param {object} endTarget 末尾位置
  6. // alert(Math.round(0.07*100));//四舍五入一下
  7. function startAnimation(obj,attr,endTarget,fn){
  8. //针对于多物体的运动,定时器的返回值要绑定到当前的对象中
  9. clearInterval(obj.timer);
  10. obj.timer = setInterval(function(){
  11. var cur = 0;
  12. //透明度变化处理
  13. //0.获取样式属性
  14. if(attr === 'opacity'){
  15. cur = Math.round(parseFloat(getStyle(obj,attr))*100);
  16. }else{
  17. cur = parseInt(getStyle(obj,attr));
  18. }
  19. // 1.求速度
  20. speed = (endTarget - cur) / 20;
  21. speed = (endTarget > cur) ? Math.ceil(speed) : Math.floor(speed);
  22. //2.临界处理
  23. if(endTarget === cur){
  24. clearInterval(obj.timer);
  25. if(fn){
  26. fn();
  27. }
  28. return;
  29. }
  30. //3.运动起来
  31. //obj.style.width = cur + speed + 'px';
  32. if(attr === 'opacity'){
  33. obj.style[attr] = `alpha(opacity:${cur+speed})`;
  34. obj.style[attr] = (cur + speed)/100;
  35. }else{
  36. obj.style[attr] = cur + speed + 'px';
  37. }
  38. },30)
  39. }
  40. //获取元素属性的函数
  41. // @param {object} obj当前元素对象
  42. // @param {object} attr 当前元素对象的属性
  43. function getStyle(obj,attr){
  44. if(obj.currentStyle){
  45. //判断是否在ie浏览器上 ie11
  46. return obj.currentStyle[attr];
  47. }else{
  48. //兼容主流浏览器 ie11也不支持
  49. return getComputedStyle(obj,null)[attr];
  50. }
  51. }

同时运动

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var box = document.getElementById('box');
  4. // json文件里面没有函数
  5. box.onmouseover = function(){
  6. startAnimation(box,{"width":500,"opacity":30});
  7. }
  8. }
  9. </script>

myAnimation4.js代码

  1. var speed = 0,timer = null;
  2. // 动画函数
  3. // @param {object} obj 当前的对象
  4. // @param {object} attr 当前元素对象的属性
  5. // @param {object} endTarget 末尾位置
  6. // alert(Math.round(0.07*100));//四舍五入一下
  7. function startAnimation(obj,json,fn){
  8. //针对于多物体的运动,定时器的返回值要绑定到当前的对象中
  9. clearInterval(obj.timer);
  10. obj.timer = setInterval(function(){
  11. var cur = 0;
  12. var flag = true;//标杆 如果为true,证明所有属性都达到终点值
  13. for(var attr in json){
  14. switch(attr){
  15. case 'opacity':
  16. cur = Math.round(parseFloat(getStyle(obj,attr))*100);
  17. break;
  18. case 'scrollTop':
  19. cur = obj[attr];//cur = obj.scrollTop
  20. break;
  21. default:
  22. cur = parseInt(getStyle(obj,attr));
  23. break;
  24. }
  25. // 1.求速度
  26. speed = (json[attr] - cur) / 20;
  27. speed = (json[attr] > cur) ? Math.ceil(speed) : Math.floor(speed);
  28. //2.临界处理
  29. if(json[attr] !== cur){
  30. flag = false;
  31. }
  32. //3.运动起来
  33. switch(attr){
  34. case 'opacity':
  35. obj.style[attr] = `alpha(opacity:${cur+speed})`;
  36. obj.style[attr] = (cur + speed)/100;
  37. break;
  38. case 'scrollTop':
  39. obj.scrollTop = cur + speed;
  40. break;
  41. default:
  42. obj.style[attr] = cur + speed + 'px';
  43. break;
  44. }
  45. if(flag){
  46. clearInterval(obj.timer);
  47. if(fn){
  48. fn();
  49. }
  50. return;
  51. }
  52. }
  53. },30)
  54. }
  55. //获取元素属性的函数
  56. // @param {object} obj当前元素对象
  57. // @param {object} attr 当前元素对象的属性
  58. function getStyle(obj,attr){
  59. if(obj.currentStyle){
  60. //判断是否在ie浏览器上 ie11
  61. return obj.currentStyle[attr];
  62. }else{
  63. //兼容主流浏览器 ie11也不支持
  64. return getComputedStyle(obj,null)[attr];
  65. }
  66. }

完美动画框架

javascript代码

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. var box = document.getElementById('box');
  4. // json文件里面没有函数
  5. box.onmouseover = function(){
  6. startAnimation(box,{"width":500,"opacity":30});
  7. }
  8. box.onmouseout = function(){
  9. startAnimation(box,{"width":200,"opacity":100});
  10. }
  11. }
  12. </script>

联动效果

css代码(380*380)

  1. <style type="text/css">
  2. *{padding:0px;margin:0px;}
  3. #ad{position:fixed;float:left;right:0px;bottom:0px;}
  4. #close{position:absolute;right:0px;top:0px;width:25px;height:25px;z-index:10;}
  5. </style>

html代码

  1. <div id="ad">
  2. <img src="images/ad.png" width="300">
  3. <span id="close"></span>
  4. </div>

javascript代码(myAnimation4.js)

  1. <script type="text/javascript">
  2. var ad = document.getElementById('ad');
  3. var close = document.getElementById('close');
  4. close.onclick = function(){
  5. startAnimation(ad,{"height":260},function(){
  6. startAnimation(ad,{"width":0},function(){
  7. ad.style.display = 'none';
  8. })
  9. })
  10. }
  11. </script>

侧边栏横幅效果

css代码

  1. <style type="text/css">
  2. *{padding:0px;margin:0px;}
  3. #aside{position:absolute;top:200px;left:0px;width:300px;}
  4. #aside img{width:100%;}
  5. </style>

html代码

  1. <body style="height:5000px;">
  2. <div id="aside">
  3. <img src="images/aside.png">
  4. </div>
  5. </body>

javascript代码()

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. //1.获取标签
  4. var aside = document.getElementById('aside');
  5. var aside_top = aside.offsetTop;
  6. window.onscroll = function(){
  7. //3.获取页面滚动的高度
  8. var docScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  9. startAnimation(aside,{"top":docScrollTop + aside_top});
  10. }
  11. }
  12. </script>

[^document.documentElement.scrollTop:chrome浏览器,火狐浏览器可以,sarfi不行]:
[^document.body.scrollTop:chrome浏览器不行,火狐浏览器可以,sarfi可以]:

滚动监听特效结构和样式实现

css代码

  1. <style type="text/css">
  2. *{margin:0px;padding:0px;}
  3. ul{list-style:none;}
  4. a{text-decoration:none;}
  5. html,body{width:100%;height:100%;}
  6. .container{width:1190px;height:100%;margin:0px auto;}
  7. .container div{width:100%;height:100%;text-align:center;color:#fff;font-size:30px;}
  8. .aside{position:fixed;width:40px;right:20px;top:300px;font-size:16px;font-weight:700;text-align:center;}
  9. .aside li{height:50px;border-bottom:1px solid #ddd;}
  10. .aside li a{color:peru;}
  11. .aside li.current{background-color:coral;}
  12. .aside li.current a {color: #fff;}
  13. </style>

html代码:

  1. <body style="height:5000px;">
  2. <div class="container" id="box">
  3. <div class="current">爱逛好货</div>
  4. <div>好点主播</div>
  5. <div>品质特色</div>
  6. <div>猜你喜欢</div>
  7. </div>
  8. <ul class="aside" id="aside">
  9. <li class="current">
  10. <a href="javascript:void(0);">爱逛好货</a>
  11. </li>
  12. <li>
  13. <a href="javascript:void(0);">好点主播</a>
  14. </li>
  15. <li>
  16. <a href="javascript:void(0);">品质特色</a>
  17. </li>
  18. <li>
  19. <a href="javascript:void(0);">猜你喜欢</a>
  20. </li>
  21. </ul>
  22. </body>

javascript代码(myAnimation4.js)

  1. <script type="text/javascript">
  2. //1.获取事件源
  3. window.onload = function(){
  4. //1.获取标签
  5. var box = document.getElementById('box');
  6. var allBoxs = box.children;
  7. var aside = document.getElementById('aside');
  8. var lis = aside.children;
  9. var isClick = false;//默认没有滚动
  10. //2.上色
  11. var colors = ['red','pink','purple','blue'];
  12. for(var i=0;i<allBoxs.length;i++){
  13. allBoxs[i].style.backgroundColor = colors[i];
  14. }
  15. //3.监听侧边栏按钮的点击
  16. for(var j=0;j<lis.length;j++){
  17. lis[j].index = j;
  18. lis[j].onclick = function(){
  19. isClick = true;
  20. for(var k = 0;k < lis.length;k++){
  21. lis[k].className = '';
  22. }
  23. //设置当前的类
  24. this.className = 'current';
  25. //页面动画起来
  26. startAnimation(document.documentElement,{
  27. "scrollTop" : this.index * (document.body.clientHeight)
  28. },function(){
  29. isClick = false;
  30. })
  31. // document.documentElement.scrollTop = this.index*(document.body.clientHeight);
  32. }
  33. }
  34. //4.监听滚动
  35. window.onscroll = function(){
  36. if(!isClick){
  37. //4.1获取页面滚动的高度
  38. var docScrollTop = document.documentElement.scrollTop+20 || document.body.scrollTop+20;
  39. for(var i=0;i<lis.length;i++){
  40. if(docScrollTop > allBoxs[i].offsetTop){
  41. for(var j =0;j<allBoxs.length;j++){
  42. lis[j].className = '';
  43. }
  44. lis[i].className = 'current';
  45. }
  46. }
  47. }
  48. }
  49. }
  50. </script>

轮播图特效

css代码

  1. *{
  2. padding:0px;
  3. margin:0px;
  4. }
  5. .slider{
  6. width:400px;
  7. height:500px;
  8. margin:100px auto;
  9. position:relative;
  10. overflow: hidden;
  11. }
  12. .slider .slider_scroll{
  13. position:relative;
  14. width:400px;
  15. height:500px;
  16. }
  17. .slider_main{
  18. position:relative;
  19. width:400px;
  20. height:500px;
  21. }
  22. .slider_main .item{
  23. width:400px;
  24. height:500px;
  25. position:absolute;
  26. }
  27. .slider_main .item img{
  28. width:400px;
  29. height:500px;
  30. }
  31. .slider_index{
  32. width:400px;
  33. height:40px;
  34. text-align:center;
  35. color:#fff;
  36. font-weight:700;
  37. z-index:20;
  38. position:absolute;
  39. bottom:0;
  40. background-color:rgb(0,0,0,0.5);
  41. cursor:pointer;
  42. }
  43. .slider_index .slider_index_icon{
  44. display: inline-block;
  45. line-height: 40px;
  46. margin: 0 10px;
  47. }
  48. .slider_index .slider_index_icon.current{
  49. color: red;
  50. }
  51. .slider_scroll span{
  52. position: absolute;
  53. width:30px;
  54. height:68px;
  55. background:url(../images/icon-slides.png) no-repeat;
  56. top:50%;
  57. margin-top:-34px;
  58. cursor:pointer;
  59. z-index:30;
  60. }
  61. .slider_scroll span.next{
  62. right: 0;
  63. background-position: -46px 0;
  64. }
  65. .slider_scroll span.prev{
  66. left: 0;
  67. background-position: 0 0;
  68. }

html代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>p_20 轮播图特效</title>
  6. <link rel="stylesheet" href="css/index.css" type="text/css">
  7. </head>
  8. <body>
  9. <div id="slider" class="slider">
  10. <div class="slider_scroll" id="slider_scroll">
  11. <div class="slider_main" id="slider_main">
  12. <div class="item">
  13. <a href="#">
  14. <img src="images/img1.png">
  15. </a>
  16. </div>
  17. <div class="item">
  18. <a href="#">
  19. <img src="images/img2.png">
  20. </a>
  21. </div>
  22. <div class="item">
  23. <a href="#">
  24. <img src="images/img3.png">
  25. </a>
  26. </div>
  27. <div class="item">
  28. <a href="#">
  29. <img src="images/img4.png">
  30. </a>
  31. </div>
  32. </div>
  33. <span class="next" id="next"></span>
  34. <span class="prev" id="prev"></span>
  35. </div>
  36. <div class="slider_index" id="slider_index">
  37. <!-- 通过js脚本动态生成 -->
  38. </div>
  39. </div>
  40. <script src="../js/myAnimation4.js" type="text/javascript" charset="utf-8"></script>
  41. <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
  42. </body>
  43. </html>

index.js代码

  1. window.onload = function(){
  2. //1.获取标签
  3. var slider = document.getElementById('slider');
  4. var slider_main = document.getElementById('slider_main');
  5. var allBoxs = slider_main.children;
  6. var next = document.getElementById('next');
  7. var prev = document.getElementById('prev');
  8. var slider_index = document.getElementById('slider_index');
  9. var iNow = 0;//当前可视元素的索引
  10. var timer =null;
  11. //2.动态创建索引器
  12. for(var i=0;i < allBoxs.length;i++){
  13. var span = document.createElement('span');
  14. if(i === 0){
  15. span.className = 'slider_index_icon current';
  16. }else{
  17. span.className = 'slider_index_icon';
  18. }
  19. span.innerText = i + 1;
  20. slider_index.appendChild(span);
  21. }
  22. //3.让滚动的元素归位
  23. var scroll_w = parseInt(getStyle(slider,'width'));
  24. // console.log(scroll_w);
  25. for(var j=1;j < allBoxs.length;j++){
  26. allBoxs[j].style.left = scroll_w +'px';
  27. }
  28. //4.监听下一张按钮的事件
  29. next.onclick = function(){
  30. // 1.让当前可视元素动画左移
  31. // 2.让下一张图片快速的显示到右边
  32. // 3.让这个元素动画进入
  33. startAnimation(allBoxs[iNow],{
  34. "left":-scroll_w
  35. })
  36. //让iNow更新
  37. iNow ++;
  38. if(iNow >= allBoxs.length){
  39. iNow = 0;
  40. }
  41. allBoxs[iNow].style.left = scroll_w +'px';
  42. startAnimation(allBoxs[iNow],{
  43. "left":0
  44. })
  45. //改变索引
  46. changeIndex();
  47. }
  48. //5.监听上一张按钮的事件
  49. prev.onclick = function(){
  50. // 1.让当前元素快速右移
  51. // 2.让上一个元素快速到左边
  52. // 3.让这个元素动画进入
  53. startAnimation(allBoxs[iNow],{
  54. "left":scroll_w
  55. })
  56. iNow--;
  57. if(iNow < 0){
  58. iNow =allBoxs.length-1;
  59. }
  60. allBoxs[iNow].style.left = -scroll_w + 'px';
  61. startAnimation(allBoxs[iNow],{
  62. "left":0
  63. })
  64. //改变索引器
  65. changeIndex();
  66. }
  67. var slider_index_icons = slider_index.children;
  68. //6.遍历索引器,添加监听事件
  69. for(var i=0;i<slider_index_icons.length;i++){
  70. slider_index_icons[i].onmousedown = function(){
  71. //6.1获取当前点击的索引
  72. var index = parseInt(this.innerText) - 1;
  73. //点击的索引与当前的索引iNow并与之对比
  74. if(index > iNow){
  75. //下一个操作
  76. startAnimation(allBoxs[iNow],{
  77. 'left':-scroll_w
  78. })
  79. allBoxs[index].style.left = scroll_w + 'px';
  80. }else if(index < iNow){
  81. //上一个操作
  82. startAnimation(allBoxs[iNow],{
  83. 'left':scroll_w
  84. })
  85. allBoxs[index].style.left = -scroll_w + 'px';
  86. }
  87. iNow = index;
  88. startAnimation(allBoxs[iNow],{
  89. "left":0
  90. })
  91. changeIndex();
  92. }
  93. }
  94. //7自动轮播图
  95. timer = setInterval(autoPlay,1000);
  96. slider.onmouseover = function(){
  97. clearInterval(timer);
  98. }
  99. slider.onmouseout = function(){
  100. timer = setInterval(autoPlay,1000);
  101. }
  102. function autoPlay(){
  103. // 1.让当前可视元素动画左移
  104. // 2.让下一张图片快速的显示到右边
  105. // 3.让这个元素动画进入
  106. startAnimation(allBoxs[iNow],{
  107. "left":-scroll_w
  108. })
  109. //让iNow更新
  110. iNow ++;
  111. if(iNow >= allBoxs.length){
  112. iNow = 0;
  113. }
  114. allBoxs[iNow].style.left = scroll_w +'px';
  115. startAnimation(allBoxs[iNow],{
  116. "left":0
  117. })
  118. //改变索引
  119. changeIndex();
  120. }
  121. //控制当前的索引
  122. function changeIndex(){
  123. for(var i=0;i<slider_index_icons.length;i++){
  124. slider_index_icons[i].className = 'slider_index_icon';
  125. }
  126. slider_index_icons[iNow].className = 'slider_index_icon current';
  127. }
  128. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!