电子商务网站放大效果的三种常用的实现方式。

举报
孙叫兽 发表于 2021/03/29 00:15:02 2021/03/29
【摘要】 预览效果(这里截取静态,有兴趣的可以运行下面的代码): 实现方式1. <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>电子商务网站放大镜效果1</title> <style type="text/css"> html, body, div { margin: 0; padding: 0; } #...

预览效果(这里截取静态,有兴趣的可以运行下面的代码):

实现方式1.


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>电子商务网站放大镜效果1</title>
  6. <style type="text/css">
  7. html, body, div { margin: 0; padding: 0; }
  8. #tab { position: relative; width: 300px; height: 300px;margin: 50px ; background: #CDE074; }
  9. #mark { position: absolute; top: 0; left: 0; width: 100px; height: 100px; background: #B00000; opacity: 0.5; filter: alpha(opacity=50); cursor: move; }
  10. #container{ position:absolute; height:360px; width:360px; background:pink; overflow:hidden;left: 350px;top: 0px}
  11. #bigImg{ position:absolute; display:block; border:none;}
  12. </style>
  13. </head>
  14. <body>
  15. <div id="tab">
  16. <img src="https://img-blog.csdnimg.cn/2020073120293658.jpg" width="300" height="300" id="img1">
  17. </div>
  18. <div id="jiance"></div>
  19. </body>
  20. </html>
  21. <script type="text/javascript">
  22. var oTab = document.getElementById("tab");
  23. //获得oDiv本身的高和宽;获得oDiv距离浏览器左上角的位移;
  24. var tabT = oTab.offsetTop;
  25. var tabL = oTab.offsetLeft;
  26. var tabW = oTab.offsetWidth;
  27. var tabH = oTab.offsetHeight;
  28. //onmouseenter限制冒泡机制
  29. oTab.onmouseenter = function (e) {
  30. //创建大图容器
  31. var container = document.createElement("div");
  32. container.id = "container";
  33. this.appendChild(container);
  34. var bigImg=document.createElement("img");
  35. bigImg.src="https://img-blog.csdnimg.cn/2020073120293658.jpg";
  36. bigImg.id="bigImg";
  37. container.appendChild(bigImg);
  38. //当鼠标移近来的时候,动态创建一个DIV元素,id是mark
  39. var mark = document.createElement("div");
  40. mark.id = "mark";
  41. this.appendChild(mark);//像oTab里添加mark;
  42. //执行setMark
  43. setMark(mark, container, e);
  44. };
  45. oTab.onmousemove = function (e) {
  46. var mark = document.getElementById("mark");
  47. if (mark) {
  48. //先判断mark是否存在,如果存在,执行setMark
  49. setMark(mark, container, e);
  50. }
  51. };
  52. //onmouseleave也可以显示冒泡机制;
  53. oTab.onmouseleave = function () {
  54. var mark = document.getElementById("mark");
  55. if (mark) {
  56. //当离开的时候移除mark节点;
  57. this.removeChild(mark);
  58. this.removeChild(container);
  59. }
  60. };
  61. var jiance=document.getElementById("jiance");
  62. function setMark(mark,container,e) {
  63. e = e || window.event;
  64. //上面是绑定事件的标配;
  65. //获取mark这个div的宽度和高度;并且让mark这个DIV显示在鼠标出现位置的正中间;
  66. var markW = mark.offsetWidth;
  67. var markH = mark.offsetHeight;
  68. var l = e.clientX - tabL - (markW / 2);
  69. var t = e.clientY - tabT - (markH / 2);
  70. mark.style.left = l + "px";
  71. mark.style.top = t + "px";
  72. container.style.left=tabW+10+"px";
  73. container.style.top=0;
  74. bigImg.style.left="-"+l/tabW*bigImg.width+"px";
  75. bigImg.style.top="-"+t/tabH*bigImg.height+"px";
  76. //下面是判断边界,当鼠标移到左边界和右边界的判断;
  77. if (l < 0) {
  78. mark.style.left = 0;
  79. } else if (l > (tabW - markW)) {
  80. mark.style.left = tabW - markW + "px";
  81. bigImg.style.left="-"+(tabW-markW)/tabW*bigImg.width+"px";
  82. }
  83. //当鼠标移到上边界和下边界的判断;
  84. if (t < 0) {
  85. mark.style.top = 0 + "px";
  86. } else if (t > (tabH - markH)) {
  87. mark.style.top = tabH - markH + "px";
  88. bigImg.style.top="-"+(tabH-markH)/tabH*bigImg.height+"px";
  89. }
  90. };
  91. </script>

