detail.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function show1() {
  2. var aTrue = document.getElementsByClassName(true);
  3. var aFalse = document.getElementsByClassName(false);
  4. var x = 1;
  5. if (aTrue[0].style.display != "none") {
  6. aTrue[0].style.display = "none";
  7. aFalse[0].style.display = "inline";
  8. } else {
  9. aTrue[0].style.display = "inline";
  10. aFalse[0].style.display = "none";
  11. }
  12. }
  13. window.addEventListener('load', function() {
  14. var preview_img = document.querySelector('.preview_img');
  15. var mask = document.querySelector('.mask');
  16. var big = document.querySelector('.big');
  17. // 1. 当我们鼠标经过 preview_img 就显示和隐藏 mask 遮挡层 和 big 大盒子
  18. preview_img.addEventListener('mouseover', function() {
  19. mask.style.display = 'block';
  20. big.style.display = 'block';
  21. })
  22. preview_img.addEventListener('mouseout', function() {
  23. mask.style.display = 'none';
  24. big.style.display = 'none';
  25. })
  26. // 2. 鼠标移动的时候,让黄色的盒子跟着鼠标来走
  27. preview_img.addEventListener('mousemove', function(e) {
  28. // (1). 先计算出鼠标在盒子内的坐标
  29. var x = e.pageX - this.offsetLeft;
  30. var y = e.pageY - this.offsetTop;
  31. // console.log(x, y);
  32. // (2) 减去盒子高度 300的一半 是 150 就是我们mask 的最终 left 和top值了
  33. // (3) 我们mask 移动的距离
  34. var maskX = x - mask.offsetWidth / 2;
  35. var maskY = y - mask.offsetHeight / 2;
  36. // (4) 如果x 坐标小于了0 就让他停在0 的位置
  37. // 遮挡层的最大移动距离
  38. var maskMax = preview_img.offsetWidth - mask.offsetWidth;
  39. if (maskX <= 0) {
  40. maskX = 0;
  41. } else if (maskX >= maskMax) {
  42. maskX = maskMax;
  43. }
  44. if (maskY <= 0) {
  45. maskY = 0;
  46. } else if (maskY >= maskMax) {
  47. maskY = maskMax;
  48. }
  49. mask.style.left = maskX + 'px';
  50. mask.style.top = maskY + 'px';
  51. // 3. 大图片的移动距离 = 遮挡层移动距离 * 大图片最大移动距离 / 遮挡层的最大移动距离
  52. // 大图
  53. var bigIMg = document.querySelector('.bigImg');
  54. // 大图片最大移动距离
  55. var bigMax = bigIMg.offsetWidth - big.offsetWidth;
  56. // 大图片的移动距离 X Y
  57. var bigX = maskX * bigMax / maskMax;
  58. var bigY = maskY * bigMax / maskMax;
  59. bigIMg.style.left = -bigX + 'px';
  60. bigIMg.style.top = -bigY + 'px';
  61. window.onload = function() {
  62. var oSc = document.getElementById('shoucang');
  63. var onOff = true;
  64. oSc.onclick = function() {
  65. if (onOff) {
  66. oSc.innerHTML = '<i class="yishoucang"></i>' + '已收藏';
  67. oSc.style.backgroundColor = '#87aab5';
  68. onOff = false;
  69. } else {
  70. oSc.innerHTML = '收藏文章';
  71. oSc.style.backgroundColor = '#87aab5';
  72. onOff = true;
  73. }
  74. };
  75. oSc.onmouseover = function() {
  76. if (onOff) {
  77. oSc.style.backgroundColor = '#03a6d7';
  78. } else {
  79. oSc.innerHTML = '取消收藏';
  80. oSc.style.backgroundColor = '#527884';
  81. }
  82. };
  83. oSc.onmouseout = function() {
  84. if (onOff) {
  85. oSc.style.backgroundColor = '#00b7ee';
  86. } else {
  87. oSc.innerHTML = '<i class="yishoucang"></i>' + '已收藏';
  88. oSc.style.backgroundColor = '#87aab5';
  89. }
  90. };
  91. }
  92. })
  93. })