1. window.onload = function () {
    2. //获取点击按钮标签
    3. var btn = document.querySelector("button");
    4. //获取改变倍数的input标签
    5. var range = document.querySelector('input[type="range"]')
    6. //获取提示倍数信息的标签
    7. var index = document.querySelector(".magIndex")
    8. //按钮点击事件函数
    9. btn.onclick = function () {
    10. //改变放大倍数
    11. reset(range.value)
    12. //显示放大倍数
    13. index.innerHTML = range.value;
    14. }
    15. //获取显示的图片
    16. var img_small = this.document.getElementsByClassName("img_small")[0];
    17. //获取放大后的图片
    18. var img_big = this.document.getElementsByClassName("img_big")[0];
    19. //获取显示图片的盒子
    20. var viewing_small = this.document.getElementsByClassName("viewing_small")[0];
    21. //获取放大后的图片的盒子
    22. var viewing_big = this.document.getElementsByClassName("viewing_big")[0];z
    23. //获取鼠标移动到图片上的遮罩层
    24. var mag = this.document.getElementsByClassName("mag")[0];
    25. //获取显示的图片的宽度
    26. var img_smallx = img_small.clientWidth;
    27. //获取显示的图片的高度
    28. var img_smally = img_small.clientHeight;
    29. //设定一个初始的放大倍数
    30. var magIndex = 1;
    31. //初始放大页面
    32. reset(magIndex)
    33. //放大镜函数
    34. function reset(magIndex) {
    35. //设置放大后的图片宽度和高度
    36. img_big.style.width = img_smallx * magIndex + "px";
    37. img_big.style.height = img_smally * magIndex + "px";
    38. //设置遮罩层的宽度和高度
    39. mag.style.width = img_smallx / magIndex + "px";
    40. mag.style.height = img_smally / magIndex + "px";
    41. //设置显示的图片的盒子高度和宽度
    42. viewing_small.style.width = img_smallx + "px";
    43. viewing_small.style.height = img_smally + "px";
    44. //设置大盒子的宽度和高度
    45. viewing_big.style.width = img_smallx + "px";
    46. viewing_big.style.height = img_smally + "px";
    47. //当鼠标移入显示的图片的盒子时遮罩层和大图片盒子显示
    48. viewing_small.onmouseenter = function () {
    49. mag.style.display = "block";
    50. viewing_big.style.display = "block";
    51. }
    52. //鼠标移出时遮罩层和大图片盒子隐藏
    53. viewing_small.onmouseleave = function () {
    54. mag.style.display = "none";
    55. viewing_big.style.display = "none";
    56. }
    57. //鼠标移动事件
    58. viewing_small.onmousemove = function (event) {
    59. //鼠标相对于屏幕的位置-小盒子相对于屏幕的位置-遮罩层的一半宽度
    60. var x = event.clientX - viewing_small.getBoundingClientRect().left - mag.clientWidth / 2;
    61. var y = event.clientY - viewing_small.getBoundingClientRect().top - mag.clientHeight / 2;
    62. if (x < 0) {
    63. x = 0;
    64. }
    65. if (x > viewing_small.offsetWidth - mag.offsetWidth) {
    66. x = viewing_small.offsetWidth - mag.offsetWidth
    67. }
    68. if (y < 0) {
    69. y = 0;
    70. }
    71. if (y > viewing_small.offsetHeight - mag.offsetHeight) {
    72. y = viewing_small.offsetHeight - mag.offsetHeight
    73. }
    74. mag.style.left = x + "px";
    75. mag.style.top = y + "px";
    76. img_big.style.left = -x * magIndex + "px";
    77. img_big.style.top = -y * magIndex + "px";
    78. }
    79. viewing_big.onmouseenter = function () {
    80. viewing_big.style.display = "none";
    81. }
    82. }
    83. }