实现方式二:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>电子商务网站放大镜效果2</title>
  6. <style type="text/css">
  7. html, body, div { margin: 0; padding: 0; }
  8. #tab { position: relative; width: 300px; height: 300px;margin: 50px ; background: #CDE074; }
  9. #mark { position: absolute; top: 0; left: 0; width: 100px; height: 100px; background: #B00000; opacity: 0.5; filter: alpha(opacity=50); cursor: move; }
  10. #container{ position:absolute; height:360px; width:360px; background:pink; overflow:hidden;left: 350px;top: 0px}
  11. #bigImg{ position:absolute; display:block; border:none;}
  12. </style>
  13. </head>
  14. <body>
  15. <div id="tab">
  16. <img src="https://img-blog.csdnimg.cn/2020073120293658.jpg" width="300" height="300" id="img1">
  17. </div>
  18. <div id="jiance"></div>
  19. </body>
  20. </html>
  21. <script type="text/javascript">
  22. //获得oDiv本身的高和宽;获得oDiv距离浏览器左上角的位移;
  23. var oDiv = document.getElementById("tab");
  24. var off = offset.call(oDiv);
  25. oDiv.onmouseenter = function (e) {
  26. //小图
  27. var mark = document.createElement("div");
  28. mark.id = "mark";
  29. this.appendChild(mark);
  30. //创建大图容器
  31. var container = document.createElement("div");
  32. container.id = "container";
  33. this.appendChild(container);
  34. var bigImg=document.createElement("img");
  35. bigImg.src="https://img-blog.csdnimg.cn/2020073120293658.jpg";
  36. bigImg.id="bigImg";
  37. container.appendChild(bigImg);
  38. setMark(mark, container, e);
  39. }
  40. oDiv.onmousemove = function (e) {
  41. var mark = document.getElementById("mark");
  42. if (mark) {
  43. critical(this, mark, e);
  44. }
  45. }
  46. oDiv.onmouseleave = function (e) {
  47. var mark = document.getElementById("mark");
  48. if (mark) {
  49. this.removeChild(mark);
  50. }
  51. }
  52. function critical(oDiv, mark, e) {
  53. e = e || window.event;
  54. var l = e.clientX - off.left - (mark.offsetWidth / 2);
  55. var t = e.clientY - off.top - (mark.offsetHeight / 2);
  56. mark.style.top = t + "px";
  57. mark.style.left = l + "px";
  58. if (l <= 0) {
  59. mark.style.left = 0;
  60. } else if (l >= (oDiv.offsetWidth - mark.offsetWidth)) {
  61. mark.style.left = oDiv.offsetWidth - mark.offsetWidth + "px";
  62. }
  63. if (t <= 0) {
  64. mark.style.top = 0;
  65. } else if (t >= (oDiv.offsetHeight - mark.offsetHeight)) {
  66. mark.style.top = oDiv.offsetHeight - mark.offsetHeight + "px";
  67. }
  68. }
  69. function setMark(mark,container,e) {
  70. e = e || window.event;
  71. //上面是绑定事件的标配;
  72. //获取mark这个div的宽度和高度;并且让mark这个DIV显示在鼠标出现位置的正中间;
  73. var markW = mark.offsetWidth;
  74. var markH = mark.offsetHeight;
  75. var l = e.clientX - tabL - (markW / 2);
  76. var t = e.clientY - tabT - (markH / 2);
  77. mark.style.left = l + "px";
  78. mark.style.top = t + "px";
  79. container.style.left=tabW+10+"px";
  80. container.style.top=0;
  81. bigImg.style.left="-"+l/tabW*bigImg.width+"px";
  82. bigImg.style.top="-"+t/tabH*bigImg.height+"px";
  83. //下面是判断边界,当鼠标移到左边界和右边界的判断;
  84. if (l-15 < 0) {
  85. mark.style.left = 0;
  86. } else if (l > (tabW - markW)) {
  87. mark.style.left = tabW - markW + "px";
  88. bigImg.style.left="-"+(tabW-markW)/tabW*bigImg.width+"px";
  89. }
  90. //当鼠标移到上边界和下边界的判断;
  91. if (t < 0) {
  92. mark.style.top = 0 + "px";
  93. } else if (t > (tabH - markH)) {
  94. mark.style.top = tabH - markH + "px";
  95. bigImg.style.top="-"+(tabH-markH)/tabH*bigImg.height+"px";
  96. }
  97. };
  98. function offset() {
  99. var left = this.offsetLeft, top = this.offsetTop, par = this.offsetParent;
  100. while (par) {
  101. left += par.offsetLeft;
  102. top += par.offsetTop;
  103. if (window.navigator.userAgent.indexOf("MSIE 8.0") <= -1) {
  104. left += par.clientLeft;
  105. top += par.clientTop;
  106. }
  107. par = par.offsetParent;
  108. }
  109. return {
  110. left: left,
  111. top: top
  112. };
  113. }
  114. </script>

