1.点击自动切换图片

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #img{
  9. width: 400px;
  10. height: 270px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <img src="./images/one.jpg" alt="" id="img">
  16. <br>
  17. <button id="btn" >开始</button>
  18. <button id="btn1">停止</button>
  19. <script>
  20. window.onload=function(){
  21. //让图片自动切换 src属性改变
  22. //定义一个timer
  23. var timer;
  24. //获取图片
  25. var img=document.getElementById("img");
  26. //创建数组保存图片路径(src)
  27. var arr=["images/one.jpg","images/two.jpg","images/three.jpg","images/four.jpg","images/five.jpg"]
  28. //创建一个变量保存当前索引
  29. var index=0;
  30. //获取按钮
  31. var btn=document.getElementById("btn")
  32. btn.onclick=function(){
  33. //创建一个定时器
  34. timer=setInterval(function(){
  35. index++;
  36. index=index%arr.length;
  37. // if(index>=arr.length){
  38. // index=0
  39. // }
  40. img.src=arr[index]
  41. },1000)
  42. }
  43. //停止切换图片
  44. var btn1=document.getElementById("btn1")
  45. btn1.onclick=function(){
  46. //关闭定时器
  47. clearInterval(timer)
  48. }
  49. }
  50. </script>
  51. </body>
  52. </html>

2.滚动事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #box{
  9. width: 200px;
  10. height: 200px;
  11. position: absolute;
  12. background-color: cornflowerblue;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="box"></div>
  18. <script>
  19. window.onload=function(){
  20. var box=document.getElementById("box");
  21. //给box绑定一个滚轮事件判断滚动了没
  22. box.onmousewheel=function(event){
  23. event=event||window.event;
  24. //获取滚轮滚动的方向event.wheelDelta 只看正负 正向上 负向下
  25. // alert(event.wheelDelta);
  26. if(event.wheelDelta>0){
  27. //向上滚变短
  28. box.style.height=box.clientHeight-5+"px";
  29. }else{
  30. //向下滚变长
  31. box.style.height=box.clientHeight+5+"px";
  32. }
  33. }
  34. }
  35. </script>
  36. </body>
  37. </html>

3.拖拽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #box{
  9. width: 200px;
  10. height: 200px;
  11. background-color: coral;
  12. position: absolute;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="box"></div>
  18. <script>
  19. window.onload=function(){
  20. //获取box
  21. var box=document.getElementById("box");
  22. //为box绑定一个onmousedown鼠标按下事件
  23. box.onmousedown=function(){
  24. //为document绑定一个onmousemove鼠标移动事件
  25. document.onmousemove=function(event){
  26. event=event ||window.event;
  27. var left=event.clientX; //获取当前移动后的位置
  28. var top=event.clientY;
  29. //修改box位置
  30. box.style.left=left+"px";
  31. box.style.top=top+"px";
  32. }
  33. }
  34. //为box绑定onmuoseup鼠标松开事件
  35. box.onmouseup=function(){
  36. //鼠标松开后 box不移动 onmousemove事件不存在
  37. document.onmousemove=null;
  38. }
  39. }
  40. </script>
  41. </body>
  42. </html>