1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <title>其他事件</title>
    5. <meta charset="utf-8" />
    6. <style type="text/css">
    7. .msg{
    8. width:600px;
    9. margin:100px auto 0px auto;
    10. color:#ff0000;
    11. }
    12. #view_box{
    13. position:relative;
    14. width:600px;
    15. height:300px;
    16. margin:10px auto;
    17. background-color:#000000;
    18. background-image:url("res/lianxi/sheji/bg1.jpg");
    19. background-position:center;
    20. background-repeat:no-repeat;
    21. background-size:cover;
    22. }
    23. </style>
    24. </head>
    25. <body>
    26. <div class="msg">提示:利用鼠标滚轮切换图片</div>
    27. <div id="view_box">
    28. </div>
    29. <script type="text/javascript">
    30. var index = 1;//图片id
    31. //添加滚轮事件
    32. document.getElementById("view_box").addEventListener('DOMMouseScroll',mousewheel,false);//firefox
    33. document.getElementById("view_box").onmousewheel = mousewheel;//chrome
    34. function mousewheel(e){
    35. e.preventDefault();//通知浏览器不执行默认的动作
    36. if(e.wheelDelta){
    37. //IE/Opera/Chrome
    38. if(e.wheelDelta < 0){
    39. //下
    40. index++;
    41. }
    42. else{
    43. //上
    44. index--;
    45. }
    46. }
    47. else if(e.detail){
    48. //Firefox
    49. if(e.detail < 0){
    50. //上
    51. index--;
    52. }
    53. else{
    54. //下
    55. index++;
    56. }
    57. }
    58. setAnimate();
    59. }
    60. //切换图片
    61. function setAnimate(){
    62. if(index > 8){
    63. index = 1;
    64. }
    65. if(index < 1){
    66. index = 8;
    67. }
    68. //设置view_box背景图片
    69. document.getElementById("view_box").style.backgroundImage = "url('res/lianxi/sheji/bg"+index+".jpg')";
    70. }
    71. </script>
    72. </body>
    73. </html>

    2021-09-26_195229.jpg