实现方式三:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>电子商务网站放大镜效果</title>
  6. <style type="text/css">
  7. html, body, div img{ margin: 0; padding: 0; }
  8. #tab { position: relative; width: 450px; height: 450px;margin: 50px ; background: #CDE074; }
  9. #mark { position: absolute; top: 0; left: 0; width: 100px; height: 100px; background: #B00000; opacity: 0.5; filter: alpha(opacity=50); cursor: move; }
  10. #container{ position:absolute; height:450px; width:450px; background:pink; overflow:hidden;left: 350px;top: 0px}
  11. #bigImg{ position:absolute; display:block; border:none;}
  12. </style>
  13. </head>
  14. <body>
  15. <div id="tab">
  16. <img src="https://img-blog.csdnimg.cn/2020073120293658.jpg" width="450" height="450" id="img1">
  17. </div>
  18. <div id="jiance"></div>
  19. </body>
  20. </html>
  21. <script type="text/javascript">
  22. var oTab = document.getElementById("tab");
  23. //获得oDiv本身的高和宽;获得oDiv距离浏览器左上角的位移;
  24. var tabT = oTab.offsetTop;
  25. var tabL = oTab.offsetLeft;
  26. var tabW = oTab.offsetWidth;
  27. var tabH = oTab.offsetHeight;
  28. //onmouseenter限制冒泡机制
  29. oTab.onmouseenter = function (e) {
  30. //创建大图容器
  31. var container = document.createElement("div");
  32. container.id = "container";
  33. this.appendChild(container);
  34. var bigImg=document.createElement("img");
  35. bigImg.src="https://img-blog.csdnimg.cn/2020073120293658.jpg";
  36. bigImg.id="bigImg";
  37. container.appendChild(bigImg);
  38. //当鼠标移近来的时候,动态创建一个DIV元素,id是mark
  39. var mark = document.createElement("div");
  40. mark.id = "mark";
  41. this.appendChild(mark);//像oTab里添加mark;
  42. //执行setMark
  43. setMark(mark, container, e);
  44. };
  45. oTab.onmousemove = function (e) {
  46. var mark = document.getElementById("mark");
  47. if (mark) {
  48. //先判断mark是否存在,如果存在,执行setMark
  49. setMark(mark, container, e);
  50. }
  51. };
  52. //onmouseleave也可以显示冒泡机制;
  53. oTab.onmouseleave = function () {
  54. var mark = document.getElementById("mark");
  55. if (mark) {
  56. //当离开的时候移除mark节点;
  57. this.removeChild(mark);
  58. this.removeChild(container);
  59. }
  60. };
  61. var jiance=document.getElementById("jiance");
  62. function setMark(mark,container,e) {
  63. e = e || window.event;
  64. //上面是绑定事件的标配;
  65. //获取mark这个div的宽度和高度;并且让mark这个DIV显示在鼠标出现位置的正中间;
  66. var markW = mark.offsetWidth;
  67. var markH = mark.offsetHeight;
  68. var l = e.clientX - tabL - (mark.offsetWidth / 2);
  69. var t = e.clientY - tabT - (mark.offsetHeight / 2);
  70. mark.style.left = l + "px";
  71. mark.style.top = t + "px";
  72. container.style.left=tabW+10+"px";
  73. container.style.top=0;
  74. // bigImg.style.left="-"+(l/tabW*bigImg.width)+"px";
  75. // bigImg.style.top="-"+(t/tabH*bigImg.height)+"px";
  76. //如果样式用上面的写,在IE678的时候有兼容性问题,应该改为下面的
  77. if(l<=0){
  78. bigImg.style.left=="0px";
  79. }else{
  80. bigImg.style.left="-"+l/tabW*bigImg.width+"px";
  81. }
  82. if(t<=0){
  83. bigImg.style.top=="0px";
  84. }else{
  85. bigImg.style.top="-"+(t/tabH*bigImg.height)+"px";
  86. }
  87. console.log("====t::"+parseInt(t));
  88. //下面是判断边界,当鼠标移到左边界和右边界的判断;
  89. if (l < 0) {
  90. mark.style.left = 0;
  91. } else if (l > (tabW - markW)) {
  92. mark.style.left = tabW - markW + "px";
  93. bigImg.style.left="-"+(tabW-markW)/tabW*bigImg.width+"px";
  94. }
  95. //当鼠标移到上边界和下边界的判断;
  96. if (t < 0) {
  97. mark.style.top = 0 + "px";
  98. } else if (t > (tabH - markH)) {
  99. mark.style.top = tabH - markH + "px";
  100. bigImg.style.top="-"+(tabH-markH)/tabH*bigImg.height+"px";
  101. }
  102. };
  103. </script>

 

文章来源: sunmenglei.blog.csdn.net,作者:孙叫兽,版权归原作者所有,如需转载,请联系作者。

原文链接:sunmenglei.blog.csdn.net/article/details/111468582

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。